Have a typescript project with installed nodejs module.
MyModule:
import * as formatter from 'nodejs-formatter' // means custom module in /node_modules/
imports in test file:
import {MyModule} from '../../app/modules/MyModule' // menas .ts
discribe('some test', () => {...})
message from console:
Test suite failed to run
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.
Details of error:
Details:
/Users/michael.y/workspace/projects/yggdrasil-boost-ui/node_modules/nodejs-formatter/src/index.js:2
import * as strings from './strings'
^
SyntaxError: Unexpected token *
1 | import SomeService from './SomeService'
2 | import * as formatter from 'nodejs-formatter'
> 3 | import {someFunc} from './someFunc'
i've tried specify ignorance like this in package.json "jest" section :
...
"transformIgnorePatterns": ["node_modules/(?!nodejs-formatter)"],
...
but I always have this error. One thing, when I've specify transform for JS throw "^.+\\.js?$": "<rootDir>/node_modules/babel-jest", import works, but have an other error, plugins in .babelrc inside of module fails...
I can't fix the library which I use, so how I can fix it in ts-jest?
Solve it:
Just added .babelrc
{
"presets": [["env", {"modules": false}]],
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
Problem still exist, but little bit different, if just import module, it's ok, if use module, error again.
Finally I solve issue. First of all, it can be used without .babelrc, but for some reason if I want include some library in test file, without .babelrc doesn't work. But for avoid error in module which used npm package works another things:
config.json where node-module is some abastract module (just for an examlpe):
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.(ts|tsx)$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.jest.json"
}
},
"testMatch": [
"**/tests/**.test.+(ts|tsx|js)"
],
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!node-formatter)"
],
"moduleNameMapper": {
"node-formatter": "<rootDir>/node_modules/node-formatter"
}
}
tsconfig.test.json:
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": false,
"sourceMap": false,
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"target": "es5",
"allowJs": true,
"moduleResolution": "node"
}
}
Most helpful comment
Finally I solve issue. First of all, it can be used without .babelrc, but for some reason if I want include some library in test file, without .babelrc doesn't work. But for avoid error in module which used npm package works another things:
config.json where node-module is some abastract module (just for an examlpe):
tsconfig.test.json: