From e5f5c11de01304fc212241d258cd00ae7b667fe9 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 31 Oct 2020 14:25:32 +0100 Subject: [PATCH 1/2] chore: show enhanced syntax error for all syntax errors --- CHANGELOG.md | 2 ++ packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66c44bef29e8..1d135a55b3f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749)) + ### Chore & Maintenance ### Performance diff --git a/packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts b/packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts index 1473265b72e2..4b403505a00b 100644 --- a/packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts +++ b/packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts @@ -23,8 +23,6 @@ export default function handlePotentialSyntaxError( if ( // `instanceof` might come from the wrong context e.name === 'SyntaxError' && - (e.message.includes('Unexpected token') || - e.message.includes('Cannot use import')) && !e.message.includes(' expected') ) { throw enhanceUnexpectedTokenMessage(e); From 65111b2e3c58438ae8677c85749a63a2a9b6200f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 4 Nov 2020 07:35:26 +0100 Subject: [PATCH 2/2] tweak message --- .../jest-transform/src/enhanceUnexpectedTokenMessage.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts b/packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts index 4b403505a00b..e666361d4099 100644 --- a/packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts +++ b/packages/jest-transform/src/enhanceUnexpectedTokenMessage.ts @@ -35,9 +35,11 @@ export function enhanceUnexpectedTokenMessage(e: Error): Error { e.stack = `${chalk.bold.red('Jest encountered an unexpected token')} -This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. +Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax. -By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules". +Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration. + +By default "node_modules" folder is ignored by transformers. Here's what you can do: ${DOT}If you are trying to use ECMAScript Modules, see ${chalk.underline(