Reputation: 494
After I added new package aws-rum-web
and re-ran the tests, I received the following errors:
● Test suite failed to run
Jest encountered an unexpected token
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.
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:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/workplace/wjiajian/lomp-ui/src/SortCenterNEFLaborOrderUI/node_modules/web-vitals/attribution.js:18
export * from './dist/web-vitals.attribution.js';
^^^^^^
SyntaxError: Unexpected token 'export'
4 | getStageName,
5 | } from "@amzn/hvh-hamilton-base-ui-components/lib/util/NavigationUtil";
> 6 | import { AwsRum } from "aws-rum-web";
| ^
7 | import { PageAttributes } from "aws-rum-web/dist/cjs/sessions/PageManager";
8 | import _ from "lodash";
9 | import { CountryCode, IRumConfig, RumConfig, Stage } from "../constants/RumConstants";
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (node_modules/aws-rum-web/dist/cjs/plugins/event-plugins/WebVitalsPlugin.js:20:21)
at Object.<anonymous> (node_modules/aws-rum-web/dist/cjs/orchestration/Orchestration.js:44:25)
at Object.<anonymous> (node_modules/aws-rum-web/dist/cjs/index.js:18:23)
at Object.<anonymous> (src/util/Rum.ts:6:1)
at Object.<anonymous> (src/App.tsx:9:1)
at Object.<anonymous> (src/tests/App.test.tsx:3:1)
By following multiple posts, I added the following in jest.config.js
-
const esModules = ['web-vitals'].join('|');
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
testMatch: ["**/__tests__/**/*.ts?(x)", "**/?(*.)+(test).ts?(x)"],
transform: {
"^.+\\.(js|ts)$": "ts-jest",
},
transformIgnorePatterns: [
`/node_modules/(?!${esModules})`,
],
}
However, I still received the same error as before. Does anyone have any ideas? Thanks!
Upvotes: 0
Views: 342
Reputation: 494
To answer my own question, adding --transformIgnorePatterns \"node_modules/(?!web-vitals)/\"
to script in package.json somehow solves the problem -
"scripts": {
...
"test": "TZ=UTC react-app-rewired test --coverage --watchAll=false --transformIgnorePatterns \"node_modules/(?!web-vitals)/\" && npm run move-docs && npm run lint",
"test:watch": "react-app-rewired test --coverage --watchAll=true --transformIgnorePatterns \"node_modules/(?!web-vitals)/\"",
"test:single": "react-app-rewired test --coverage --watchAll=false --transformIgnorePatterns \"node_modules/(?!web-vitals)/\"",
"test:update": "react-app-rewired test -u --watchAll=false --transformIgnorePatterns \"node_modules/(?!web-vitals)/\""
},
...
Upvotes: 0