Skip to content

Commit

Permalink
fix(@jest/transform): throw better error if an invalid return value i…
Browse files Browse the repository at this point in the history
…f encountered (#12764)
  • Loading branch information
mrazauskas committed Apr 28, 2022
1 parent e2a452f commit 880e04c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -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));
}
}

Expand Down
Expand Up @@ -358,79 +358,103 @@ 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`] = `
"<red><bold>● Invalid return value:</></>
<red> Code transformer's \`process\` method must return an object containing \`code\` key </>
<red> with processed string. If \`processAsync\` method is implemented it must return </>
<red> a Promise resolving to an object containing \`code\` key with processed string.</>
<red> \`process()\` or/and \`processAsync()\` method of code transformer found at </>
<red> "passthrough-preprocessor" </>
<red> should return an object or a Promise resolving to an object. The object </>
<red> must have \`code\` property with a string of processed code.</>
<red> <bold>This error may be caused by a breaking change in Jest 28:</></>
<red> https://jestjs.io/docs/upgrading-to-jest28#transformer</>
<red> <bold>Code Transformation Documentation:</></>
<red> https://jestjs.io/docs/code-transformation</>
<red></>"
`;
exports[`ScriptTransformer throws an error if \`process\` doesn't return an object containing \`code\` key with processed string 2`] = `
"<red><bold>● Invalid return value:</></>
<red> Code transformer's \`process\` method must return an object containing \`code\` key </>
<red> with processed string. If \`processAsync\` method is implemented it must return </>
<red> a Promise resolving to an object containing \`code\` key with processed string.</>
<red> \`process()\` or/and \`processAsync()\` method of code transformer found at </>
<red> "passthrough-preprocessor" </>
<red> should return an object or a Promise resolving to an object. The object </>
<red> must have \`code\` property with a string of processed code.</>
<red> <bold>This error may be caused by a breaking change in Jest 28:</></>
<red> https://jestjs.io/docs/upgrading-to-jest28#transformer</>
<red> <bold>Code Transformation Documentation:</></>
<red> https://jestjs.io/docs/code-transformation</>
<red></>"
`;
exports[`ScriptTransformer throws an error if \`process\` doesn't return an object containing \`code\` key with processed string 3`] = `
"<red><bold>● Invalid return value:</></>
<red> Code transformer's \`process\` method must return an object containing \`code\` key </>
<red> with processed string. If \`processAsync\` method is implemented it must return </>
<red> a Promise resolving to an object containing \`code\` key with processed string.</>
<red> \`process()\` or/and \`processAsync()\` method of code transformer found at </>
<red> "passthrough-preprocessor" </>
<red> should return an object or a Promise resolving to an object. The object </>
<red> must have \`code\` property with a string of processed code.</>
<red> <bold>This error may be caused by a breaking change in Jest 28:</></>
<red> https://jestjs.io/docs/upgrading-to-jest28#transformer</>
<red> <bold>Code Transformation Documentation:</></>
<red> https://jestjs.io/docs/code-transformation</>
<red></>"
`;
exports[`ScriptTransformer throws an error if \`process\` doesn't return an object containing \`code\` key with processed string 4`] = `
"<red><bold>● Invalid return value:</></>
<red> Code transformer's \`process\` method must return an object containing \`code\` key </>
<red> with processed string. If \`processAsync\` method is implemented it must return </>
<red> a Promise resolving to an object containing \`code\` key with processed string.</>
<red> \`process()\` or/and \`processAsync()\` method of code transformer found at </>
<red> "passthrough-preprocessor" </>
<red> should return an object or a Promise resolving to an object. The object </>
<red> must have \`code\` property with a string of processed code.</>
<red> <bold>This error may be caused by a breaking change in Jest 28:</></>
<red> https://jestjs.io/docs/upgrading-to-jest28#transformer</>
<red> <bold>Code Transformation Documentation:</></>
<red> https://jestjs.io/docs/code-transformation</>
<red></>"
`;
exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a promise of object containing \`code\` key with processed string 1`] = `
"<red><bold>● Invalid return value:</></>
<red> Code transformer's \`process\` method must return an object containing \`code\` key </>
<red> with processed string. If \`processAsync\` method is implemented it must return </>
<red> a Promise resolving to an object containing \`code\` key with processed string.</>
<red> \`process()\` or/and \`processAsync()\` method of code transformer found at </>
<red> "passthrough-preprocessor-fruits-banana-js" </>
<red> should return an object or a Promise resolving to an object. The object </>
<red> must have \`code\` property with a string of processed code.</>
<red> <bold>This error may be caused by a breaking change in Jest 28:</></>
<red> https://jestjs.io/docs/upgrading-to-jest28#transformer</>
<red> <bold>Code Transformation Documentation:</></>
<red> https://jestjs.io/docs/code-transformation</>
<red></>"
`;
exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a promise of object containing \`code\` key with processed string 2`] = `
"<red><bold>● Invalid return value:</></>
<red> Code transformer's \`process\` method must return an object containing \`code\` key </>
<red> with processed string. If \`processAsync\` method is implemented it must return </>
<red> a Promise resolving to an object containing \`code\` key with processed string.</>
<red> \`process()\` or/and \`processAsync()\` method of code transformer found at </>
<red> "passthrough-preprocessor-fruits-avocado-js" </>
<red> should return an object or a Promise resolving to an object. The object </>
<red> must have \`code\` property with a string of processed code.</>
<red> <bold>This error may be caused by a breaking change in Jest 28:</></>
<red> https://jestjs.io/docs/upgrading-to-jest28#transformer</>
<red> <bold>Code Transformation Documentation:</></>
<red> https://jestjs.io/docs/code-transformation</>
<red></>"
`;
exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a promise of object containing \`code\` key with processed string 3`] = `
"<red><bold>● Invalid return value:</></>
<red> Code transformer's \`process\` method must return an object containing \`code\` key </>
<red> with processed string. If \`processAsync\` method is implemented it must return </>
<red> a Promise resolving to an object containing \`code\` key with processed string.</>
<red> \`process()\` or/and \`processAsync()\` method of code transformer found at </>
<red> "passthrough-preprocessor-fruits-kiwi-js" </>
<red> should return an object or a Promise resolving to an object. The object </>
<red> must have \`code\` property with a string of processed code.</>
<red> <bold>This error may be caused by a breaking change in Jest 28:</></>
<red> https://jestjs.io/docs/upgrading-to-jest28#transformer</>
<red> <bold>Code Transformation Documentation:</></>
<red> https://jestjs.io/docs/code-transformation</>
<red></>"
`;
exports[`ScriptTransformer throws an error if \`processAsync\` doesn't return a promise of object containing \`code\` key with processed string 4`] = `
"<red><bold>● Invalid return value:</></>
<red> Code transformer's \`process\` method must return an object containing \`code\` key </>
<red> with processed string. If \`processAsync\` method is implemented it must return </>
<red> a Promise resolving to an object containing \`code\` key with processed string.</>
<red> \`process()\` or/and \`processAsync()\` method of code transformer found at </>
<red> "passthrough-preprocessor-fruits-grapefruit-js" </>
<red> should return an object or a Promise resolving to an object. The object </>
<red> must have \`code\` property with a string of processed code.</>
<red> <bold>This error may be caused by a breaking change in Jest 28:</></>
<red> https://jestjs.io/docs/upgrading-to-jest28#transformer</>
<red> <bold>Code Transformation Documentation:</></>
<red> https://jestjs.io/docs/code-transformation</>
<red></>"
Expand Down
18 changes: 13 additions & 5 deletions packages/jest-transform/src/runtimeErrorsAndWarnings.ts
Expand Up @@ -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 = (
Expand Down

0 comments on commit 880e04c

Please sign in to comment.