-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Error thrown yet the test fails #3647
Comments
I'm going to guess that Effectively, something like this happens: try {
setTimeout(() => throw new Error());
} catch (err) {
// do stuff
} The This is more of a design issue; if I'm correct, the code under test isn't easily testable. My suggestion is to make a separate method which will actually bind to the port (you could also accept a callback parameter in your constructor, or use an |
@boneskull So I guess using |
Yes, IIRC Chai doesn’t have Promise-handling stuff build in. Try |
I tried it('should alert', () => {
let port = 5e3;
let fx = () => { new Server(smallApp, port, {}) };
return fx().should.eventually.throw(`Port ${port} is already in use`);
} But it fails even if I use I also tried it('should alert', () => {
let port = 5e3;
let fx = () => { new Server(smallApp, port, {}) };
unexpect(fx, 'to throw', `Port ${port} is already in use`);
} That also fails but it doesn't seem to give as much details as I expected after looking at the website. |
Your constructor returns a Again, my advice is to not attempt to make a connection in the constructor. If you feel you must, you can do this: const {EventEmitter} = require('events');
class Server extends EventEmitter {
constructor(thing, port, opts) {
super();
connect(err => {
if (err) {
this.emit('error', err);
}
});
}
} Then you can test it like this: it('should alert', function(done) {
const server = new Server(smallApp, port, {});
server.on('error', err => {
expect(err.message).to.equal(`Port ${port} is already in use`);
done();
});
}); |
If you go this route, chai-eventemitter is helpful. |
@boneskull Thanks for the suggestion, I'll look into refactoring it and see if it's more testable. |
I've managed to get around this issue (by refactoring the code where the connection is done in an |
Prerequisites
faq
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend avoiding the use of globally installed Mocha.Description
When I'm expecting an error to be thrown, mocha highlights in red the relevant
it
block but without any details (when the assertion inside should pass.When I'm changing the
expect
assertion to expect no thrown errors, it shows up on the console (as I would expect from NodeJS) like it was supposed to throw an error (which is expected).Steps to Reproduce
Test code:
Use
mocha test --exit
as the NPMtest
script and runnpm test
Expected behavior: [What you expect to happen]
1) should alert
should pass (ie. be in grey on the CLI).Actual behavior: [What actually happens]
1) should alert
fails since it's in red.Reproduces how often: [What percentage of the time does it reproduce?]
Everytime even with the following test code:
Which leads to this on the output:
Versions
mocha --version
andnode node_modules/.bin/mocha --version
: 5.2.0node --version
: v11.1.0Additional Information
The text was updated successfully, but these errors were encountered: