Jack Tidbury Jack Tidbury - 1 year ago 78
Node.js Question

Jasmine-node Body is undefined

I am new to jasmine and attempting to run a basic test however I keep getting an error.

My testing is as follows

var request = require('request');
var base_url = 'http://localhost:8080'
var getdata = require('../modules/getdata.js')
var index = require('../index.js')
describe("GetData Test",function(){

describe("Get /", function() {
it("returns statusCode 200", function(done) {
request.get(base_url, function(error, response, body){
it("returns Api is Online", function(done) {
request.get('base_url', function(error, response, body){
expect(body).toBe("Api is online");

This code is testing the following Functions

server.get('/', getdata.test)

and getdata.test is the following

exports.test = function test(req, res) {
res.send('Api is online')

The first test passes, as the responseCode is indeed 200. However the second tests errors as apparently the body is undefined. Any help? The error is as follows:

jack@JacksLaptop:~/workspace$ npm test

> jack_tidbury_web_api@1.0.0 test /home/jack/workspace
> jasmine-node spec

App running on port 8080


1) GetData Test Get / returns Api is Online
Expected undefined to be 'Api is online'.
Error: Expected undefined to be 'Api is online'.
at Request._callback (/home/jack/workspace/spec/getdata_spec.js:17:22)
at self.callback (/home/jack/workspace/node_modules/request/request.js:186:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.init (/home/jack/workspace/node_modules/request/request.js:274:17)
at new Request (/home/jack/workspace/node_modules/request/request.js:128:8)
at request (/home/jack/workspace/node_modules/request/index.js:54:10)
at Function.get (/home/jack/workspace/node_modules/request/index.js:62:12)

Finished in 0.051 seconds
2 tests, 2 assertions, 1 failure, 0 skipped

npm ERR! Test failed. See above for more details.

Answer Source

You meant response body instead of request.body. Also, you are getting 'base_url' instead of base_url. Try this:

it("returns Api is Online", function(done) {
  // Here you were getting 'base_url', which threw an error because that's not a valid url
  request.get(base_url, function(error, response, body){
    console.log(body); // Here you were logging request.body
    expect(body).toBe("Api is online");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download