-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Programatically fail a test #2129
Comments
I'm not sure if we want to have a function like this. Instead you could test if |
How would you use toThrow() to check for a rejection of a promise? Isn't toThrow only used to check if errors are thrown in a particular function? |
If you want to test Promise rejection, you can still go on with something like this: it('tests if getUserName rejects promise', () => {
user.getUserName(2)
.then(() => {}, error => {
expect(error).toBe('My rejected Promise')
})
}); There are plenty of ways, I just don't see the reason to add another one. |
The problem with your method is that if the promise resolves then the test will pass even though you expect it to reject. The test should fail if the promise resolves. It should only pass if it rejects with the exact error that you expected. it('tests error with async/await', async () => {
try {
await user.getUserName(2); //If this resolves then the test will pass
} catch (object) {
expect(object.error).toEqual('User with 2 not found.');
}
}); |
I'm not sure if it's possible to do with async/await syntax, I didn't play with it much to be honest. But maybe we could introduce a new matcher e.g. Then you could test the case like this: it('rejects on username 2', () => {
const object = {error: 'User with 2 not found'};
expect(user.getUserName(2)).toBeRejected(object);
}) |
Yeah such an API would be awesome! And you could also use snapshots here: it('rejects on username 2', () => {
expect(user.getUserName(2)).toBeRejectedWithSnapshot(); |
Closing this in favour of #1377. You're welcome do discuss new this API shape there. |
Somehow this is not documented, but since Jest uses Jasmin this is possible: test('How to manually fail tests', done => {
done.fail(new Error('I want my test to fail'))
}) |
You can also call For example I wanted any call to // console.error should fail the test
beforeEach(done => {
jest.spyOn(global.console, 'error').mockImplementation(e => {
done.fail(e);
});
done(); // it is important to call this here or every test will timeout
}); |
The above did not work for me. But managed to make it work using this, |
In case anyone else comes across this, the global function
|
This is the idiomatic example from the docs:
|
This feature is also useful in case you are programmatically generating As the Here is my example. describe('examples from file', () => {
let lines;
beforeAll(() => new Promise(resolve => {
lines = []
// asynchronously read each line into lines
}))
it('should accept all lines in samples.txt', done => {
lines.forEach(line => {
if (!someBooleanChecking(line)) {
done.fail(new Error(`"${line}" was NOT accepted !`))
} else {
done()
}
})
})
}) |
Won't this pass if fetchData() doesn't throw as the test is expecting? Therefore is something like this required?
|
No, |
That will stop working at some point - it's not part of Jest's documented API. Also, any reason why you aren't using this? test('the fetch fails with an error', async () => {
expect.assertions(1);
await expect(fetchData()).rejects.toMatch('error');
}); |
I totally missed the expect.assertions(1) line. nice |
So related to this, I'm currently trying to force my Jest tests to fail if
Raising an error does not work reliably when dealing with asynchronous React code, unfortunately. Does anyone have any thoughts on how this could be made to work without resort to It does look like using |
When we use it with restartBrowserBetweenTests:true -done.fail(msg) causing no such session error to the rest of the test cases when we perform it in chrome |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
In the jest docs it mentions an example in the async tutorial
I think that this code should be written like this:
The
fail()
will prevent the test from passing ifgetUserName()
does not throw an error.However, I see no mention of the
fail()
command anywhere in the docs, is this a supported API?The text was updated successfully, but these errors were encountered: