27 Sep 2023 02:06 PM
After migration `@dynatrace/strato-components-preview` from 0.99 to 0.104 every unit test is failing with the same error:
```
FAIL src/app/components/BuildsOverTimeContainer.spec.tsx
● 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:
/Users/user/code/dynatrace/node_modules/@dynatrace/strato-design-tokens/colors/index.esm.js:15
...
SyntaxError: Unexpected token 'export'
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
at Object.<anonymous> (node_modules/@dynatrace/strato-components-preview/core/index.cjs.js:131:30)```
jest.config.ts:
```
import type { Config } from "@jest/types";
import { stratoPreset } from "@dynatrace/strato-components-preview/testing/jest";
const config: Config.InitialOptions = {
preset: "ts-jest",
testEnvironment: "jsdom",
roots: ["<rootDir>/src"],
setupFiles: ["@dynatrace/strato-components-preview/testing"],
transform: {
".(css|scss|sass|less)$": "<rootDir>/style-mock.ts",
},
setupFilesAfterEnv: [`<rootDir>/src/jest-setup.ts`],
...stratoPreset,
moduleNameMapper: {
...stratoPreset.moduleNameMapper,
"^libs/(.*)": "<rootDir>/src/libs/$1",
"^components/(.*)": "<rootDir>/src/app/components/$1",
"^hooks/(.*)": "<rootDir>/src/app/hooks/$1",
"^contexts/(.*)": "<rootDir>/src/app/contexts/$1",
},
};
export default config;
```
Solved! Go to Solution.
27 Sep 2023 06:04 PM
Hi @beigert ,
Can you add this in your module name resolution config (jest.config.ts):
moduleNameMapper: {
"^@dynatrace/strato-design-tokens/(.*)$": "<rootDir>/node_modules/@dynatrace/strato-design-tokens/$1",
"^@dynatrace/strato-design-tokens$": "<rootDir>/node_modules/@dynatrace/strato-design-tokens",
},
and check if this fixes your problem?
Best,
Sini
28 Sep 2023 10:46 AM
it helps a little bit: tests that don't import anything from `strato-components-preview` works. However rest of them fail with the same error:
```
FAIL src/app/components/BuildsOverTimeContainer.spec.tsx
● 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:
/Users/przemyslawbeigert/code/dynatrace/pipeline-observability-application/node_modules/@dynatrace/strato-components-preview/index.esm.js:2
export * from "@dynatrace/strato-components-preview/buttons";
^^^^^^
SyntaxError: Unexpected token 'export'
> 1 | import { Flex, Skeleton } from "@dynatrace/strato-components-preview";
| ^
2 | import { TimeseriesChart } from "@dynatrace/strato-components-preview/charts";
3 | import Colors from "@dynatrace/strato-design-tokens/colors";
4 | import { InformationIcon } from "@dynatrace/strato-icons";
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
at Object.<anonymous> (src/app/components/BuildsOverTimeContainer.tsx:1:1)
at Object.<anonymous> (src/app/components/BuildsOverTimeContainer.spec.tsx:10:1)
```
28 Sep 2023 01:52 PM
@beigert unfortunately I can't help you further. Please create a support ticket in https://one.dynatrace.com/hc/en-us/requests with a reference to this thread and the error messages. Our developers need to have a detailed look at it.
Thx,
Sini
04 Oct 2023 09:44 AM
It works, however after clear the cache by `rm -rf node_modules/.cache`. However pls update docs: https://developer.dynatrace.com/develop/testing/unit-tests/
06 Oct 2023 08:04 AM
Just synced with the team about this.
this workaround won't be needed anymore with design system version 0.106.2 and design tokens version 0.19.0