-
Notifications
You must be signed in to change notification settings - Fork 176
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
Disable "uncaughtException" handler for tests #782
Comments
@kanongil can you remove all listeners before adding your listener... or remove all listeners but yours in the tests? |
Yeah, I guess I can do a |
It would be handy to have this built-in... So that you could do something like this: it('rethrows a system error to crash the process', async (flags) => {
const team = new Teamwork();
flags.onUnhandledRejection((err) => {
expect(err).to.be.an.error('TypeError: obj.noSuchMethod is not a function');
team.attend();
});
myModule.asyncOperation();
await team.work;
}); I'm thinking of releasing a helper (as suggested - by removing lab's listeners during the test) to deal with the rejections/exceptions, something like this: afterEach(() => {
processErrorHandlers.restore()
});
it('rethrows a system error to crash the process', async (flags) => {
processErrorHandlers.intercept(); // records existing handlers and attaches our own
myModule.asyncOperation();
expect(processErrorHandlers.rejections[0]).to.be.an.error('TypeError: obj.noSuchMethod is not a function');
}); However, if something goes wrong, and they handlers are not reattached - then the process will crash and lab will stop intercepting the errors and things will not look pretty. |
Now that I think of it, maybe the second approach is a nicer API, so lab could have something like flags.recordUnhandledRejections(3); // after three rejections - fail the same way as it happens currently
expect(flags.rejections[0]).to.be.an.error('TypeError: obj.noSuchMethod is not a function'); and would restore/assert the behavior automatically? Edit: then again, this probably needs something like Happy to PR. |
@dominykas can you wrap your operation in a try/catch or create a |
@geek I suppose it's always possible to rearchitect the code in such a way, that all promises/errors thrown are handled or bubble up to a single "main" app An actual use case - I was testing how my lib behaves if the consumer does not attach an error event handler (I do manipulate the errors and I intend it to crash things). |
We might be able to do something with Another potential solution is I'm open to possible solutions |
Maybe not an expectation, but you do need to to a way to "enable" this tracking, because you need to wait for the error to happen, before you finish the test.
Explicit
|
I think those flags are a decent option. In order to simplify things we can also restrict them to synchronous behavior and simply wrap them in try/catch. Are you interested in creating a PR? |
Sure, I'll try to get to this over the next few weeks. |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
In my exiting module I have code that triggers on
uncaughtException
, which I want to test. This works in v14, but fails in v15.Maybe add an option to disable the built-in
uncaughtException
handling?The text was updated successfully, but these errors were encountered: