UNPKG

2.61 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = handlePotentialSyntaxError;
7exports.enhanceUnexpectedTokenMessage = enhanceUnexpectedTokenMessage;
8function _chalk() {
9 const data = _interopRequireDefault(require('chalk'));
10 _chalk = function () {
11 return data;
12 };
13 return data;
14}
15function _interopRequireDefault(obj) {
16 return obj && obj.__esModule ? obj : {default: obj};
17}
18/**
19 * Copyright (c) Meta Platforms, Inc. and affiliates.
20 *
21 * This source code is licensed under the MIT license found in the
22 * LICENSE file in the root directory of this source tree.
23 */
24
25const DOT = ' \u2022 ';
26function handlePotentialSyntaxError(e) {
27 if (e.codeFrame != null) {
28 e.stack = `${e.message}\n${e.codeFrame}`;
29 }
30 if (
31 // `instanceof` might come from the wrong context
32 e.name === 'SyntaxError' &&
33 !e.message.includes(' expected')
34 ) {
35 throw enhanceUnexpectedTokenMessage(e);
36 }
37 return e;
38}
39function enhanceUnexpectedTokenMessage(e) {
40 e.stack = `${_chalk().default.bold.red(
41 'Jest encountered an unexpected token'
42 )}
43
44Jest 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.
45
46Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
47
48By default "node_modules" folder is ignored by transformers.
49
50Here's what you can do:
51${DOT}If you are trying to use ECMAScript Modules, see ${_chalk().default.underline(
52 'https://jestjs.io/docs/ecmascript-modules'
53 )} for how to enable it.
54${DOT}If you are trying to use TypeScript, see ${_chalk().default.underline(
55 'https://jestjs.io/docs/getting-started#using-typescript'
56 )}
57${DOT}To have some of your "node_modules" files transformed, you can specify a custom ${_chalk().default.bold(
58 '"transformIgnorePatterns"'
59 )} in your config.
60${DOT}If you need a custom transformation specify a ${_chalk().default.bold(
61 '"transform"'
62 )} option in your config.
63${DOT}If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the ${_chalk().default.bold(
64 '"moduleNameMapper"'
65 )} config option.
66
67You'll find more details and examples of these config options in the docs:
68${_chalk().default.cyan('https://jestjs.io/docs/configuration')}
69For information about custom transformations, see:
70${_chalk().default.cyan('https://jestjs.io/docs/code-transformation')}
71
72${_chalk().default.bold.red('Details:')}
73
74${e.stack ?? ''}`.trimRight();
75 return e;
76}