For a long time i have being trying to correct this but I am unable to find what the issue is.
it('Should be able to DELETE a Valid asset', function(done) {
var jsonPayload = JSON.parse(JSON.stringify(deFaultJsonPayload));
request.post('/ngp/asset').send(jsonPayload)
.set('User', 'admin')
.set('Password', 'YWRtaW4=')
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set('Authorization', 'Basic YWRtaW46YWRtaW4=').expect(200)
request.delete('/ngp/asset' + jsonPayload.location)
.set('User', 'admin')
.set('Password', 'YWRtaW4=')
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set('Authorization', 'Basic YWRtaW46YWRtaW4=')
.expect(404)
.end(function(err, res) {
if (err) {
done.fail(err);
} else {
done();
}
});
});
Here .expect is not being evaluated and returns true for any code.
please help.
+1 for this - is driving me mad
I :+1: for this issue, there really is a bug here.
:+1: This issue makes supertest unusable as a valid testframework!
I have no clue about internals, but please fix this issue ASAP as it's a real show-stopper...
I'm seeing this too. I'm using Jasmine in a node app. Here's a simple example that fails:
var request = require('supertest')('http://google.com');
describe('Just open Google front page', function(){
it('works', function(done) {
request.get('')
.expect(400)
.expect(function(res) {
if (true) return 'should not this fail?!?';
})
.end(function(err, res){
done();
});
});
});
Then I run it thusly...
$ jasmine path/to/this/file.js
(jasmine version 2.3.4)
and get...
.
1 spec, 0 failures
Finished in 0.101 seconds
TL;DR, I don't think there's a problem with supertest here... most likely incorrectly configured test cases.
@heck, I think your sample is incorrectly set up - the expect is evaluated, and the error is being passed to the end callback, but you are not passing it as an argument when calling done. Try the following test... it works fine for me.
var request = require('supertest')('http://google.com');
describe('Just open Google front page', function(){
it('works', function(done) {
request.get('')
.expect(400)
.expect(function(res) {
if (true) return 'should not this fail?!?';
})
.end(function(err, res){
done(err);
});
});
});
results in:
1 failing
1) Just open Google front page works:
Error: expected 400 "Bad Request", got 301 "Moved Permanently"
at Test._assertStatus (node_modules/supertest/lib/test.js:232:12)
at Test._assertFunction (node_modules/supertest/lib/test.js:247:11)
at Test.assert (node_modules/supertest/lib/test.js:148:18)
at assert (node_modules/supertest/lib/test.js:127:12)
at node_modules/supertest/lib/test.js:124:5
at Test.Request.callback (node_modules/supertest/node_modules/superagent/lib/node/index.js:797:3)
at IncomingMessage.<anonymous> (node_modules/supertest/node_modules/superagent/lib/node/index.js:990:12)
at endReadableNT (_stream_readable.js:893:12)
@mona2052, it actually makes sense that the expect method is not raising any errors - your server is probably returning 404. With the way you've set the test up, the two requests (POST and then DELETE) are performed asynchronously - the delete request might be processed before the post request, at which point it does not find the asset, and you actually do get a 404 response. You should instead nest the second request inside the end callback of the first one - this guarantees that the asset is added to the database before trying to delete it. Maybe if you could give the following test a try? Note: I assumed that your test entails you creating an asset, and then deleting it.
it('Should be able to DELETE a Valid asset', function(done) {
var jsonPayload = JSON.parse(JSON.stringify(deFaultJsonPayload));
// Create asset
request.post('/ngp/asset').send(jsonPayload)
.set('User', 'admin')
.set('Password', 'YWRtaW4=')
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set('Authorization', 'Basic YWRtaW46YWRtaW4=')
.expect(200)
.end(function(err) {
if (err) return done(err);
// Delete asset
request.delete('/ngp/asset' + jsonPayload.location)
.set('User', 'admin')
.set('Password', 'YWRtaW4=')
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set('Authorization', 'Basic YWRtaW46YWRtaW4=')
.expect(404) // This should fail now - the asset should be found and deleted
.end(function(err, res) {
done(err);
});
});
});
@dylanrhysscott @Codii @oschwengers, it would be nice if you could provide sample tests where the .expect is failing for you.
@AhmedAKZM I'm not sure if this is 100% relevant to this thread, but I've been experiencing similar issues. Here is an example of a test that does not work. The output suggests that the test was looking for status code '200' instead of the '404' specified.
it('should handle requests to non-existent routes', function(done) {
request(app)
.get('/doesnotexist')
.expect('Content-Type', /json/)
.expect(404, {
errors: [
{
status: "404",
title: "Resource not found",
detail: "Could not locate the requested resource."
}
]
}
)
.end(function(err, res) {
if(err)
done.fail(err);
else
done();
});
});
Thanks for your reply @AhmedAKZM!
Sadly, if I try to run your code exactly I get...
$ jasmine spec/server/supertestSpec.js
Started
.
1 spec, 0 failures
Finished in 0.121 seconds
But, if I take @peterhadlaw 's suggestion...
var request = require('supertest')('http://google.com');
describe('Just open Google front page', function(){
it('works', function(done) {
request.get('')
.expect(400)
.expect(function(res) {
if (true) return 'should not this fail?!?';
})
.end(function(err, res){
if (err) done.fail(err);
});
});
});
Then it does fail:
$ jasmine spec/server/supertestSpec.js
Started
F
Failures:
1) Just open Google front page works
Message:
Failed: expected 400 "Bad Request", got 301 "Moved Permanently"
Stack:
Error: expected 400 "Bad Request", got 301 "Moved Permanently"
at Test._assertStatus
<CALL STACK DELETED BY ME>
1 spec, 1 failure
Finished in 0.104 seconds
Which is livable for me.
@AhmedAKZM: If you clue me in on how to run this through a debugger (Node debugger?) I'll give it a roll.
Thanks,
heck
@peterhadlaw, I tried your test against my own (Hapi.js) server, and it seems to be working fine (*disclaimer: I had to change the expected response to match Hapi's default 404 response). The test passes for any non-existent endpoints. I am using jasmine v2.3.2 and supertest v1.1.0.
var request = require('supertest');
it('should handle requests to non-existent routes', function(done) {
request('http://localhost:8000')
.get('/doesnotexist')
.expect('Content-Type', /json/)
.expect(404, {
statusCode: 404,
error: "Not Found"
})
.end(function(err, res) {
if (err)
done.fail(err);
else
done();
});
});
Rather odd - I'm unable to reproduce your error. What versions of jasmine and supertest are you using? Could you post the output here?
@heck, you're right. My code does pass (when it shouldn't) when using jasmine. I was previously using the mocha suite, which seems to handle the done callback differently. I'm sorry; I should have read your comment more carefully (i.e, should have used jasmine to try to reproduce the error instead of mocha). Your test works fine for me with jasmine.
I have the same problem using v1.1.0
I think in these examples supertest is working as expected.
Please pay attention to this section in the READMe about handling errors in expect if you also are using a .end function.
If you are using the .end() method .expect() assertions that fail will not throw - they will return the assertion as an error to the .end() callback. In order to fail the test case, you will need to rethrow or pass err to done(), as follows:
Let's try to use the supertest tag on stackoverflow for general questions on supertest usage. Thanks!
@AhmedAKZM you sir are a golden god. I was doing done() instead of done(err) in all my tests and pulling my hair out trying to figure out why expect(200) was still passing on a 404 error.
@hamoderr in your second code snippet it uses .end twice. I'm trying to do the same thing and am being yelled at by the console, so I switched it to .then and it still isn't working. Does .end and then .end again in the same block work for you?
Most helpful comment
TL;DR, I don't think there's a problem with supertest here... most likely incorrectly configured test cases.
@heck, I think your sample is incorrectly set up - the expect is evaluated, and the error is being passed to the end callback, but you are not passing it as an argument when calling done. Try the following test... it works fine for me.
results in:
@mona2052, it actually makes sense that the expect method is not raising any errors - your server is probably returning 404. With the way you've set the test up, the two requests (POST and then DELETE) are performed asynchronously - the delete request might be processed before the post request, at which point it does not find the asset, and you actually do get a 404 response. You should instead nest the second request inside the end callback of the first one - this guarantees that the asset is added to the database before trying to delete it. Maybe if you could give the following test a try? Note: I assumed that your test entails you creating an asset, and then deleting it.
@dylanrhysscott @Codii @oschwengers, it would be nice if you could provide sample tests where the .expect is failing for you.