Hi,
Using spread operator with --coverage causes unexpected token error.
It is mentioned in #386, but I'm not sure whether this is related to #386, because in that issue it is mentioned that if the project includes js files (with the allowJs option in tsconfig.json). My project does not include any js files and there isn't allowJs option in my tsconfig.json.
Only single file is src/index.ts
const a = { a: 1 };
const b = { ...a };
jest --coverage
I also tried both configuration below, but none of them solved my problem:
"jest": {
"transform": {
"^.+\\.(j|t)sx?$": "ts-jest"
},
"mapCoverage": true,
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
}
"jest": {
"transform": {
"^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
"^.+\\.tsx?$": "ts-jest"
},
"mapCoverage": true,
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
}
@ozum Can you please create a minimal repo that reproduces this issue?
@kulshekhar I created a minimal repo. Please see https://github.com/ozum/ts-jest-spread
Please run npm test to see error, and run npm run test:nocoverage to see without coverage.
@ozum setting the target to esnext is what's causing this issue. Because of this, the spread operator isn't converted to a form that istanbul can understand, causing it to throw that error.
A quick solution is to create a tsconfig for tests with the following content:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es5"
}
}
and use it in package.json:
"jest": {
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.test.json"
}
},
:
:
But why is it failing only when "coverage" is required though? Normal testing with target esnext worked fine.
Most helpful comment
@ozum setting the target to
esnextis what's causing this issue. Because of this, the spread operator isn't converted to a form that istanbul can understand, causing it to throw that error.A quick solution is to create a tsconfig for tests with the following content:
and use it in
package.json: