rejects/resolves does not fail the test on non-promise value
See original GitHub issueDo 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
.
Test code
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');
});
});
Output
(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
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:14 (5 by maintainers)
Top Results From Across the Web
Why Promise.all doesn't reject when a non-promise throws an ...
Because the error is thrown before you call Promise.all . It's impossible for Promise.all to convert that to rejection for you.
Read more >Why Promise.all doesn't reject when a non ... - GeeksforGeeks
In this article, we will talk about why Promise.all() method does not get rejected when one of the non-input promises throws an error, ......
Read more >Promise.reject() - JavaScript - MDN Web Docs
The Promise.reject() method returns a Promise object that is rejected with a given reason.
Read more >Testing promise rejection in JavaScript with Jest - Codeleak.pl
Let's consider a simple function that returns a Promise that can either resolve or reject depending on the value of the first argument:...
Read more >JavaScript Promise Tutorial – How to Resolve or Reject ...
First, let us create a generic function that accepts a PokeAPI URL as argument and returns a Promise. If the API call is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Note that the OP uses
resolves
/rejects
incorrectly - it either has to bereturn
ed orawait
ed. 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)
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. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.