When running mocha tests using npm run test, is it possible to have the contents of the response body printed whenever a test fails with an error?
chai.request(server)
.post('/')
.set('X-Access-Token', testUser.accessToken)
.send(fields)
.end((error, response) => {
console.log(response.body); // log this!
response.should.have.status(201); // if this fails!
done();
});
});
In other words, could the afterEach function have access to error and response for each test?
afterEach(function(error, response) {
if (error) console.log('afterEach', response.body);
});
We have useful error messages coming down in the response, so we find ourselves pasting that console.log line into the failing test to debug. It'd be nice to always see the response.body on each error.
OP here - I came up with an answer and figured I'd leave it here until someone comes up with a better one.
The reason it's not ideal is that it requires a single line in each test, which updates a shared variable currentResponse with that test's response. But if your tests span many files, you can maintain a global variable in your setup script:
// you can use a global variable if tests span many files
let currentResponse = null;
afterEach(function() {
const errorBody = currentResponse && currentResponse.body;
if (this.currentTest.state === 'failed' && errorBody) {
console.log(errorBody);
}
currentResponse = null;
});
And then each of your tests would update the current response, so we can log it in the afterEach, in the event that it fails.
describe('POST /interests', () => {
it('400s if categoryName field is not present in the category', done => {
const fields = [
{ language: 'en' },
];
chai.request(server)
.post('/interests')
.set('X-Access-Token', testUser.accessToken)
.send(fields)
.end((error, response) => {
currentResponse = response; // update it here
response.should.have.status(400);
done();
});
});
And this will output the response whenever there's an error, so you can see what the server returned.
Related
I am writing end-to-end tests for a web application in cypress and I would like cypress to fail the test if the applications sends any request with a http 500 error response.
I am using cypress in version 3.1.5. I already tried the following, but I get an promise error.
cy.server();
cy.route({
url: '**',
onResponse: (xhr) => {
cy.log(xhr);
}
});
I hope to find a more elegant solution to this problem, because this sounds to me like a pretty standard use-case.
Try using below code refer doc
cy.intercept('**').as('all')
cy.wait('#all').its('response.statusCode').should('not.be.oneOf', [500])
OR
Checking status code using - should('be.oneOf', [200,300])
cy.wait('#all').its('response.statusCode').should('be.oneOf', [200,300])
If you're using a Cypress version that supports intercept (v7.x.x or later), then you can do this in your test file:
beforeEach(() => {
cy.intercept('**', request => {
request.on('response', function (response) {
expect(response.statusCode).is.lessThan(500); // Test will fail if an 500 error happen
});
});
});
Not sure if there's some canonical way, but you can monkey-patch the XMLHttpRequest API and throw on >= 500 status responses.
If your app is using Fetch API, SendBeacon, or something else, you can do something similar.
// put this in support/index.js
Cypress.on('window:before:load', win => {
const origSend = win.XMLHttpRequest.prototype.send;
win.XMLHttpRequest.prototype.send = function () {
// deferring because your app may be using an abstraction (jquery),
// which may set this handler *after* it sets the `send` handler
setTimeout(() => {
const origHandler = this.onreadystatechange;
this.onreadystatechange = function () {
if ( this.readyState === 4 && this.status >= 500 ) {
throw new Error(`Server responded to "${this.url}" with ${this.status}`);
}
if ( origHandler ) {
return origHandler.apply(this, arguments);
}
};
return origSend.apply(this, arguments);
});
};
});
WTBS, this is not gonna be very useful because you app may want to handle that 500 instead, while this will prevent it.
Better solution is to make sure you app simply throws unhandled 500 responses.
You can throw an error inside the on request, that should work.
cy.server();
cy.route({
url: '**',
onResponse: (xhr) => {
if(xhr.status === 500) {
throw new Error('Failing test caused by a unhandled request with status 500')
}
cy.log(xhr);
}
})
I would like to test my simple API that has /groups URL.
I want to make an API request to that URL (using Axios) before all tests begin and make the response visible to all test functions.
I am trying to make the response visible but not able to make it work. I followed a similar case with filling out the DB upfront but no luck with my case.
My simple test file below:
var expect = require('chai').expect
var axios = require('axios')
var response = {};
describe('Categories', function() {
describe('Groups', function() {
before(function() {
axios.get(config.hostname + '/groups').then(function (response) {
return response;
})
});
it('returns a not empty set of results', function(done) {
expect(response).to.have.length.greaterThan(0);
done();
})
});
});
I tried also a sligh modification of before function:
before(function(done) {
axios.get(config.hostname + '/groups')
.then(function (response) {
return response;
}).then(function() {
done();
})
});
but no luck too.
The error I am getting is simply that response isn't changing nor is visible within it. AssertionError: expected {} to have property 'length'
Summarising: How can I pass response from axios inside to in()?
Your first form is incorrect, because you're not returning the chained promise. As such, mocha has no way of knowing when your before is finished, or even that it's async at all. Your second form will solve this problem, but since axios.get already returns a promise, it's kind of a waste not to use mocha's built-in promise support.
As for making the response visible in the it, you need to assign it to a variable in a scope that will be visible within the it.
var expect = require('chai').expect
var axios = require('axios')
var response;
describe('Categories', function() {
describe('Groups', function() {
before(function() {
// Note that I'm returning the chained promise here, as discussed.
return axios.get(config.hostname + '/groups').then(function (res) {
// Here's the assignment you need.
response = res;
})
});
// This test does not need the `done` because it is not asynchronous.
// It will not run until the promise returned in `before` resolves.
it('returns a not empty set of results', function() {
expect(response).to.have.length.greaterThan(0);
})
});
});
I'm trying to setup a node-jasmine test for the first time. Currently I'm just trying to setup a simple test to see that getting the index returns status 200.
It seemed to be working but I noticed no matter what I change the status number to it never fails, for example expecting status 666, but I don't get a failure:
const request = require("request")
const helloWorld = require("../app.js")
const base_url = "http://localhost:3002/"
describe("Return the index page", function() {
describe("GET /", function() {
it("returns status code 200", function() {
request.get(base_url, function(error, response, body) {
expect(response.statusCode).toBe(666)
done()
})
})
})
})
Which returns:
Finished in 0.009 seconds
1 test, 0 assertions, 0 failures, 0 skipped
When I expected a failure here.
You need to include the done callback as a parameter to the test function.
Eg:
it("returns status code 200", function(done) {
request.get(base_url, function(error, response, body) {
expect(response.statusCode).toBe(666)
done();
})
})
Without this, the test is completing before the asynchronous request returns.
While it looks like you found your answer, I came here with a similar problem. My problem was that the request was failing, and the assertion was never reached. Once I added an error catch like below, I found my problem!
I'm new to Jasmine, but it seems odd that an exception generated inside your test wouldn't be more visible or apparent, so if anyone has feedback on how to better handle, let me know.
const request = require("request")
const helloWorld = require("../app.js")
const base_url = "http://localhost:3002/"
describe("Return the index page", function() {
describe("GET /", function() {
it("returns status code 200", function() {
request.get(base_url, function(error, response, body) {
if (error)
console.log("Something borked: ", error);
expect(response.statusCode).toBe(666)
done()
})
})
})
})
I have the following route (express) for which I'm writing an integration test.
Here's the code:
var q = require("q"),
request = require("request");
/*
Example of service wrapper that makes HTTP request.
*/
function getProducts() {
var deferred = q.defer();
request.get({uri : "http://localhost/some-service" }, function (e, r, body) {
deferred.resolve(JSON.parse(body));
});
return deferred.promise;
}
/*
The route
*/
exports.getProducts = function (request, response) {
getProducts()
.then(function (data) {
response.write(JSON.stringify(data));
response.end();
});
};
I want to test that all the components work together but with a fake HTTP response, so I am creating a stub for the request/http interactions.
I am using Chai, Sinon and Sinon-Chai and Mocha as the test runner.
Here's the test code:
var chai = require("chai"),
should = chai.should(),
sinon = require("sinon"),
sinonChai = require("sinon-chai"),
route = require("../routes"),
request = require("request");
chai.use(sinonChai);
describe("product service", function () {
before(function(done){
sinon
.stub(request, "get")
// change the text of product name to cause test failure.
.yields(null, null, JSON.stringify({ products: [{ name : "product name" }] }));
done();
});
after(function(done){
request.get.restore();
done();
});
it("should call product route and return expected resonse", function (done) {
var writeSpy = {},
response = {
write : function () {
writeSpy.should.have.been.calledWith("{\"products\":[{\"name\":\"product name\"}]}");
done();
}
};
writeSpy = sinon.spy(response, "write");
route.getProducts(null, response);
});
});
If the argument written to the response (response.write) matches the test passes ok. The issue is that when the test fails the failure message is:
"Error: timeout of 2000ms exceeded"
I've referenced this answer, however it doesn't resolve the problem.
How can I get this code to display the correct test name and the reason for failure?
NB A secondary question may be, could the way the response object is being asserted be improved upon?
The problem looks like an exception is getting swallowed somewhere. The first thing that comes to my mind is adding done at the end of your promise chain:
exports.getProducts = function (request, response) {
getProducts()
.then(function (data) {
response.write(JSON.stringify(data));
response.end();
})
.done(); /// <<< Add this!
};
It is typically the case when working with promises that you want to end your chain by calling a method like this. Some implementations call it done, some call it end.
How can I get this code to display the correct test name and the reason for failure?
If Mocha never sees the exception, there is nothing it can do to give you a nice error message. One way to diagnose a possible swallowed exception is to add a try... catch block around the offending code and dump something to the console.
In my node application I'm using mocha to test my code. While calling many asynchronous functions using mocha, I'm getting timeout error (Error: timeout of 2000ms exceeded.). How can I resolve this?
var module = require('../lib/myModule');
var should = require('chai').should();
describe('Testing Module', function() {
it('Save Data', function(done) {
this.timeout(15000);
var data = {
a: 'aa',
b: 'bb'
};
module.save(data, function(err, res) {
should.not.exist(err);
done();
});
});
it('Get Data By Id', function(done) {
var id = "28ca9";
module.get(id, function(err, res) {
console.log(res);
should.not.exist(err);
done();
});
});
});
You can either set the timeout when running your test:
mocha --timeout 15000
Or you can set the timeout for each suite or each test programmatically:
describe('...', function(){
this.timeout(15000);
it('...', function(done){
this.timeout(15000);
setTimeout(done, 15000);
});
});
For more info see the docs.
I find that the "solution" of just increasing the timeouts obscures what's really going on here, which is either
Your code and/or network calls are way too slow (should be sub 100 ms for a good user experience)
The assertions (tests) are failing and something is swallowing the errors before Mocha is able to act on them.
You usually encounter #2 when Mocha doesn't receive assertion errors from a callback. This is caused by some other code swallowing the exception further up the stack. The right way of dealing with this is to fix the code and not swallow the error.
When external code swallows your errors
In case it's a library function that you are unable to modify, you need to catch the assertion error and pass it onto Mocha yourself. You do this by wrapping your assertion callback in a try/catch block and pass any exceptions to the done handler.
it('should not fail', function (done) { // Pass reference here!
i_swallow_errors(function (err, result) {
try { // boilerplate to be able to get the assert failures
assert.ok(true);
assert.equal(result, 'bar');
done();
} catch (error) {
done(error);
}
});
});
This boilerplate can of course be extracted into some utility function to make the test a little more pleasing to the eye:
it('should not fail', function (done) { // Pass reference here!
i_swallow_errors(handleError(done, function (err, result) {
assert.equal(result, 'bar');
}));
});
// reusable boilerplate to be able to get the assert failures
function handleError(done, fn) {
try {
fn();
done();
} catch (error) {
done(error);
}
}
Speeding up network tests
Other than that I suggest you pick up the advice on starting to use test stubs for network calls to make tests pass without having to rely on a functioning network. Using Mocha, Chai and Sinon the tests might look something like this
describe('api tests normally involving network calls', function() {
beforeEach: function () {
this.xhr = sinon.useFakeXMLHttpRequest();
var requests = this.requests = [];
this.xhr.onCreate = function (xhr) {
requests.push(xhr);
};
},
afterEach: function () {
this.xhr.restore();
}
it("should fetch comments from server", function () {
var callback = sinon.spy();
myLib.getCommentsFor("/some/article", callback);
assertEquals(1, this.requests.length);
this.requests[0].respond(200, { "Content-Type": "application/json" },
'[{ "id": 12, "comment": "Hey there" }]');
expect(callback.calledWith([{ id: 12, comment: "Hey there" }])).to.be.true;
});
});
See Sinon's nise docs for more info.
If you are using arrow functions:
it('should do something', async () => {
// do your testing
}).timeout(15000)
A little late but someone can use this in future...You can increase your test timeout by updating scripts in your package.json with the following:
"scripts": {
"test": "test --timeout 10000" //Adjust to a value you need
}
Run your tests using the command test
For me the problem was actually the describe function,
which when provided an arrow function, causes mocha to miss the
timeout, and behave not consistently. (Using ES6)
since no promise was rejected I was getting this error all the time for different tests that were failing inside the describe block
so this how it looks when not working properly:
describe('test', () => {
assert(...)
})
and this works using the anonymous function
describe('test', function() {
assert(...)
})
Hope it helps someone, my configuration for the above:
(nodejs: 8.4.0, npm: 5.3.0, mocha: 3.3.0)
My issue was not sending the response back, so it was hanging. If you are using express make sure that res.send(data), res.json(data) or whatever the api method you wanna use is executed for the route you are testing.
Make sure to resolve/reject the promises used in the test cases, be it spies or stubs make sure they resolve/reject.