Async test never calls done() if assertion fails [duplicate] - express

This question already has answers here:
mocha with nodejs assert hangs/timeouts for assert(false) instead of error
(3 answers)
Closed 5 years ago.
I'm trying to unit test a controller method with mocha + chai + sinon, but I'm struggling to understand how the assertion works with these async methods. Here is my test:
it('should fetch all registered books and send them as json', (done) => {
res.json = sandbox.spy();
const books = [];
BookMock.expects('find').withArgs()
.resolves(books);
booksController.index(req, res, () => {
res.json.should.have.been.calledWith(200, { data: books });
done();
});
});
The problem with this test is that if I change the assertion to expect a 300 instead of 200 parameter, for example, my test will stop (fail) and never call done(), leading to a generic failure which tells nothing more than a 'Test failed because done was never called', which says nothing related to the reason the test failed at all.
If I keep the test like that and my controller method does everything good, the test passes and it's all ok, but if the controller does anything wrong, the test fails and doesn't tell me the reason it failed (which should be "res.json was not called with bla bla bla parameters").
I am also not sure this is the correct way to unit test controller methods, since they don't return a Promise I can't use chai-as-promised or promise chains at all, so I used the next() callback (which always gets called using restify) to make the assertion, that might not be right, so I'm open to a full refactor to make this test the most correct.
Thank you in advance!

Digging a little bit more I've found this answer in other question: https://stackoverflow.com/a/26793989/4233017
So I updated my code and wrapped the assertion in a try/catch, ending up like this:
it('should fetch all registered books and send them as json', (done) => {
res.json = sandbox.spy();
const books = [];
BookMock.expects('find').withArgs()
.resolves(books);
booksController.index(req, res, () => {
try {
res.json.should.have.been.calledWith(200, { data: books });
done();
} catch (e) {
done(e);
}
});
});
Now when the test fails it gives me the correct error message. I could also do it with booleans as the answer says, but I think it's better this way.
Any other solution is still very much appreciated.

Related

Cypress - Unable to get the Response Headers - API Automation

I have an API automation test suite using Cypress and one of the issue I am facing in one of the test is to validate the response headers.
For some reason, I am not able to read the response headers using Cypress.
The code is below
cy.request({
method:'GET',
url:Cypress.env("Authorisationurl")+tokenId+'&decision=allow&acr_values=1',
followRedirect: false,
headers:{
'Accept': "/*"
}
}).then((response) => {
const rbody = (response.body);
cy.log(response.status)
//THIS GOT ASSERTED TO TRUE
expect(response.status).to.equal(302)
//OPTION1
cy.wrap(response.headers['X-Frame-Options']).then(() => {
return response.headers['X-Frame-Options'];
});
//OPTION2
return response.headers['X-Frame-Options']
//OPTION3
return response.headers
})
None of the above options gives me the header information. Infact I am confused with the order of execution too.
This is my output.
for the following code.
const rbody = (response.body);
cy.log(response.status)
cy.log(response)
expect(response.status).to.equal(302)
cy.log(response.headers)
cy.log(response.headers['X-Frame-Options'])
return response.headers['X-Frame-Options']
Also, not very sure what Object{9} indicates. Can anyone please explain what is happening here.
I am aware of Cypress flow of execution and the code is written in then block as a call back function.
Option 3 is very scary as it gives an error
cy.then() failed because you are mixing up async and sync code.
In your callback function you invoked 1 or more cy commands but then returned a synchronous value.
Cypress commands are asynchronous and it doesn't make sense to queue cy commands and yet return a synchronous value.
You likely forgot to properly chain the cy commands using another cy.then().
The value you synchronously returned was: Object{9}
Can anyone please help me here as in what is the correct way of doing it. I know Cypress is very quick and easy to use but to move away from Selenium, we need to make coding easier for developers with meaningful error message. Object{9} is not very helpful.
Also, Do I need to use Cy.log ? As the sequence of prints is not what I have written in the code. Thanks very much for your time on this.
Please use like this:
JSON.parse(JSON.stringify(response.headers))["X-Frame-Options"];
The "mixing async and sync code" message is basically saying you should keep the .then() callback simple.
But you can chain more than one .then() to run the async and sync code separately.
Use an alias to "return" the value. Since cy.request() is asynchronous, you will need to wait for the value and the alias pattern is the most straight-forward way to do this reliably.
WRT Object{9}, it's the result of the way Cypress logs complex objects.
Don't use cy.log() to debug things, use console.log().
cy.request( ... )
.then(response => {
expect(response.status).to.equal(200) // assert some properties
})
.then(response => response.headers) // convert response to response.headers
.as('headers')
cy.get('#headers')
.then(headers => {
console.log(headers)
})
Since this was originally posted a year ago and Cypress has had many versions since then, the behavior may have changed, however this works in Cypress 11.
You can access the response.headers array as you would normally expect, however the casing of the header name was not as expected, Postman reported the header as X-Frame-Options, but Cypress would only allow me to access it if I used a lower-cased version of the header name (x-frame-options).
cy.request({
url: '<Url>',
method: 'GET',
failOnStatusCode: false
})
.then((response) => {
expect(response.status).to.eq(200);
expect(response.headers["x-frame-options"]).to.equal("SameOrigin");
})
.its('body')
.then((response) => {
// Add your response testing here
});
});

