Reputation: 432
I'm having some trouble getting a new test off the ground with this Frisby.js API testing framework.
For context, I have written some other tests that don't require reading in reference files from disk, and have run some of the samples that came with Frisby, and they all run very quickly and accurately. Really liking the speed of execution so far. Seeing as how all of that comes back OK, I am fairly certain that my environment is sound.
Below is a simplified version of a problematic JavaScript file, that I am running through jasmine-node:
var frisby = require('frisby');
var fs = require('fs');
var path = require('path');
var URL = 'http://server/api/';
var getJSON = fs.readFileSync(path.resolve(__dirname, 'GET.json'), 'utf-8').replace(/^\uFEFF/, ''); // the .replace removes the BOM from the start of the file
console.log(getJSON); // this dumps the file contents to the screen, no problems here
frisby.create('GET from API')
.get(URL + 'Endpoint?Parameters=Values')
.expectStatus(200) // tests that HTTP Status code equals expected 200, still no worries
.expectJSON(getJSON) // this is where the 'undefined' error is thrown
.toss();
Running the test from the command line is very straight forward:
C:\Testing\Frisby> jasmine-node apitest.js
I believe I'm doing things in the right order, with the synchronous file read, and then the Frisby calls afterwards, but when executed it throws the following error:
Failures:
1) Frisby Test: GET from API
[ GET http://server/api/Endpoint?Parameters=Values ]
Message:
TypeError: Expected valid JavaScript object to be given, got undefined
Stacktrace:
TypeError: Expected valid JavaScript object to be given, got undefined
at _jsonContains (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:1182:11)
at jasmine.Matchers.toContainJson (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:1141:12)
at null.<anonymous> (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:686:24)
at null.<anonymous> (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:1043:43)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Finished in 0.292 seconds
1 test, 2 assertions, 1 failure, 0 skipped
I have the jasmine-node and frisby packages both installed globally via npm, and have used npm link frisby
which created the appropriate junction from my testing directory over to %APPDATA%\npm.
I have also tried changing the code around, to use fs.readFile
instead of fs.readFileSync
with the frisby calls inside the callback, but still have the same problem.
As I said above, my other tests and the samples that came with Frisby run and come back without error. Specifically, the httpbin_binary_post_put_spec.js
sample uses almost identical code to what I ended up writing myself, and that sample works just fine.
I have routed the HTTP requests through Fiddler and can see the request and response, and everything looks fine there. It gets a HTTP 200 and the response body has the expected JSON that I want to compare against the file contents.
Why am I getting this error about an undefined object?
Upvotes: 1
Views: 1964
Reputation: 432
Rubber duck problem solving seems to have rescued me from my own stupidity.
The string needed to be turned into a proper JSON object:
.expectJSON(getJSON)
-> .expectJSON(JSON.parse(getJSON))
Upvotes: 2