ts-jest: Jest encountered an unexpected token
Issue :
I am using ts-jest to test my typescript library. When I run jest, I get the following error:
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.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• 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/en/configuration.html
Details:
/Users/mohsinulhaq/Documents/react-popper-tooltip/tests/TooltipTrigger.spec.tsx:9
const BasicTooltipTrigger = ({ tooltip, children, hideArrow, ...props }) => (<src_1.default {...props} tooltip={({ getTooltipProps, getArrowProps, placement }) => (<div {...getTooltipProps({
^
SyntaxError: Unexpected token <
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Here is my jest.config.js:
module.exports = {
preset: 'ts-jest',
globals: {
'ts-jest': {
diagnostics: false
}
}
};
and my babel.config.js:
module.exports = {
presets: ['@babel/typescript', ['@babel/env', {loose: true}], '@babel/react'],
plugins: [['@babel/proposal-class-properties', {loose: true}]]
};
In my package.json, I do have jest, ts-jest, babel-jest and babel-core@7.0.0-bridge.0 installed.
Please help.
Thanks.
EDIT: I get the same output with the babel config file removed. Looks like ts-jest is not picking the babel config file up.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 19
- Comments: 36 (1 by maintainers)
@mohsinulhaq I was able to fix the error similiar to yours by changing jsx property of tsconfig.json. I changed
"jsx": "preserve"->"jsx": "react"So my tsconfig.json looks like thisjest.config.js
I was having this same problem, and this seems to work:
@zishe i was seeing the same issue. I ended up creating a
tsconfig.jsonand atsconfig.test.json. the latter extendstsconfig.jsonand changescompilerOptions.jsxto bereact.then i added this to my jest config:
Wrestled with this error and finally got
ts-jestto work in my project. To do so, I created a bare bones repo, got it working there, and then applied what I learned to my project. To summarize:"jsx": "preserve"to"jsx": "react"yarn jest --clearCacheas @ahnpnl mentioned. Without this step, it theunexpected token errorwould still to occur whenever I ran tests.Here is the bare bones repo for anyone that’s interested. It’s using
testing-library/react, but it should work with other packages. Also note, it doesn’t rely on babel.I have since switched to
babel-jest+@babel/preset-typescript, which seems to be a much better and well-supported alternative.for me adding “js” to transform fixed the issue
This is before
This is after
For anyone still getting this error after reading all the responses above, this helped fix it for me. I’m only using ts-jest, without any babel dependencies whatsoever in my
package.json.Copy your
tsconfig.jsontotsconfig.test.jsonAdd this line to your
jest.config.js:The only issue, when I run a project it changes
jsxback topreserveall I had to do to fix this was create a file
jest.config.jswith the contents of:while having also installed
ts-jestThe best solution is to give up on writing tests in JavaScript. Come back in another ten years.
So I came across this solution in my Google travels. It worked for me. If it is incorrect, please explain my mistake.
.babelrc
https://github.com/mohsinulhaq/react-popper-tooltip here my repository where I use the above config
Jest expects module in your tsconfig.spec.ts to set to commonjs. After setting it to commonjs, you have to run jest --clearCache and then run your tests again
same issue and it works for mine:
jest --init, createjest.config.tsjest.config.ts:I had a similar problem. Exact problem was this:
I’m using babel 7 and jest 24.9. Changing .babelrc to babel.config.js didn’t resolve the problem. Content of the file was:
The problem in it was missing
@babel/preset-typescriptin env. I changed it tothanks, this worked for me.
I think it’s much better now to rely on babel for compiling typescript after babel 7, no
ts-jestneeded as there is an official typescript babel preset (https://babeljs.io/docs/en/babel-preset-typescript). You can wire it to jest usingbabel-jestlike you would normally do for non-ts projects.For whom having this issue in typescript 4.1 or later, and have explicitly set ‘jsxImportSource’ option in tsconfig.json file, you could probably try setting ‘jsx’ to ‘react-jsx’, as ‘react’ mode would make ‘jsxImportSource’ yelling.
e.g.
{ “compilerOptions”: { “jsx”: “react-jsx”, “jsxImportSource”: “@emotion/react”, } }
I was able to get the JS node module with the offending import statement transpiled by doing:
and use the js-with-ts preset:
For me the error occured in a non-React project, and the cause was simple: I’ve run
npx ts-jest config:initin a subdirectory (by accident) instead of the root folder wheretsconfig.jsonresides. Movingjest.config.jsfile up the tree fixed the problem.@jubairsaidi I just want to add onto this, if you have anything in the transform object, remove it. I originally had this which was causing problems with `preset: “ts–jest”
closing as there’s no minimal repo and because this isn’t required by the OP any more