Angular 8 karma test both success and error of Observable from service

I'm testing MyComponent that, in ngOnInit, calls a service method that returns an observable:
this.myService.getMyData().subscribe(
(data) => {
//do something
},
(error) => {
//do something - error
}
);
I've installed a spy on getMyData, this way:
mySpy = spyOn(myService, 'getMyData').and.returnValue(of(mockObject));
and this covers the "success branch" of subscribe. But I need also to cover the "error branch". I've tried doing this:
spyOn(myService, 'getMyData').and.returnValue(throwError(new Error('Error')));
but of course Jasmine tells me that getMyData has already been spied upon. How can I cover both branches?
What you are doing is correct but, you can't spy on the same service more than once in the same test case.
Hence you need to write a second test case which will cover your error scenario.
Something like this:
it('should run success scenario', () => {
mySpy = spyOn(myService, 'getMyData').and.returnValue(of(mockObject));
})
it('should run error scenario', () => {
mySpy = spyOn(myService, 'getMyData').and.returnValue(throwError(new Error('Error')));
})

Mocha Before Hook not working asynchronously

I can't seem to get the before Hook to be asynchronous.
I've used a before hook to start it up, which it does, but I'm trying to pick up when it fails, so that I can fail/skip the whole block (since there is no server to test with).
I've used the [async] before hook all throughout my code, but in this case it's not waiting for my done to be fired.
Basically, got a bunch of MochaJS tests, and for one block I need to start up a Web Server so that I can make Requests to it to test the RESTful API. I want to make sure that it started successfully, hence the async method.
describe('RESTful Tests', function(){
// Start up the whole express server for these API tests
before(function(done) {
try {
server.on('error', (e) => {
console.error("HERE");
done();
});
listener = server.listen(port, function() {
console.log("Server Started");
done();
});
console.error("AFTER LISTEN");
} catch(err) {
console.error("ON Caught");
done();
}
console.error("At End!!");
});
This runs, and shows as an 1) "before all" hook in the test, but it does not wait for the done to be called. I'm only getting the output...
RESTful Tests
AFTER LISTEN
At End!!
1) "before all" hook
None of the other items show up, but I'll get an exception (expected as I've blocked the port) about the EADDRINUSE. I'm really struggling to find a way to catch this.
I understand that it's in a different (kinda) "thread", so I wouldn't pick up the error, but still.... why is the asynchronous done method not being obeyed?
Seems some of the documentation out there is a bit misleading...
The Node/ExpressJS documentation state to put the .on('error' method on the server object. Seems in this case it needed to be on the listener object!
So my final [working] code looks like this...
listener = server.listen(port, function() {
done();
}).on('error', (function(e) {
this.skip(e.code);
done();
}).bind(this));
Yay!!! :)

