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: print underlying error when global hooks fail #11003

Merged
merged 6 commits into from Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/jest-cli/src/cli/index.ts
Expand Up @@ -37,7 +37,7 @@ export async function run(
} catch (error) {
clearLine(process.stderr);
clearLine(process.stdout);
if (error.stack) {
if (error?.stack) {
console.error(chalk.red(error.stack));
} else {
console.error(chalk.red(error));
Expand Down
30 changes: 20 additions & 10 deletions packages/jest-core/src/runGlobalHook.ts
Expand Up @@ -10,6 +10,7 @@ import {ScriptTransformer} from '@jest/transform';
import type {Config} from '@jest/types';
import type {Test} from 'jest-runner';
import {interopRequireDefault} from 'jest-util';
import prettyFormat from 'pretty-format';

export default async ({
allTests,
Expand All @@ -29,7 +30,7 @@ export default async ({
}

if (globalModulePaths.size > 0) {
await pEachSeries(Array.from(globalModulePaths), async modulePath => {
await pEachSeries(globalModulePaths, async modulePath => {
if (!modulePath) {
return;
}
Expand All @@ -45,17 +46,26 @@ export default async ({

const transformer = new ScriptTransformer(projectConfig);

await transformer.requireAndTranspileModule(modulePath, async m => {
const globalModule = interopRequireDefault(m).default;
try {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore whitespace

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a link to whitespace-free diff: https://github.com/facebook/jest/pull/11003/files?w=1

await transformer.requireAndTranspileModule(modulePath, async m => {
const globalModule = interopRequireDefault(m).default;

if (typeof globalModule !== 'function') {
throw new TypeError(
`${moduleName} file must export a function at ${modulePath}`,
);
}
if (typeof globalModule !== 'function') {
throw new TypeError(
`${moduleName} file must export a function at ${modulePath}`,
);
}

await globalModule(globalConfig);
});
await globalModule(globalConfig);
});
} catch (error) {
throw new Error(
`Got error running ${moduleName} - ${modulePath}, reason: ${prettyFormat(
error,
{maxDepth: 3},
)}`,
);
}
});
}

Expand Down