Mocha + supertest + assert: print response body for testing error

I use mocha, supertest and assert to test my Express application. The My Express application runs in development mode, so if the request fails, useful information about debugging as JSON is returned. I would like to print this data in my test suite, but only when the test failed. An example of one of my tests (in CoffeeScript):

assert = require "assert" request = require "supertest" url = request "http://localhost:3000" describe "GET /user/:id", -> it "should return one user", (done) -> url .get("/user" + id) .expect(200) .expect("Content-Type", /json/) .end (err, res) -> if err done err else # assuming the test reaches here, but fails on one of the following, # how do i make mocha print res.body? assert.equal(res.body.name, user.name) assert.equal(res.body.email, user.email) done() 

How to make mocha print res.body, but only if the test fails? I would prefer that each describe block does not fit something like console.log(res.body) if test.failed .

+5
source share
2 answers

A few ways to achieve this:

Option 1: I would just use an if condition to check the failure status in the else block and make console.log (res.body)

Option 2: or in the callback function, if there is an error, you can return res.body.

For instance:

Use something like below at the end

 .end(function(err, res){ if (err) throw err; if (!res.body.password) assert.fail(res.body.password, "valid password", "Invalid password") else done() }); 

You can use res.body instead of res.body.password as well

Try this, it should work.

+1
source

I do it the same way:

 var supertest = require("supertest"); var should = require("should"); var util = require('util'); describe("My test",function(){ var response; it("should validate the API key",function(done){ server .post("/validate") .set('authorization', apiKey) .expect("Content-type",/json/) .expect(200) .end(function(err,res){ response = res; res.status.should.equal(200); res.body.error.should.equal(false); done(); }); }); afterEach(function(){ if (this.currentTest.state == 'failed') { console.log(" Response body: " + util.inspect(response.body,{depth: null, colors: true}) + "\n"); } }) }); 

I highlighted the response variable in my test area, and each test sets it to the given response ( response = res; ). I have to do this once in each test, but then I do not need to worry when and where it fails. Previously, I had to be careful, because if the test failed, some code under it would not be executed, so it would not even reach the print statement. This way I save what I need, regardless of the result.

Then after each test, this afterEach event will start and check if the test passed or failed.

  afterEach(function(){ if (this.currentTest.state == 'failed') { console.log(" Response body: " + util.inspect(response.body,{depth: null, colors: true}) + "\n"); } }) 

This provides a consistent printing method for each test. This is just 1 line for all tests, so it’s easy to change the format or turn it off, and I don’t need to worry about where the test failed, I just care about the end result. It seems to me the best and easiest approach, of all the lazy approaches. Even the JSON output is beautifully and colorfully displayed. There is probably a better way to handle this, but this is a good lazy approach.

+1
source

Source: https://habr.com/ru/post/1200517/


All Articles