How do I unit test rendering views in Express?

my controller queries an API on another server I've already tested and it works correctly. I'm curious how I would go about testing this controller, all it does is fetch the api data and render it. I've already tested and verified that my api works with chai-http so how does one correctly unit test this?
function(req, res){
rp(http://www.foobar.com/api/posts)
.then(function(posts){
res.render('view', { data : posts } );
})
.catch(function(err){
next(err);
})
}
I'm using Tape as opposed to mocha/chai, but my approach might help you.
I just assert that the response includes the text I am expecting.
test('Post /auth/login renders an error raised by the api service', (t) => {
let spy = sinon.spy();
let mock = sinon.stub(api, 'doLogin')
.resolves({dataForView: 'great success'});
request(app)
.post('/auth/login')
.expect(200)
.end((err, res) => {
t.ok(res.text.includes('great success'));
t.end();
mock.restore();
});
});
The key part is the t.ok(res.text.includes('great success'));. I don't bother asserting that my entire view is rendered as expected, just that any data I am passing into the render call actually ends up in the response.

How to test promises in Mongo(ose)/Express app?

I'm using promises to wrap asynchronous (Mongo) DB ops at the end of an (expressJS) route.
I want to try and figure out how to test the following code.
userService
userService.findOne = function (id) {
var deferred = q.defer();
User.findOne({"_id" : id})
.exec(function (error, user) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve(user);
}
});
return deferred.promise;
};
userRoute
var user = function (req, res) {
var userId = req.params.id
, userService = req.load("userService");
// custom middleware that enables me to inject mocks
return userService.findOne(id)
.then(function (user) {
console.log("called then");
res.json({
msg: "foo"
});
}).catch(function (error) {
console.log("called catch");
res.json({
error: error
});
}).done();
};
Here's an attempt to test the above with mocha
userTest
it("when resolved", function (done) {
var jsonSpy = sinon.spy(httpMock.res, "json")
, httpMock = require("/path/to/mock/http/object")
, serviceMock = require("/path/to/mock/service"),
, deferred = q.defer()
, findStub = sinon.stub(serviceMock, "findOne")
.returns(deferred.promise)
, loadStub = sinon.stub(httpMock.req, "load")
.returns(serviceMock),
retPromise;
// trigger route
routes.user(httpMock.req, httpMock.res);
// force promise to resolve?
deferred.resolve();
expect(jsonSpy.called).to.be.true; // fails
// chai as promised
retPromise = findStub.returnValues[0];
expect(retPromise).to.be.fulfilled; // passes
});
the http mock is just an empty object with no-ops where expressJS would normally start rendering stuff. I've added some logging inside those no-ops to get an idea on how this is hanging together.
This isn't really working out. I want to verify how the whole is integrated, to establish some sort of regression suite - but I've effectively mocked it to smithereens and I'm just testing my mocks (not entirely successfully at that).
I'm also noticing that the console logs inside my http mocks triggered by then and catch are firing twice - but the jsonSpy that is invoked inside the actual code (verified by logging out the sinon spy within the userRoute code) is not called in test.
Has anyone got some advice on integration testing strategies for express apps backed by Mongo?
It looks to me like you're not giving your promise an opportunity to fire before you check if the result has been called. You need to wait asynchronously for userService.findOne()'s promise chain to complete before jsonSpy.called will be set. Try this instead:
// start of code as normal
q.when(
routes.user(httpMock.req, httpMock.res),
function() { expect(jsonSpy.called).to.be.true; }
);
deferred.resolve();
// rest of code as normal
That should chain off the routes.user() promise and pass as expected.
One word of caution: I'm not familiar with your framework, so I don't know if it will wait patiently for all async events to go off. If it's giving you problems calling back into your defer chain, you may want to try nodeunit instead, which handles async tests very well (IMO).