Do you want to request a _feature_ or report a _bug_?
Bug
What is the current behavior?
Passing a non-promise value to rejects or resolves does not fail the test
If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.
describe('someMethod', () => {
it('should reject', () => {
expect(123).rejects.toThrow('error');
});
});
Also this one:
describe('someMethod', () => {
it('should reject', () => {
const fn = new Promise((resolve, reject) => {
resolve(1234);
});
expect(fn).rejects.toThrow('error');
});
});
(node:93202) UnhandledPromiseRejectionWarning: Error: expect(received).rejects.toThrow()
received value must be a Promise.
Received:
number: 123
(node:93202) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
PASS app/remote-control/index.spec.js
test
someMethod
✓ should reject (1ms)
console.warn node_modules/bluebird/js/release/debuggability.js:873
Unhandled rejection error
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.226s, estimated 1s
What is the expected behavior?
Should fail the test
Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
Node v9.3.0
Jest v22.1.2
Yarn v1.3.2
Mac OS X 10.12.6
Good catch!
This function should not be async: https://github.com/facebook/jest/blob/36c705bb1eac380e0926487bbe8b8db0aa2fa2e8/packages/expect/src/index.js#L159-L189
Hi, I'd like to take a look and submit a PR for this issue.
Go for it!
Added another test code in the description that's not as expected
Note that the OP uses resolves/rejects incorrectly - it either has to be returned or awaited. See docs. If that is done, the test behaves as expected.
I still think we should give a proper synchronous error before switching into async mode, though.
There is no way for jest to detect this scenario (help welcome in the lint plugin: https://github.com/jest-community/eslint-plugin-jest/issues/54)
Oh wow, what a bummer, totally forgot about it.
Would be awesome if Jest would let me know!
It should be pretty easy using the eslint plugin. Help welcome 🙂
I don't think it's possible to detect in Jest itself as jest is runtime only - no static analysis (beyond what we do in the babel plugin)
I see.
Might take a try at the eslint rule later.
Is there still something to do in this ticket then?
Will close once #5364 is merged
It's also reports false positives. I think there is something wrong the resolves api.
it('should fail', () => {
expect(Promise.resolve(false)).resolves.toBeTruthy();
});
This test case reports back as passed.
However if I write it another way;
it('should fail', async () => {
const res = await Promise.resolve(false);
expect(res).toBeTruthy();
});
This test case fails, as expected.
$ node --version
v8.9.2
$ jest --version
v22.1.4
EDIT
I do get the following in the console output
(node:9596) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: expect(received).toBeTruthy()
I think return expect(Promise.resolve(false)).resolves.toBeTruthy(); would be correct in the first case as doc says. But we can also give a synchronous error in resolves as rejects will do.
@tur-nr see https://github.com/facebook/jest/issues/5361#issuecomment-359475752
Most helpful comment
Note that the OP uses
resolves/rejectsincorrectly - it either has to bereturned orawaited. See docs. If that is done, the test behaves as expected.I still think we should give a proper synchronous error before switching into
asyncmode, though.There is no way for jest to detect this scenario (help welcome in the lint plugin: https://github.com/jest-community/eslint-plugin-jest/issues/54)