From 880e04ce927c33713a32362b116e771654c865de Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 28 Apr 2022 11:31:51 +0300 Subject: [PATCH] fix(@jest/transform): throw better error if an invalid return value if encountered (#12764) --- CHANGELOG.md | 1 + .../jest-transform/src/ScriptTransformer.ts | 4 +- .../ScriptTransformer.test.ts.snap | 72 ++++++++++++------- .../src/runtimeErrorsAndWarnings.ts | 18 +++-- 4 files changed, 65 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4728e16f5def..096c72a4bf18 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 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..70beba890c0b 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. + 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 " @@ -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. + 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 " @@ -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. + 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,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. + 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 " @@ -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. + 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 " @@ -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. + 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 " @@ -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. + 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 " @@ -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. + 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 " diff --git a/packages/jest-transform/src/runtimeErrorsAndWarnings.ts b/packages/jest-transform/src/runtimeErrorsAndWarnings.ts index c57292ac5b14..2431dbfc67b8 100644 --- a/packages/jest-transform/src/runtimeErrorsAndWarnings.ts +++ b/packages/jest-transform/src/runtimeErrorsAndWarnings.ts @@ -14,16 +14,24 @@ const DOCUMENTATION_NOTE = ` ${chalk.bold( )} https://jestjs.io/docs/code-transformation `; +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 +`; -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 = (