-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async support? #28
Comments
We're having this issue as well. It looks like the problem has been addressed via events. I found this pull request from early last year -- #11. However, as you'll see in my comment, I've been unable to get it working. Hope you'll have better luck! |
👎 has anyone got this to work with async yet? I will try to meddle more but it'd be awesome if someone else has. |
Nope I couldn't, so moved over to Supertest with sinon for mock/stub. |
Not really sure how at this point with really changing its simple interface. I'll be curious if your able to come up with a solution.
|
@jayeshgangadharan, I know I'm relatively new to this project, but I would like to give my two cents on this issue.
I think my two cents require more explanation ... In your example I assume The async call to In the test below I simulate the two possible conditions in the callback (with and without an error); var MongooseCategory = {
find: function() {}
}
afterEach(function() {
MongooseCategory.find.restore();
})
it('shoud return doc', function() {
var request = mocks.createRequest();
var response = mocks.createResponse();
// simulate MongooseCategory.find() returns a doc
sinon.stub(MongooseCategory, 'find', function(query, callback) {
callback(null, [{ name: 'value' }]);
});
getData(request, response);
console.log(response._getData());
});
it('shoud return error', function() {
var request = mocks.createRequest();
var response = mocks.createResponse();
// simulate MongooseCategory.find() returns an error
sinon.stub(MongooseCategory, 'find', function(query, callback) {
callback('error message');
});
getData(request, response);
console.log(response._getData());
}); I just ran this test using the current version of
The concept employed above can be easily extrapolated and transferred to any use-case involving async calls.
It is therefore, IMHO, each developer's responsibility to mock/stub calls to any async functions/methods that are not part of the unit under test. As-is, |
@howardabrams ... I'm closing this issue for now. If anyone believes it requires further discussion, please feel free to reopen. |
This my (first) take on simple async testing with this library: |
@chmanie, that's a nice solution. Thanks for sharing. The solution I provided above intended to use Thanks again for sharing! :-) |
What's wrong with this out-of-the-box solution? var mock_res = httpMocks.createResponse({
eventEmitter: require('events').EventEmitter
}); events being the node default events library ofc |
Some of the functions ( EDIT: In the current version (1.4.3) only |
@chmanie, thanks for point that out. We are currently aware of some the current inconsistencies in the implementation. We're in the process of overhauling |
Solution described in #159 seems like the easiest to implement here is typescript example, works with next.js |
Can i use node-mocks-http for testing async? for eg: I have this in my router which can be reached through GET /category/list
var getData = function (req, res) {
MongooseCategory.find({}, function (err, docs) {
if (!err) {
res.json(200, { categories: docs });
} else {
res.json(500, { message: err });
}
});
};
In the test
var response = httpMocks.createResponse();
getData(request, response);
console.log(response._getData());
but the response does not contain the json (response comes back later after a few seconds). How can i test this? Any help is much appreciated.
The text was updated successfully, but these errors were encountered: