Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@jest/transform): throw better error if an invalid return value if encountered #12764

Merged
merged 4 commits into from Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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