Jest Encountered an Unexpected Token: Syntaxerror: Unexpected Token {

Jest encountered an unexpected token - SyntaxError: Unexpected token 'export'

react-markdown is shipped as js, add babel-jest as a transformer in your jest config

  transform: {
'^.+\\.ts?$': 'ts-jest',
"^.+\\.(js|jsx)$": "babel-jest"
},

Jest encountered an unexpected token when working with React TypeScript

Looks like your test file is a js file (src\__tests__\DatePicker.spec.js) instead of ts?x file which means this pattern will never meet "^.+\\.(ts|tsx)$": "ts-jest".

However, you might know tsc can also have capability to transpile your js code as well as long as you set allowJs: true as you already did. So I think your problem would be fixed as you refine your pattern to to transform above including jsx file:

{
transform: {
"^.+\\.(t|j)sx?$": "ts-jest",
}
}

Jest encountered an unexpected token + react markdown

In the jest.config file, you need to add the following to the moduleNameMapper attribute:

"react-markdown": "<rootDir>/node_modules/react-markdown/react-markdown.min.js"

So effectively, your moduleNameMapper should really look like this:

  ...
moduleNameMapper: {
'next/router': '<rootDir>/__mocks__/next/router.js',
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
'^.+\\.(jpg|jpeg|png|gif|webp|avif|svg)$': '<rootDir>/__mocks__/file-mock.js',
'react-markdown': '<rootDir>/node_modules/react-markdown/react-markdown.min.js',
},
...

Good luck!



Related Topics



Leave a reply



Submit