Normalize .rejects.toThrow#16163
Open
joshkel wants to merge 1 commit into
Open
Conversation
`.rejects.toThrow` is inconsistent with `.toThrow`: `.toThrow` will accept any string or anything with a `message`, while `.rejects.toThrow` only accepts Error objects. Besides the general inconsistency, this causes a couple of specific issues: * It fails with `Error` objects from other realms (which can happen to with native modules across Jest's vm realms). (See https://github.com/jestjs/jest/blob/v30.4.2/packages/expect-utils/src/utils.ts#L559 and [`Error.isError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError).) * Because of the toThrowMatchers implementation, if a non-Error is rejected, it will be treated as if nothing was thrown, resulting in a very confusing error message ("Received function did not throw"). This has been reported a few times: jestjs#12024, jestjs#9748, jestjs#6675. See also the original implementation at jestjs#5670. Specific notes: - The linked GitHub issues discuss whether "throws" is even the appropriate term for promises. Intuitively, I would expect "rejects" to be equivalent to "throws from an async function," so I would expect Jest's `.rejects.toThrow` to be implemented accordingly. - As a result of Jest's previous implementation, code like `expect(Promise.resolve(new Error('apple'))).rejects.toThrow('apple')` passes. This does not match my intuition, but I'm preserving it for backward compatibility. - Previous versions of Jest unwrap functions within promises: `expect(Promise.reject(() => { throw new Error('apple'); })).rejects.toThrow('apple')` passes. This is unusual, but I'm preserving it for backward compatibility.
✅ Deploy Preview for jestjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
babel-jest
babel-plugin-jest-hoist
babel-preset-jest
create-jest
@jest/diff-sequences
expect
@jest/expect-utils
jest
jest-changed-files
jest-circus
jest-cli
jest-config
@jest/console
@jest/core
@jest/create-cache-key-function
jest-diff
jest-docblock
jest-each
@jest/environment
jest-environment-jsdom
@jest/environment-jsdom-abstract
jest-environment-node
@jest/expect
@jest/fake-timers
@jest/get-type
@jest/globals
jest-haste-map
jest-jasmine2
jest-leak-detector
jest-matcher-utils
jest-message-util
jest-mock
@jest/pattern
jest-phabricator
jest-regex-util
@jest/reporters
jest-resolve
jest-resolve-dependencies
jest-runner
jest-runtime
@jest/schemas
jest-snapshot
@jest/snapshot-utils
@jest/source-map
@jest/test-result
@jest/test-sequencer
@jest/transform
@jest/types
jest-util
jest-validate
jest-watcher
jest-worker
pretty-format
commit: |
Member
|
I like this, thanks! However, I wonder if it's a breaking change and should wait for Jest 31? Any code relying on |
Contributor
Author
|
Yes, it would be a breaking change. I can try covering the special case of Thank you. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
.rejects.toThrowis inconsistent with.toThrow:.toThrowwill accept any string or anything with amessage, while.rejects.toThrowonly accepts Error objects.Besides the general inconsistency, this causes a couple of specific issues:
Errorobjects from other realms (which can happen to with native modules across Jest's vm realms). (See https://github.com/jestjs/jest/blob/v30.4.2/packages/expect-utils/src/utils.ts#L559 andError.isError.)This has been reported a few times: #12024, #9748, #6675. See also the original implementation at #5670.
Specific notes:
.rejects.toThrowto be implemented accordingly.expect(Promise.resolve(new Error('apple'))).resolves.toThrow('apple')passes. This does not match my intuition, but I'm preserving it for backward compatibility.expect(Promise.reject(() => { throw new Error('apple'); })).rejects.toThrow('apple')passes. This is unusual, but I'm preserving it for backward compatibility.Test plan
Note: I have not yet updated the changelog, because I'm not certain if this should be classified as a feature or a fix.