question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

rejects/resolves does not fail the test on non-promise value

See original GitHub issue

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.

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:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
SimenBcommented, Jan 22, 2018

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)

0reactions
github-actions[bot]commented, May 10, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found