Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use supertest to check the body of response - getting an error

I am trying to use supertest for some testing. Here is the code snippet that I am trying to test:

it("should create a new org with valid privileges and input with status 201", function(done) {   request(app)     .post("/orgs")     .send({ name: "new_org", owner: "[email protected]", timezone: "America/New_York", currency: "USD"})     .expect(201)     .end(function(err, res) {       res.body.should.include("new_org");       done();     }); }); 

I am getting an error when trying to test the res body:

 TypeError: Object #<Object> has no method 'indexOf'   at Object.Assertion.include (../api/node_modules/should/lib/should.js:508:21)   at request.post.send.name (../api/test/orgs/routes.js:24:27)   at Test.assert (../api/node_modules/supertest/lib/test.js:195:3)   at Test.end (../api/node_modules/supertest/lib/test.js:124:10)   at Test.Request.callback (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:575:3)   at Test.<anonymous> (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:133:10)   at Test.EventEmitter.emit (events.js:96:17)   at IncomingMessage.Request.end (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:12)   at IncomingMessage.EventEmitter.emit (events.js:126:20)   at IncomingMessage._emitEnd (http.js:366:10)   at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)   at Socket.socketOnData [as ondata] (http.js:1367:20)   at TCP.onread (net.js:403:27) 

Is this a bug in supertest, or am I formatting my test incorrectly? Thanks

like image 699
Scott Switzer Avatar asked Jan 15 '13 13:01

Scott Switzer


People also ask

What does SuperTest request do?

It is used to test HTTP servers by sending a method like GET, PUT, POST or DELETE, Supertest can test any HTTP requests. Even multipart file uploads like the one my API has.

What is SuperTest in mocha?

SuperTest is a Node. js library that helps developers test APIs. It extends another library called superagent, a JavaScript HTTP client for Node. js and the browser. Developers can use SuperTest as a standalone library or with JavaScript testing frameworks like Mocha or Jest.


1 Answers

Alternatively, this should work too:

res.body.should.have.property("name", "new_org"); 

Also, just a note but logically I think it makes sense to put this in another call to expects instead of in the final callback. This function can also be re-used, so I tend to put it somewhere reusable when possible:

var isValidOrg = function(res) {   res.body.should.have.property("name", "new_org"); };  it("should create a new org with valid privileges and input with status 201", function(done) {   request(app)     .post("/orgs")     .send({ name: "new_org", owner: "[email protected]", timezone: "America/New_York", currency: "USD"})     .expect(201)     .expect(isValidOrg)     .end(done); }); 

Now you could imagine you're testing a GET for /orgs/:orgId and you could just re-use the same validation.

like image 95
kabuko Avatar answered Sep 21 '22 21:09

kabuko