From 89d160d99d58c2c9e7b3d84ca78097dfe9c31bd5 Mon Sep 17 00:00:00 2001 From: ibuibu Date: Sat, 12 Nov 2022 21:46:38 +0900 Subject: [PATCH 01/16] Update toThrow() to be able to use Error.cause --- packages/expect/src/toThrowMatchers.ts | 57 ++++++++++++++++++++++---- packages/expect/tsconfig.json | 2 +- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/packages/expect/src/toThrowMatchers.ts b/packages/expect/src/toThrowMatchers.ts index 63d6ff616bfd..bf9d0eaec579 100644 --- a/packages/expect/src/toThrowMatchers.ts +++ b/packages/expect/src/toThrowMatchers.ts @@ -225,14 +225,48 @@ const toThrowExpectedObject = ( thrown: Thrown | null, expected: Error, ): SyncExpectationResult => { - const pass = thrown !== null && thrown.message === expected.message; + function createMessageAndCause(error: Error): string { + if (error.cause instanceof Error) { + return _createMessageAndCause(error); + } else { + return error.message; + } + } + + function _createMessageAndCause(error: Error): string { + if (error.cause instanceof Error) { + return `{ message: ${error.message}, cause: ${_createMessageAndCause( + error.cause, + )}}`; + } else { + return `{ message: ${error.message} }`; + } + } + + function expectedMessageAndCause(error: Error) { + return error.cause === undefined + ? error.message + : createMessageAndCause(error); + } + + function messageAndCause(error: Error) { + return error.cause === undefined ? 'message' : 'message and cause'; + } + + const pass = + thrown !== null && + thrown.message === expected.message && + createMessageAndCause(thrown.value) === createMessageAndCause(expected); const message = pass ? () => // eslint-disable-next-line prefer-template matcherHint(matcherName, undefined, undefined, options) + '\n\n' + - formatExpected('Expected message: not ', expected.message) + + formatExpected( + `Expected ${messageAndCause(expected)}: not `, + expectedMessageAndCause(expected), + ) + (thrown !== null && thrown.hasMessage ? formatStack(thrown) : formatReceived('Received value: ', thrown, 'value')) @@ -242,22 +276,27 @@ const toThrowExpectedObject = ( '\n\n' + (thrown === null ? // eslint-disable-next-line prefer-template - formatExpected('Expected message: ', expected.message) + + formatExpected( + `Expected ${messageAndCause(expected)}: `, + expectedMessageAndCause(expected), + ) + '\n' + DID_NOT_THROW : thrown.hasMessage ? // eslint-disable-next-line prefer-template printDiffOrStringify( - expected.message, - thrown.message, - 'Expected message', - 'Received message', + createMessageAndCause(expected), + createMessageAndCause(thrown.value), + `Expected ${messageAndCause(expected)}`, + `Received ${messageAndCause(thrown.value)}`, true, ) + '\n' + formatStack(thrown) - : formatExpected('Expected message: ', expected.message) + - formatReceived('Received value: ', thrown, 'value')); + : formatExpected( + `Expected ${messageAndCause}: `, + expectedMessageAndCause, + ) + formatReceived('Received value: ', thrown, 'value')); return {message, pass}; }; diff --git a/packages/expect/tsconfig.json b/packages/expect/tsconfig.json index 330ff26d00c9..c4e6edb2c0b2 100644 --- a/packages/expect/tsconfig.json +++ b/packages/expect/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "lib": ["es2020", "dom"], + "lib": ["es2022", "dom"], "rootDir": "src", "outDir": "build" }, From b84793c2b2922408f9fd1b182c831719a34c21cd Mon Sep 17 00:00:00 2001 From: ibuibu Date: Sat, 12 Nov 2022 21:46:47 +0900 Subject: [PATCH 02/16] Add tests --- .../src/__tests__/toThrowMatchers.test.ts | 31 +++++++++++++++++++ packages/expect/src/__tests__/tsconfig.json | 3 ++ 2 files changed, 34 insertions(+) diff --git a/packages/expect/src/__tests__/toThrowMatchers.test.ts b/packages/expect/src/__tests__/toThrowMatchers.test.ts index ff0770008b93..f3c20c41a48e 100644 --- a/packages/expect/src/__tests__/toThrowMatchers.test.ts +++ b/packages/expect/src/__tests__/toThrowMatchers.test.ts @@ -278,6 +278,37 @@ describe.each(['toThrowError', 'toThrow'] as const)('%s', toThrow => { }); }); + describe('error message and cause', () => { + const errorA = new Error('A'); + const errorB = new Error('B', {cause: errorA}); + const expected = new Error('good', {cause: errorB}); + + describe('pass', () => { + test('isNot false', () => { + jestExpect(() => { + throw new Error('good', {cause: errorB}); + })[toThrow](expected); + }); + + test('isNot true, incorrect message', () => { + jestExpect(() => { + throw new Error('bad', {cause: errorB}); + }).not[toThrow](expected); + }); + + test('isNot true, incorrect cause', () => { + // less than v16 does not yet support Error.cause + if (Number(process.version.split('.')[0].slice(1)) < 16) { + expect(true).toBe(true); + } else { + jestExpect(() => { + throw new Error('good', {cause: errorA}); + }).not[toThrow](expected); + } + }); + }); + }); + describe('asymmetric', () => { describe('any-Class', () => { describe('pass', () => { diff --git a/packages/expect/src/__tests__/tsconfig.json b/packages/expect/src/__tests__/tsconfig.json index dd1bca103251..a13f31174db4 100644 --- a/packages/expect/src/__tests__/tsconfig.json +++ b/packages/expect/src/__tests__/tsconfig.json @@ -1,5 +1,8 @@ { "extends": "../../../../tsconfig.test.json", + "compilerOptions": { + "lib": ["es2022"] + }, "include": ["./**/*"], "references": [{"path": "../../"}] } From f69694340f8877f126ab4761ceecdb5e4930a14b Mon Sep 17 00:00:00 2001 From: ibuibu Date: Mon, 14 Nov 2022 20:24:53 +0900 Subject: [PATCH 03/16] change es2022 to es2022.error of tsconfig for test --- packages/expect/src/__tests__/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/expect/src/__tests__/tsconfig.json b/packages/expect/src/__tests__/tsconfig.json index a13f31174db4..484f94ef0562 100644 --- a/packages/expect/src/__tests__/tsconfig.json +++ b/packages/expect/src/__tests__/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../../../tsconfig.test.json", "compilerOptions": { - "lib": ["es2022"] + "lib": ["es2022.error"] }, "include": ["./**/*"], "references": [{"path": "../../"}] From 8e3479937ee65bc333220b17394380d902d463ea Mon Sep 17 00:00:00 2001 From: ibuibu Date: Mon, 14 Nov 2022 20:29:19 +0900 Subject: [PATCH 04/16] Fix bug --- packages/expect/src/toThrowMatchers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/expect/src/toThrowMatchers.ts b/packages/expect/src/toThrowMatchers.ts index bf9d0eaec579..d5f21918d59f 100644 --- a/packages/expect/src/toThrowMatchers.ts +++ b/packages/expect/src/toThrowMatchers.ts @@ -294,8 +294,8 @@ const toThrowExpectedObject = ( '\n' + formatStack(thrown) : formatExpected( - `Expected ${messageAndCause}: `, - expectedMessageAndCause, + `Expected ${messageAndCause(expected)}: `, + expectedMessageAndCause(expected), ) + formatReceived('Received value: ', thrown, 'value')); return {message, pass}; From 1d84bd40436698c70049abcc49e7b56584a12a9c Mon Sep 17 00:00:00 2001 From: ibuibu Date: Mon, 14 Nov 2022 20:54:59 +0900 Subject: [PATCH 05/16] Change helpers position --- packages/expect/src/toThrowMatchers.ts | 56 +++++++++++++------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/packages/expect/src/toThrowMatchers.ts b/packages/expect/src/toThrowMatchers.ts index d5f21918d59f..13eece459b5c 100644 --- a/packages/expect/src/toThrowMatchers.ts +++ b/packages/expect/src/toThrowMatchers.ts @@ -225,34 +225,6 @@ const toThrowExpectedObject = ( thrown: Thrown | null, expected: Error, ): SyncExpectationResult => { - function createMessageAndCause(error: Error): string { - if (error.cause instanceof Error) { - return _createMessageAndCause(error); - } else { - return error.message; - } - } - - function _createMessageAndCause(error: Error): string { - if (error.cause instanceof Error) { - return `{ message: ${error.message}, cause: ${_createMessageAndCause( - error.cause, - )}}`; - } else { - return `{ message: ${error.message} }`; - } - } - - function expectedMessageAndCause(error: Error) { - return error.cause === undefined - ? error.message - : createMessageAndCause(error); - } - - function messageAndCause(error: Error) { - return error.cause === undefined ? 'message' : 'message and cause'; - } - const pass = thrown !== null && thrown.message === expected.message && @@ -486,4 +458,32 @@ const formatStack = (thrown: Thrown | null) => }, ); +const _createMessageAndCause = (error: Error): string => { + if (error.cause instanceof Error) { + return `{ message: ${error.message}, cause: ${_createMessageAndCause( + error.cause, + )}}`; + } else { + return `{ message: ${error.message} }`; + } +}; + +const createMessageAndCause = (error: Error): string => { + if (error.cause instanceof Error) { + return _createMessageAndCause(error); + } else { + return error.message; + } +}; + +const expectedMessageAndCause = (error: Error) => { + return error.cause === undefined + ? error.message + : createMessageAndCause(error); +}; + +const messageAndCause = (error: Error) => { + return error.cause === undefined ? 'message' : 'message and cause'; +}; + export default matchers; From 95e9c6bf7737a2438d40cc12f7d9e2c0c35c83b7 Mon Sep 17 00:00:00 2001 From: ibuibu Date: Mon, 14 Nov 2022 20:55:47 +0900 Subject: [PATCH 06/16] Remove unnecessary helper --- packages/expect/src/toThrowMatchers.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/expect/src/toThrowMatchers.ts b/packages/expect/src/toThrowMatchers.ts index 13eece459b5c..40713dc35712 100644 --- a/packages/expect/src/toThrowMatchers.ts +++ b/packages/expect/src/toThrowMatchers.ts @@ -237,7 +237,7 @@ const toThrowExpectedObject = ( '\n\n' + formatExpected( `Expected ${messageAndCause(expected)}: not `, - expectedMessageAndCause(expected), + createMessageAndCause(expected), ) + (thrown !== null && thrown.hasMessage ? formatStack(thrown) @@ -250,7 +250,7 @@ const toThrowExpectedObject = ( ? // eslint-disable-next-line prefer-template formatExpected( `Expected ${messageAndCause(expected)}: `, - expectedMessageAndCause(expected), + createMessageAndCause(expected), ) + '\n' + DID_NOT_THROW @@ -267,7 +267,7 @@ const toThrowExpectedObject = ( formatStack(thrown) : formatExpected( `Expected ${messageAndCause(expected)}: `, - expectedMessageAndCause(expected), + createMessageAndCause(expected), ) + formatReceived('Received value: ', thrown, 'value')); return {message, pass}; @@ -476,12 +476,6 @@ const createMessageAndCause = (error: Error): string => { } }; -const expectedMessageAndCause = (error: Error) => { - return error.cause === undefined - ? error.message - : createMessageAndCause(error); -}; - const messageAndCause = (error: Error) => { return error.cause === undefined ? 'message' : 'message and cause'; }; From def8b8641569d689b0b69ba41646fcc1c1ac44cf Mon Sep 17 00:00:00 2001 From: ibuibu Date: Mon, 14 Nov 2022 20:57:16 +0900 Subject: [PATCH 07/16] Remove unnecessary return type --- packages/expect/src/toThrowMatchers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/expect/src/toThrowMatchers.ts b/packages/expect/src/toThrowMatchers.ts index 40713dc35712..1b40973d2ef3 100644 --- a/packages/expect/src/toThrowMatchers.ts +++ b/packages/expect/src/toThrowMatchers.ts @@ -468,7 +468,7 @@ const _createMessageAndCause = (error: Error): string => { } }; -const createMessageAndCause = (error: Error): string => { +const createMessageAndCause = (error: Error) => { if (error.cause instanceof Error) { return _createMessageAndCause(error); } else { From a522080e09f431c32e84e47a18e65e4f7ccaa943 Mon Sep 17 00:00:00 2001 From: ibuibu Date: Sun, 20 Nov 2022 22:35:34 +0900 Subject: [PATCH 08/16] Update tsconfig --- packages/expect/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/expect/tsconfig.json b/packages/expect/tsconfig.json index c4e6edb2c0b2..75c8686f6b59 100644 --- a/packages/expect/tsconfig.json +++ b/packages/expect/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "lib": ["es2022", "dom"], + "lib": ["es2020", "es2021.promise", "es2022.error", "dom"], "rootDir": "src", "outDir": "build" }, From 613425bc688e96ffc1114f87c12f3056be3acecd Mon Sep 17 00:00:00 2001 From: ibuibu Date: Sun, 20 Nov 2022 22:35:52 +0900 Subject: [PATCH 09/16] Add fail tests --- .../src/__tests__/toThrowMatchers.test.ts | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/expect/src/__tests__/toThrowMatchers.test.ts b/packages/expect/src/__tests__/toThrowMatchers.test.ts index f3c20c41a48e..6d7435b7ca11 100644 --- a/packages/expect/src/__tests__/toThrowMatchers.test.ts +++ b/packages/expect/src/__tests__/toThrowMatchers.test.ts @@ -297,16 +297,39 @@ describe.each(['toThrowError', 'toThrow'] as const)('%s', toThrow => { }); test('isNot true, incorrect cause', () => { - // less than v16 does not yet support Error.cause - if (Number(process.version.split('.')[0].slice(1)) < 16) { - expect(true).toBe(true); - } else { + // only v16 or higher support Error.cause + if (Number(process.version.split('.')[0].slice(1)) >= 16) { jestExpect(() => { throw new Error('good', {cause: errorA}); }).not[toThrow](expected); } }); }); + + describe('fail', () => { + // only v16 or higher support Error.cause + if (Number(process.version.split('.')[0].slice(1)) >= 16) { + test('isNot false, incorrect message', () => { + expect(() => + jestExpect(() => { + throw new Error('bad', {cause: errorB}); + })[toThrow](expected), + ).toThrow( + /^(?=.*Expected message and cause: ).*Received message and cause: /s, + ); + }); + + test('isNot true, incorrect cause', () => { + expect(() => + jestExpect(() => { + throw new Error('good', {cause: errorA}); + })[toThrow](expected), + ).toThrow( + /^(?=.*Expected message and cause: ).*Received message and cause: /s, + ); + }); + } + }); }); describe('asymmetric', () => { From 1e650964daa789a70f936f47bcf56bf4664c3ee8 Mon Sep 17 00:00:00 2001 From: ibuibu Date: Wed, 15 Feb 2023 16:06:43 +0900 Subject: [PATCH 10/16] Combine functions --- packages/expect/src/toThrowMatchers.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/expect/src/toThrowMatchers.ts b/packages/expect/src/toThrowMatchers.ts index 1b40973d2ef3..98dbde6163a8 100644 --- a/packages/expect/src/toThrowMatchers.ts +++ b/packages/expect/src/toThrowMatchers.ts @@ -458,17 +458,17 @@ const formatStack = (thrown: Thrown | null) => }, ); -const _createMessageAndCause = (error: Error): string => { - if (error.cause instanceof Error) { - return `{ message: ${error.message}, cause: ${_createMessageAndCause( - error.cause, - )}}`; - } else { - return `{ message: ${error.message} }`; - } -}; - const createMessageAndCause = (error: Error) => { + const _createMessageAndCause = (error: Error): string => { + if (error.cause instanceof Error) { + return `{ message: ${error.message}, cause: ${_createMessageAndCause( + error.cause, + )}}`; + } else { + return `{ message: ${error.message} }`; + } + }; + if (error.cause instanceof Error) { return _createMessageAndCause(error); } else { From 9d57a89c47601ee2a76f30b31ffe920a6a74ebd4 Mon Sep 17 00:00:00 2001 From: ibuibu Date: Wed, 15 Feb 2023 16:07:24 +0900 Subject: [PATCH 11/16] Use test helper --- .../expect/src/__tests__/toThrowMatchers.test.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/expect/src/__tests__/toThrowMatchers.test.ts b/packages/expect/src/__tests__/toThrowMatchers.test.ts index 6d7435b7ca11..1ec1fcb5a489 100644 --- a/packages/expect/src/__tests__/toThrowMatchers.test.ts +++ b/packages/expect/src/__tests__/toThrowMatchers.test.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {alignedAnsiStyleSerializer} from '@jest/test-utils'; +import {alignedAnsiStyleSerializer, onNodeVersions} from '@jest/test-utils'; import jestExpect from '../'; expect.addSnapshotSerializer(alignedAnsiStyleSerializer); @@ -296,19 +296,17 @@ describe.each(['toThrowError', 'toThrow'] as const)('%s', toThrow => { }).not[toThrow](expected); }); - test('isNot true, incorrect cause', () => { - // only v16 or higher support Error.cause - if (Number(process.version.split('.')[0].slice(1)) >= 16) { + onNodeVersions('>=16.9.0', () => { + test('isNot true, incorrect cause', () => { jestExpect(() => { throw new Error('good', {cause: errorA}); }).not[toThrow](expected); - } + }); }); }); describe('fail', () => { - // only v16 or higher support Error.cause - if (Number(process.version.split('.')[0].slice(1)) >= 16) { + onNodeVersions('>=16.9.0', () => { test('isNot false, incorrect message', () => { expect(() => jestExpect(() => { @@ -328,7 +326,7 @@ describe.each(['toThrowError', 'toThrow'] as const)('%s', toThrow => { /^(?=.*Expected message and cause: ).*Received message and cause: /s, ); }); - } + }); }); }); From d359f8025eea8584fbe0f145b671a8a0ab1ecb64 Mon Sep 17 00:00:00 2001 From: ibuibu Date: Wed, 15 Feb 2023 16:19:38 +0900 Subject: [PATCH 12/16] Merge Changelog --- CHANGELOG.md | 123 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 108 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 494e36427afb..ce86f3d161fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,16 +2,109 @@ ### Features +- `[jest-message-util]` Add support for [error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) + ### Fixes -- `[jest-mock]` Treat cjs modules as objects so they can be mocked ([#13513](https://github.com/facebook/jest/pull/13513)) +- `[jest-mock]` Clear mock state when `jest.restoreAllMocks()` is called ([#13867](https://github.com/facebook/jest/pull/13867)) +- `[jest-mock]` Prevent `mockImplementationOnce` and `mockReturnValueOnce` bleeding into `withImplementation` ([#13888](https://github.com/facebook/jest/pull/13888)) ### Chore & Maintenance -- `[@jest/transform]` Update `convert-source-map` ([#13509](https://github.com/facebook/jest/pull/13509)) +### Performance + +## 29.4.2 + +### Features + +- `[@jest/core]` Instrument significant lifecycle events with [`performance.mark()`](https://nodejs.org/docs/latest-v16.x/api/perf_hooks.html#performancemarkname-options) ([#13859](https://github.com/facebook/jest/pull/13859)) + +### Fixes + +- `[expect, @jest/expect]` Provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions ([#13848](https://github.com/facebook/jest/pull/13848)) +- `[jest-circus]` Added explicit mention of test failing because `done()` is not being called in error message ([#13847](https://github.com/facebook/jest/pull/13847)) +- `[jest-runtime]` Handle CJS re-exports of node core modules from ESM ([#13856](https://github.com/facebook/jest/pull/13856)) +- `[jest-transform]` Downgrade `write-file-atomic` to v4 ([#13853](https://github.com/facebook/jest/pull/13853)) +- `[jest-worker]` Ignore IPC messages not intended for Jest ([#13543](https://github.com/facebook/jest/pull/13543)) + +### Chore & Maintenance + +- `[*]` make sure to exclude `.eslintcache` from published module ([#13832](https://github.com/facebook/jest/pull/13832)) +- `[docs]` Cleanup incorrect links in CHANGELOG.md ([#13857](https://github.com/facebook/jest/pull/13857)) + +## 29.4.1 + +### Features + +- `[expect, jest-circus, @jest/types]` Implement `numPassingAsserts` of testResults to track the number of passing asserts in a test ([#13795](https://github.com/facebook/jest/pull/13795)) +- `[jest-core]` Add newlines to JSON output ([#13817](https://github.com/facebook/jest/pull/13817)) +- `[@jest/reporters]` Automatic log folding in GitHub Actions Reporter ([#13626](https://github.com/facebook/jest/pull/13626)) + +### Fixes + +- `[@jest/expect-utils]` `toMatchObject` diffs should include `Symbol` properties ([#13810](https://github.com/facebook/jest/pull/13810)) +- `[jest-runtime]` Handle missing `replaceProperty` ([#13823](https://github.com/facebook/jest/pull/13823)) +- `[@jest/types]` Add partial support for `done` callbacks in typings of `each` ([#13756](https://github.com/facebook/jest/pull/13756)) + +## 29.4.0 + +### Features + +- `[expect, @jest/expect-utils]` Support custom equality testers ([#13654](https://github.com/facebook/jest/pull/13654)) +- `[jest-config, jest-worker]` Use `os.availableParallelism` if available to calculate number of workers to spawn ([#13738](https://github.com/facebook/jest/pull/13738)) +- `[@jest/globals, jest-mock]` Add `jest.replaceProperty()` that replaces property value ([#13496](https://github.com/facebook/jest/pull/13496)) +- `[jest-haste-map]` ignore Sapling vcs directories (`.sl/`) ([#13674](https://github.com/facebook/jest/pull/13674)) +- `[jest-resolve]` Support subpath imports ([#13705](https://github.com/facebook/jest/pull/13705), [#13723](https://github.com/facebook/jest/pull/13723), [#13777](https://github.com/facebook/jest/pull/13777)) +- `[jest-runtime]` Add `jest.isolateModulesAsync` for scoped module initialization of asynchronous functions ([#13680](https://github.com/facebook/jest/pull/13680)) +- `[jest-runtime]` Add `jest.isEnvironmentTornDown` function ([#13741](https://github.com/facebook/jest/pull/13741)) +- `[jest-test-result]` Added `skipped` and `focused` status to `FormattedTestResult` ([#13700](https://github.com/facebook/jest/pull/13700)) +- `[jest-transform]` Support for asynchronous `createTransformer` ([#13762](https://github.com/facebook/jest/pull/13762)) + +### Fixes + +- `[jest-environment-node]` Fix non-configurable globals ([#13687](https://github.com/facebook/jest/pull/13687)) +- `[@jest/expect-utils]` `toMatchObject` should handle `Symbol` properties ([#13639](https://github.com/facebook/jest/pull/13639)) +- `[jest-mock]` Fix `mockReset` and `resetAllMocks` `undefined` return value([#13692](https://github.com/facebook/jest/pull/13692)) +- `[jest-resolve]` Add global paths to `require.resolve.paths` ([#13633](https://github.com/facebook/jest/pull/13633)) +- `[jest-resolve]` Correct node core module detection when using `node:` specifiers ([#13806](https://github.com/facebook/jest/pull/13806)) +- `[jest-runtime]` Support WASM files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608)) +- `[jest-runtime]` Use the `scriptTransformer` cache in `jest-runner` ([#13735](https://github.com/facebook/jest/pull/13735)) +- `[jest-runtime]` Enforce import assertions when importing JSON in ESM ([#12755](https://github.com/facebook/jest/pull/12755) & [#13805](https://github.com/facebook/jest/pull/13805)) +- `[jest-snapshot]` Make sure to import `babel` outside of the sandbox ([#13694](https://github.com/facebook/jest/pull/13694)) +- `[jest-transform]` Ensure the correct configuration is passed to preprocessors specified multiple times in the `transform` option ([#13770](https://github.com/facebook/jest/pull/13770)) + +### Chore & Maintenance + +- `[@jest/fake-timers]` Update `@sinonjs/fake-timers` ([#13612](https://github.com/facebook/jest/pull/13612)) +- `[docs]` Improve custom puppeteer example to prevent worker warnings ([#13619](https://github.com/facebook/jest/pull/13619)) + +## 29.3.1 + +### Fixes + +- `[jest-config]` Do not warn about `preset` in `ProjectConfig` ([#13583](https://github.com/facebook/jest/pull/13583)) ### Performance +- `[jest-transform]` Defer creation of cache directory ([#13420](https://github.com/facebook/jest/pull/13420)) + +## 29.3.0 + +### Features + +- `[jest-runtime]` Support WebAssembly (Wasm) imports in ESM modules ([#13505](https://github.com/facebook/jest/pull/13505)) + +### Fixes + +- `[jest-config]` Add config validation for `projects` option ([#13565](https://github.com/facebook/jest/pull/13565)) +- `[jest-mock]` Treat cjs modules as objects so they can be mocked ([#13513](https://github.com/facebook/jest/pull/13513)) +- `[jest-worker]` Throw an error instead of hanging when jest workers terminate unexpectedly ([#13566](https://github.com/facebook/jest/pull/13566)) + +### Chore & Maintenance + +- `[@jest/transform]` Update `convert-source-map` ([#13509](https://github.com/facebook/jest/pull/13509)) +- `[docs]` Mention `toStrictEqual` in UsingMatchers docs. ([#13560](https://github.com/facebook/jest/pull/13560)) + ## 29.2.2 ### Fixes @@ -219,7 +312,7 @@ ### Fixes -- `[@jest/expect-utils]` Fix deep equality of ImmutableJS OrderedMaps ([#12763](https://github.com/facebook/jest/pull/12899)) +- `[@jest/expect-utils]` Fix deep equality of ImmutableJS OrderedMaps ([#12899](https://github.com/facebook/jest/pull/12899)) - `[jest-docblock]` Handle multiline comments in parseWithComments ([#12845](https://github.com/facebook/jest/pull/12845)) - `[jest-mock]` Improve `spyOn` error messages ([#12901](https://github.com/facebook/jest/pull/12901)) - `[jest-runtime]` Correctly report V8 coverage with `resetModules: true` ([#12912](https://github.com/facebook/jest/pull/12912)) @@ -697,7 +790,7 @@ ### Features -- `[@jest/fake-timers]` Flush callbacks scheduled with `requestAnimationFrame` every 16ms when using legacy timers. ([#11523](https://github.com/facebook/jest/pull/11567)) +- `[@jest/fake-timers]` Flush callbacks scheduled with `requestAnimationFrame` every 16ms when using legacy timers. ([#11567](https://github.com/facebook/jest/pull/11567)) - `[pretty-format]` Use `globalThis` (with polyfill if required) to bring support for esbuild's browser bundling mode ([#11569](https://github.com/facebook/jest/pull/11569)) ### Fixes @@ -784,7 +877,7 @@ - `[jest-runtime]` Detect reexports from CJS as named exports in ESM ([#10988](https://github.com/facebook/jest/pull/10988)) - `[jest-runtime]` Support for async code transformations ([#11191](https://github.com/facebook/jest/pull/11191) & [#11220](https://github.com/facebook/jest/pull/11220)) - `[jest-snapshot]` [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792) & [#11192](https://github.com/facebook/jest/pull/11192)) -- `[jest-snapshot]` [**BREAKING**] Run transforms over `snapshotResolver` ([#8751](https://github.com/facebook/jest/pull/8829)) +- `[jest-snapshot]` [**BREAKING**] Run transforms over `snapshotResolver` ([#8829](https://github.com/facebook/jest/pull/8829)) - `[jest-transform]` Pass config options defined in Jest's config to transformer's `process` and `getCacheKey` functions ([#10926](https://github.com/facebook/jest/pull/10926)) - `[jest-transform]` Add support for transformers written in ESM ([#11163](https://github.com/facebook/jest/pull/11163)) - `[jest-transform]` [**BREAKING**] Do not export `ScriptTransformer` class, instead export the async function `createScriptTransformer` ([#11163](https://github.com/facebook/jest/pull/11163)) @@ -1032,7 +1125,7 @@ - `[jest-core]` Don't report ELDHistogram as open handle ([#10417](https://github.com/facebook/jest/pull/10417)) - `[jest-matcher-utils]` Fix diffing object contain readonly symbol key object ([#10414](https://github.com/facebook/jest/pull/10414)) -- `[jest-reporters]` Fixes notify reporter on Linux (using notify-send) ([#10393](https://github.com/facebook/jest/pull/10400)) +- `[jest-reporters]` Fixes notify reporter on Linux (using notify-send) ([#10400](https://github.com/facebook/jest/pull/10400)) - `[jest-snapshot]` Correctly handles arrays and property matchers in snapshots ([#10404](https://github.com/facebook/jest/pull/10404)) ## 26.4.0 @@ -1081,7 +1174,7 @@ - `[jest-haste-map]` Watchman crawler now includes dotfiles ([#10075](https://github.com/facebook/jest/pull/10075)) - `[jest-worker]` Added support for workers to send custom messages to parent in jest-worker ([#10293](https://github.com/facebook/jest/pull/10293)) - `[jest-worker]` Support passing `resourceLimits` ([#10335](https://github.com/facebook/jest/pull/10335)) -- `[pretty-format]` Added support for serializing custom elements (web components) ([#10217](https://github.com/facebook/jest/pull/10237)) +- `[pretty-format]` Added support for serializing custom elements (web components) ([#10237](https://github.com/facebook/jest/pull/10237)) ### Fixes @@ -1498,7 +1591,7 @@ - `[expect]` Display equal values for ReturnedWith similar to CalledWith ([#8791](https://github.com/facebook/jest/pull/8791)) - `[expect, jest-snapshot]` Change color from green for some args in matcher hints ([#8812](https://github.com/facebook/jest/pull/8812)) - `[jest-snapshot]` Highlight substring differences when matcher fails, part 3 ([#8569](https://github.com/facebook/jest/pull/8569)) -- `[jest-core]` Improve report when snapshots are obsolete ([#8448](https://github.com/facebook/jest/pull/8665)) +- `[jest-core]` Improve report when snapshots are obsolete ([#8665](https://github.com/facebook/jest/pull/8665)) - `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454)) - `[*]` Manage the global timeout with `--testTimeout` command line argument. ([#8456](https://github.com/facebook/jest/pull/8456)) - `[pretty-format]` Render custom displayName of memoized components ([#8546](https://github.com/facebook/jest/pull/8546)) @@ -1787,7 +1880,7 @@ We skipped 24.2.0 because a draft was accidentally published. Please use `24.3.0 - `[jest-docblock]`: Migrate to TypeScript ([#7836](https://github.com/facebook/jest/pull/7836)) - `[jest-each]`: Migrate to Typescript ([#8007](https://github.com/facebook/jest/pull/8007)) - `[jest-each]`: Refactor into multiple files with better types ([#8018](https://github.com/facebook/jest/pull/8018)) -- `[jest-environment-jsdom]`: Migrate to TypeScript ([#7985](https://github.com/facebook/jest/pull/8003)) +- `[jest-environment-jsdom]`: Migrate to TypeScript ([#8003](https://github.com/facebook/jest/pull/8003)) - `[jest-environment-node]`: Migrate to TypeScript ([#7985](https://github.com/facebook/jest/pull/7985)) - `[jest-get-type]`: Migrate to TypeScript ([#7818](https://github.com/facebook/jest/pull/7818)) - `[jest-haste-map]`: Migrate to TypeScript ([#7854](https://github.com/facebook/jest/pull/7854), [#7951](https://github.com/facebook/jest/pull/7951)) @@ -1810,7 +1903,7 @@ We skipped 24.2.0 because a draft was accidentally published. Please use `24.3.0 - `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843)) - `[jest-worker]`: Migrate to TypeScript ([#7853](https://github.com/facebook/jest/pull/7853)) - `[jest]`: Migrate to TypeScript ([#8024](https://github.com/facebook/jest/pull/8024)) -- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809), [#7809](https://github.com/facebook/jest/pull/7972)) +- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809), [#7972](https://github.com/facebook/jest/pull/7972)) ### Performance @@ -2104,7 +2197,7 @@ We skipped 24.2.0 because a draft was accidentally published. Please use `24.3.0 - `[jest-jasmine2]` Use prettier through `require` instead of `localRequire`. Fixes `matchInlineSnapshot` where prettier dependencies like `path` and `fs` are mocked with `jest.mock`. ([#6776](https://github.com/facebook/jest/pull/6776)) - `[docs]` Fix contributors link ([#6711](https://github.com/facebook/jest/pull/6711)) - `[website]` Fix website versions page to link to correct language ([#6734](https://github.com/facebook/jest/pull/6734)) -- `[expect]` Update `toContain` suggestion to contain equal message ([#6792](https://github.com/facebook/jest/pull/6810)) +- `[expect]` Update `toContain` suggestion to contain equal message ([#6810](https://github.com/facebook/jest/pull/6810)) ## 23.4.1 @@ -2245,7 +2338,7 @@ We skipped 24.2.0 because a draft was accidentally published. Please use `24.3.0 - `[jest-runtime]` Prevent modules from marking themselves as their own parent ([#5235](https://github.com/facebook/jest/issues/5235)) - `[jest-mock]` Add support for auto-mocking generator functions ([#5983](https://github.com/facebook/jest/pull/5983)) - `[expect]` Add support for async matchers ([#5919](https://github.com/facebook/jest/pull/5919)) -- `[expect]` Suggest toContainEqual ([#5948](https://github.com/facebook/jest/pull/5953)) +- `[expect]` Suggest toContainEqual ([#5953](https://github.com/facebook/jest/pull/5953)) - `[jest-config]` Export Jest's default options ([#5948](https://github.com/facebook/jest/pull/5948)) - `[jest-editor-support]` Move `coverage` to `ProjectWorkspace.collectCoverage` ([#5929](https://github.com/facebook/jest/pull/5929)) - `[jest-editor-support]` Add `coverage` option to runner ([#5836](https://github.com/facebook/jest/pull/5836)) @@ -2323,7 +2416,7 @@ We skipped 24.2.0 because a draft was accidentally published. Please use `24.3.0 - `[docs]` Add explanation on how to mock methods not implemented in JSDOM - `[jest-jasmine2]` Simplify `Env.execute` and TreeProcessor to setup and clean resources for the top suite the same way as for all of the children suites ([#5885](https://github.com/facebook/jest/pull/5885)) - `[babel-jest]` [**BREAKING**] Always return object from transformer ([#5991](https://github.com/facebook/jest/pull/5991)) -- `[*]` Run Prettier on compiled output ([#5858](https://github.com/facebook/jest/pull/3497)) +- `[*]` Run Prettier on compiled output ([#5858](https://github.com/facebook/jest/pull/5858)) - `[jest-cli]` Add fileChange hook for plugins ([#5708](https://github.com/facebook/jest/pull/5708)) - `[docs]` Add docs on using `jest.mock(...)` ([#5648](https://github.com/facebook/jest/pull/5648)) - `[docs]` Mention Jest Puppeteer Preset ([#5722](https://github.com/facebook/jest/pull/5722)) @@ -2409,7 +2502,7 @@ We skipped 24.2.0 because a draft was accidentally published. Please use `24.3.0 ### Fixes - `[babel-jest]` Revert "Remove retainLines from babel-jest" ([#5496](https://github.com/facebook/jest/pull/5496)) -- `[jest-docblock]` Support multiple of the same `@pragma`. ([#5154](https://github.com/facebook/jest/pull/5502)) +- `[jest-docblock]` Support multiple of the same `@pragma`. ([#5502](https://github.com/facebook/jest/pull/5502)) ### Features @@ -2593,7 +2686,7 @@ We skipped 24.2.0 because a draft was accidentally published. Please use `24.3.0 - `[pretty-format]` Fix errors when identity-obj-proxy mocks CSS Modules ([#4935](https://github.com/facebook/jest/pull/4935)) - `[babel-jest]` Fix support for namespaced babel version 7 ([#4918](https://github.com/facebook/jest/pull/4918)) - `[expect]` fix .toThrow for promises ([#4884](https://github.com/facebook/jest/pull/4884)) -- `[jest-docblock]` pragmas should preserve urls ([#4837](https://github.com/facebook/jest/pull/4629)) +- `[jest-docblock]` pragmas should preserve urls ([#4837](https://github.com/facebook/jest/pull/4837)) - `[jest-cli]` Check if `npm_lifecycle_script` calls Jest directly ([#4629](https://github.com/facebook/jest/pull/4629)) - `[jest-cli]` Fix --showConfig to show all configs ([#4494](https://github.com/facebook/jest/pull/4494)) - `[jest-cli]` Throw if `maxWorkers` doesn't have a value ([#4591](https://github.com/facebook/jest/pull/4591)) From 0f3282c30486745858232443186d151e944a8fb6 Mon Sep 17 00:00:00 2001 From: ibuibu Date: Wed, 15 Feb 2023 16:19:56 +0900 Subject: [PATCH 13/16] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce86f3d161fa..6feb09a2b5d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[jest-message-util]` Add support for [error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) +- `[expect]` Update `toThrow()` to be able to use [error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) ### Fixes From db115e82bc059ac8c080319d602c632250bff325 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 15 Feb 2023 11:13:06 +0100 Subject: [PATCH 14/16] extract function declaration --- packages/expect/src/toThrowMatchers.ts | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/expect/src/toThrowMatchers.ts b/packages/expect/src/toThrowMatchers.ts index a41b2593c3e3..7ac9b752e7c1 100644 --- a/packages/expect/src/toThrowMatchers.ts +++ b/packages/expect/src/toThrowMatchers.ts @@ -458,26 +458,26 @@ const formatStack = (thrown: Thrown | null) => }, ); -const createMessageAndCause = (error: Error) => { - const _createMessageAndCause = (error: Error): string => { - if (error.cause instanceof Error) { - return `{ message: ${error.message}, cause: ${_createMessageAndCause( - error.cause, - )}}`; - } else { - return `{ message: ${error.message} }`; - } - }; +function createMessageAndCauseMessage(error: Error): string { + if (error.cause instanceof Error) { + return `{ message: ${error.message}, cause: ${createMessageAndCauseMessage( + error.cause, + )}}`; + } else { + return `{ message: ${error.message} }`; + } +} +function createMessageAndCause(error: Error) { if (error.cause instanceof Error) { - return _createMessageAndCause(error); + return createMessageAndCauseMessage(error); } else { return error.message; } -}; +} -const messageAndCause = (error: Error) => { +function messageAndCause(error: Error) { return error.cause === undefined ? 'message' : 'message and cause'; -}; +} export default matchers; From de64948c2f7bda23c0240255d837d18cebe6b1de Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 15 Feb 2023 11:13:53 +0100 Subject: [PATCH 15/16] else-return --- packages/expect/src/toThrowMatchers.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/expect/src/toThrowMatchers.ts b/packages/expect/src/toThrowMatchers.ts index 7ac9b752e7c1..84c5972c7967 100644 --- a/packages/expect/src/toThrowMatchers.ts +++ b/packages/expect/src/toThrowMatchers.ts @@ -463,17 +463,17 @@ function createMessageAndCauseMessage(error: Error): string { return `{ message: ${error.message}, cause: ${createMessageAndCauseMessage( error.cause, )}}`; - } else { - return `{ message: ${error.message} }`; } + + return `{ message: ${error.message} }`; } function createMessageAndCause(error: Error) { if (error.cause instanceof Error) { return createMessageAndCauseMessage(error); - } else { - return error.message; } + + return error.message; } function messageAndCause(error: Error) { From c0328f933e92f73f3a7aad49961f7eec3f5e8227 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 15 Feb 2023 11:17:22 +0100 Subject: [PATCH 16/16] reuse function call --- packages/expect/src/toThrowMatchers.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/expect/src/toThrowMatchers.ts b/packages/expect/src/toThrowMatchers.ts index 84c5972c7967..77eff9008e8b 100644 --- a/packages/expect/src/toThrowMatchers.ts +++ b/packages/expect/src/toThrowMatchers.ts @@ -225,10 +225,13 @@ const toThrowExpectedObject = ( thrown: Thrown | null, expected: Error, ): SyncExpectationResult => { + const expectedMessageAndCause = createMessageAndCause(expected); + const thrownMessageAndCause = + thrown !== null ? createMessageAndCause(thrown.value) : null; const pass = thrown !== null && thrown.message === expected.message && - createMessageAndCause(thrown.value) === createMessageAndCause(expected); + thrownMessageAndCause === expectedMessageAndCause; const message = pass ? () => @@ -237,7 +240,7 @@ const toThrowExpectedObject = ( '\n\n' + formatExpected( `Expected ${messageAndCause(expected)}: not `, - createMessageAndCause(expected), + expectedMessageAndCause, ) + (thrown !== null && thrown.hasMessage ? formatStack(thrown) @@ -250,15 +253,15 @@ const toThrowExpectedObject = ( ? // eslint-disable-next-line prefer-template formatExpected( `Expected ${messageAndCause(expected)}: `, - createMessageAndCause(expected), + expectedMessageAndCause, ) + '\n' + DID_NOT_THROW : thrown.hasMessage ? // eslint-disable-next-line prefer-template printDiffOrStringify( - createMessageAndCause(expected), - createMessageAndCause(thrown.value), + expectedMessageAndCause, + thrownMessageAndCause, `Expected ${messageAndCause(expected)}`, `Received ${messageAndCause(thrown.value)}`, true, @@ -267,7 +270,7 @@ const toThrowExpectedObject = ( formatStack(thrown) : formatExpected( `Expected ${messageAndCause(expected)}: `, - createMessageAndCause(expected), + expectedMessageAndCause, ) + formatReceived('Received value: ', thrown, 'value')); return {message, pass};