From f8b18313bc97d8082b3562c9961e05e080f40bc8 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 28 Apr 2022 10:30:12 +0300 Subject: [PATCH 1/4] fix(@jest/transform): InvalidReturnValueError --- .../jest-transform/src/ScriptTransformer.ts | 4 +- .../ScriptTransformer.test.ts.snap | 72 ++++++++++++------- .../src/runtimeErrorsAndWarnings.ts | 16 +++-- 3 files changed, 62 insertions(+), 30 deletions(-) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index fc674e22ebac..2af4344fe470 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -384,7 +384,9 @@ class ScriptTransformer { if (processed != null && typeof processed.code === 'string') { transformed = processed; } else { - throw new Error(makeInvalidReturnValueError()); + const transformPath = this._getTransformPath(filename); + invariant(transformPath); + throw new Error(makeInvalidReturnValueError(transformPath)); } } diff --git a/packages/jest-transform/src/__tests__/__snapshots__/ScriptTransformer.test.ts.snap b/packages/jest-transform/src/__tests__/__snapshots__/ScriptTransformer.test.ts.snap index d79e6c7ee43f..a504fe01c9fa 100644 --- a/packages/jest-transform/src/__tests__/__snapshots__/ScriptTransformer.test.ts.snap +++ b/packages/jest-transform/src/__tests__/__snapshots__/ScriptTransformer.test.ts.snap @@ -358,9 +358,12 @@ exports[`ScriptTransformer passes expected transform options to getCacheKeyAsync exports[`ScriptTransformer throws an error if \`process\` doesn't return an object containing \`code\` key with processed string 1`] = ` "● Invalid return value: - Code transformer's \`process\` method must return an object containing \`code\` key - with processed string. If \`processAsync\` method is implemented it must return - a Promise resolving to an object containing \`code\` key with processed string. + \`process()\` or/and \`processAsync()\` method of code transformer found at + "passthrough-preprocessor" + should return an object or a Promise resolving to an object. The object + must have \`code\` property with a string of processed code. + Upgrading Guide: + https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation " @@ -368,9 +371,12 @@ exports[`ScriptTransformer throws an error if \`process\` doesn't return an obje exports[`ScriptTransformer throws an error if \`process\` doesn't return an object containing \`code\` key with processed string 2`] = ` "● Invalid return value: - Code transformer's \`process\` method must return an object containing \`code\` key - with processed string. If \`processAsync\` method is implemented it must return - a Promise resolving to an object containing \`code\` key with processed string. + \`process()\` or/and \`processAsync()\` method of code transformer found at + "passthrough-preprocessor" + should return an object or a Promise resolving to an object. The object + must have \`code\` property with a string of processed code. + Upgrading Guide: + https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation " @@ -378,9 +384,12 @@ exports[`ScriptTransformer throws an error if \`process\` doesn't return an obje exports[`ScriptTransformer throws an error if \`process\` doesn't return an object containing \`code\` key with processed string 3`] = ` "● Invalid return value: - Code transformer's \`process\` method must return an object containing \`code\` key - with processed string. If \`processAsync\` method is implemented it must return - a Promise resolving to an object containing \`code\` key with processed string. + \`process()\` or/and \`processAsync()\` method of code transformer found at + "passthrough-preprocessor" + should return an object or a Promise resolving to an object. The object + must have \`code\` property with a string of processed code. + Upgrading Guide: + https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation " @@ -388,9 +397,12 @@ exports[`ScriptTransformer throws an error if \`process\` doesn't return an obje exports[`ScriptTransformer throws an error if \`process\` doesn't return an object containing \`code\` key with processed string 4`] = ` "● Invalid return value: - Code transformer's \`process\` method must return an object containing \`code\` key - with processed string. If \`processAsync\` method is implemented it must return - a Promise resolving to an object containing \`code\` key with processed string. + \`process()\` or/and \`processAsync()\` method of code transformer found at + "passthrough-preprocessor" + should return an object or a Promise resolving to an object. The object + must have \`code\` property with a string of processed code. + Upgrading Guide: + https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation " @@ -398,9 +410,12 @@ exports[`ScriptTransformer throws an error if \`process\` doesn't return an obje exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a promise of object containing \`code\` key with processed string 1`] = ` "● Invalid return value: - Code transformer's \`process\` method must return an object containing \`code\` key - with processed string. If \`processAsync\` method is implemented it must return - a Promise resolving to an object containing \`code\` key with processed string. + \`process()\` or/and \`processAsync()\` method of code transformer found at + "passthrough-preprocessor-fruits-banana-js" + should return an object or a Promise resolving to an object. The object + must have \`code\` property with a string of processed code. + Upgrading Guide: + https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation " @@ -408,9 +423,12 @@ exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a promise of object containing \`code\` key with processed string 2`] = ` "● Invalid return value: - Code transformer's \`process\` method must return an object containing \`code\` key - with processed string. If \`processAsync\` method is implemented it must return - a Promise resolving to an object containing \`code\` key with processed string. + \`process()\` or/and \`processAsync()\` method of code transformer found at + "passthrough-preprocessor-fruits-avocado-js" + should return an object or a Promise resolving to an object. The object + must have \`code\` property with a string of processed code. + Upgrading Guide: + https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation " @@ -418,9 +436,12 @@ exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a promise of object containing \`code\` key with processed string 3`] = ` "● Invalid return value: - Code transformer's \`process\` method must return an object containing \`code\` key - with processed string. If \`processAsync\` method is implemented it must return - a Promise resolving to an object containing \`code\` key with processed string. + \`process()\` or/and \`processAsync()\` method of code transformer found at + "passthrough-preprocessor-fruits-kiwi-js" + should return an object or a Promise resolving to an object. The object + must have \`code\` property with a string of processed code. + Upgrading Guide: + https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation " @@ -428,9 +449,12 @@ exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a promise of object containing \`code\` key with processed string 4`] = ` "● Invalid return value: - Code transformer's \`process\` method must return an object containing \`code\` key - with processed string. If \`processAsync\` method is implemented it must return - a Promise resolving to an object containing \`code\` key with processed string. + \`process()\` or/and \`processAsync()\` method of code transformer found at + "passthrough-preprocessor-fruits-grapefruit-js" + should return an object or a Promise resolving to an object. The object + must have \`code\` property with a string of processed code. + Upgrading Guide: + https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation " diff --git a/packages/jest-transform/src/runtimeErrorsAndWarnings.ts b/packages/jest-transform/src/runtimeErrorsAndWarnings.ts index c57292ac5b14..5b4004e16979 100644 --- a/packages/jest-transform/src/runtimeErrorsAndWarnings.ts +++ b/packages/jest-transform/src/runtimeErrorsAndWarnings.ts @@ -14,16 +14,22 @@ const DOCUMENTATION_NOTE = ` ${chalk.bold( )} https://jestjs.io/docs/code-transformation `; +const UPGRADE_NOTE = ` ${chalk.bold('Upgrading Guide:')} + https://jestjs.io/docs/upgrading-to-jest28#transformer +`; -export const makeInvalidReturnValueError = (): string => +export const makeInvalidReturnValueError = (transformPath: string): string => chalk.red( [ chalk.bold(`${BULLET}Invalid return value:`), - " Code transformer's `process` method must return an object containing `code` key ", - ' with processed string. If `processAsync` method is implemented it must return ', - ' a Promise resolving to an object containing `code` key with processed string.', + ' `process()` or/and `processAsync()` method of code transformer found at ', + ` "${slash(transformPath)}" `, + ' should return an object or a Promise resolving to an object. The object ', + ' must have `code` property with a string of processed code.', '', - ].join('\n') + DOCUMENTATION_NOTE, + ].join('\n') + + UPGRADE_NOTE + + DOCUMENTATION_NOTE, ); export const makeInvalidSourceMapWarning = ( From 33f80bf7922160517bd335a7ae5f77af2a2fdbc6 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 28 Apr 2022 10:41:26 +0300 Subject: [PATCH 2/4] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66af53abd33f..fb2e8ea61055 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `[@jest/reporters]` Fix trailing slash in matching `coverageThreshold` key ([#12714](https://github.com/facebook/jest/pull/12714)) +- `[@jest/transform]` Throw better error if an invalid return value if encountered ([#12764](https://github.com/facebook/jest/pull/12764)) ### Chore & Maintenance From 08b5981fffc8fe452179ada355ef0cf7ebc2a87b Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 28 Apr 2022 10:47:00 +0300 Subject: [PATCH 3/4] improve message --- packages/jest-transform/src/runtimeErrorsAndWarnings.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/jest-transform/src/runtimeErrorsAndWarnings.ts b/packages/jest-transform/src/runtimeErrorsAndWarnings.ts index 5b4004e16979..2431dbfc67b8 100644 --- a/packages/jest-transform/src/runtimeErrorsAndWarnings.ts +++ b/packages/jest-transform/src/runtimeErrorsAndWarnings.ts @@ -14,7 +14,9 @@ const DOCUMENTATION_NOTE = ` ${chalk.bold( )} https://jestjs.io/docs/code-transformation `; -const UPGRADE_NOTE = ` ${chalk.bold('Upgrading Guide:')} +const UPGRADE_NOTE = ` ${chalk.bold( + 'This error may be caused by a breaking change in Jest 28:', +)} https://jestjs.io/docs/upgrading-to-jest28#transformer `; From cf82664562b019ec27fc3d768c7cb6179e9878a1 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 28 Apr 2022 10:53:14 +0300 Subject: [PATCH 4/4] snaps --- .../__snapshots__/ScriptTransformer.test.ts.snap | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/jest-transform/src/__tests__/__snapshots__/ScriptTransformer.test.ts.snap b/packages/jest-transform/src/__tests__/__snapshots__/ScriptTransformer.test.ts.snap index a504fe01c9fa..70beba890c0b 100644 --- a/packages/jest-transform/src/__tests__/__snapshots__/ScriptTransformer.test.ts.snap +++ b/packages/jest-transform/src/__tests__/__snapshots__/ScriptTransformer.test.ts.snap @@ -362,7 +362,7 @@ exports[`ScriptTransformer throws an error if \`process\` doesn't return an obje "passthrough-preprocessor" should return an object or a Promise resolving to an object. The object must have \`code\` property with a string of processed code. - Upgrading Guide: + This error may be caused by a breaking change in Jest 28: https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation @@ -375,7 +375,7 @@ exports[`ScriptTransformer throws an error if \`process\` doesn't return an obje "passthrough-preprocessor" should return an object or a Promise resolving to an object. The object must have \`code\` property with a string of processed code. - Upgrading Guide: + This error may be caused by a breaking change in Jest 28: https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation @@ -388,7 +388,7 @@ exports[`ScriptTransformer throws an error if \`process\` doesn't return an obje "passthrough-preprocessor" should return an object or a Promise resolving to an object. The object must have \`code\` property with a string of processed code. - Upgrading Guide: + This error may be caused by a breaking change in Jest 28: https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation @@ -401,7 +401,7 @@ exports[`ScriptTransformer throws an error if \`process\` doesn't return an obje "passthrough-preprocessor" should return an object or a Promise resolving to an object. The object must have \`code\` property with a string of processed code. - Upgrading Guide: + This error may be caused by a breaking change in Jest 28: https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation @@ -414,7 +414,7 @@ exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a "passthrough-preprocessor-fruits-banana-js" should return an object or a Promise resolving to an object. The object must have \`code\` property with a string of processed code. - Upgrading Guide: + This error may be caused by a breaking change in Jest 28: https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation @@ -427,7 +427,7 @@ exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a "passthrough-preprocessor-fruits-avocado-js" should return an object or a Promise resolving to an object. The object must have \`code\` property with a string of processed code. - Upgrading Guide: + This error may be caused by a breaking change in Jest 28: https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation @@ -440,7 +440,7 @@ exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a "passthrough-preprocessor-fruits-kiwi-js" should return an object or a Promise resolving to an object. The object must have \`code\` property with a string of processed code. - Upgrading Guide: + This error may be caused by a breaking change in Jest 28: https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation @@ -453,7 +453,7 @@ exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a "passthrough-preprocessor-fruits-grapefruit-js" should return an object or a Promise resolving to an object. The object must have \`code\` property with a string of processed code. - Upgrading Guide: + This error may be caused by a breaking change in Jest 28: https://jestjs.io/docs/upgrading-to-jest28#transformer Code Transformation Documentation: https://jestjs.io/docs/code-transformation