-
Notifications
You must be signed in to change notification settings - Fork 448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jest encountered an unexpected token #937
Comments
I have since switched to |
I was having this same problem, and this seems to work: // jest.config.js
module.exports = {
preset: 'ts-jest',
transform: {
'^.+\\.tsx?$': 'babel-jest',
},
} |
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 |
I am having same issue.
|
@mohsinulhaq I was able to fix the error similiar to yours by changing jsx property of tsconfig.json. I changed
jest.config.js
|
I think it's much better now to rely on babel for compiling typescript after babel 7, no |
@mohsinulhaq can you share your working jest.config.js file ? |
https://github.com/mohsinulhaq/react-popper-tooltip |
closing as there's no minimal repo and because this isn't required by the OP any more |
The only issue, when I run a project it changes |
@zishe i was seeing the same issue. I ended up creating a then i added this to my jest config:
|
thanks, this worked for me. |
yeah if you haven't got any mind blowing issues with that stack go ahead. Looks like TypeORM doesn't want to work with babel stack. |
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
|
you should have used ts-jest transformer. If you use babe-jest in transform config you don't use ts-jest so you don't need it in your dependencies |
all I had to do to fix this was create a file module.exports = {
preset: 'ts-jest'
}; while having also installed |
@jubairsaidi I just want to add onto this, if you have anything in the transform object, remove it.
|
I was able to get the JS node module with the offending import statement transpiled by doing: and use the js-with-ts preset:
|
same error again in another project node_modules\jest-config\build\readConfigFileAndSetRootDir.js:110 SyntaxError: Unexpected token import :-( Cant get it running with typescript and different presets.... |
I had the same error with 9.11.2 node version. |
For anyone still getting this error after reading all the responses above, this helped fix it for me.
globals: {
"ts-jest": {
tsConfig: "tsconfig.test.json"
}
}
{
"compilerOptions": {
"jsx": "react",
"allowJs": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"noImplicitAny": true,
"sourceMap": true,
"target": "es5"
}
} |
So I came across this solution in my Google travels. It worked for me. If it is incorrect, please explain my mistake. .babelrc
|
Wrestled with this error and finally got
Here is the bare bones repo for anyone that's interested. It's using |
Thank you very much @arteforme !!!!!!!!!!! |
Any reason why this bug still exists? I tried everything and its still not working, I can't get ts-jest to work at all, if I import another typescript repo. Jest really needs to have TypeScript support out of the box, with 0 configuration. At this points its just as difficult to setup like Mocha/Chai |
@joshuarobs jest does support TypeScript out of the box via babel. It is just You can also check my repo for the setup https://github.com/ahnpnl/react-ts-jest-babel with React. |
@ahnpnl |
@joshuarobs I dont know how to setup for |
this works for mine:
|
same issue and it works for mine:
export default {
...
"transform": {
"^.+\\.(js|ts|tsx)$": "ts-jest"
},
...
} |
Next.js requires that jsx is set to preserve in order to provide a "optimized" jsx transformation. However, Jest needs jsx to be transformed by the ts compiler. See * kulshekhar/ts-jest#937 * https://stackoverflow.com/questions/61973940/next-js-typescript-and-tsconfig-json-jsx-property-being-overwritten This isn't particularly well publicised by Next from what I can see * vercel/next.js#11216 * vercel/next.js#19155
for me adding "js" to transform fixed the issue This is before "transform": {
- "^.+\\.(ts|tsx)$": "ts-jest"
} This is after "transform": {
+ "^.+\\.(ts|tsx|js)$": "ts-jest"
} |
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. { |
Confirmed that this gets me past the error of Just for others in this same discovery, after this fix if your application specifies custom module paths in your
Then you also need to add to your
|
For me the error occured in a non-React project, and the cause was simple: I've run |
The best solution is to give up on writing tests in JavaScript. Come back in another ten years. |
it can work when tsconfig.json file add "jsxFactory": "React.createElement". |
Issue :
I am using ts-jest to test my typescript library. When I run jest, I get the following error:
Here is my
jest.config.js
:and my
babel.config.js
:In my package.json, I do have
jest
,ts-jest
,babel-jest
andbabel-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.
The text was updated successfully, but these errors were encountered: