Using latest Jest, with no cache, clean npm install.
Whenever a switch statement is imported through jest, I get the following error:
/utils/layout.js:16
case /* istanbul ignore next */(++_cover__().s['5'], 'SORT_POLL_BY'): /* istanbul ignore next */++_cover__().b['1'][0]++_cover__().s['9'];
^^^^^^^^^^^^^^^^^^^^^^
ReferenceError: Invalid left-hand side expression in prefix operation
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:284:10)
at Object.<anonymous> (/__tests__/layout.spec.js:1:169)
ex.
export default (val) => {
switch (val) {
case -1:
console.log('-1');
break;
default:
console.log('default');
}
};
test:
import reducer from '../layout';
test('it should layout', () => {
expect(true).toBe(true);
});
here is my package.json as well:
"jest": {
"coverageDirectory": "<rootDir>/__coverage__",
"coverageReporters": ["html", "text"],
"moduleDirectories": ["node_modules", "bower_components", "shared"],
"moduleNameMapper": {
"^shared": "<rootDir>/src/shared"
}
},
I would assume its my own issue, but no matter how simple I make the switch, in any imported file, it throws the error.
interesting. this is definitely an issue with instrumented code.
@seethroughtrees can you confirm that it works when you run it without --coverage flag, or with collectCoverage: false?
also, do you use babel? and if yes, what does your .babelrc look like?
Thanks so much for the response @dmitriiabramov
Oddly, it still throws the error without coverage flag, and with adding "collectCoverage": false, to my package.json.
I am using latest babel, I've minimized my babelrc file and get the same result:
Thanks again.
{
"presets": [
"react",
"es2015"
]
}
this is very, very strange then.
those are definitely some kind of coverage instrumentation
case /* istanbul ignore next */(++_cover__().s['5'], 'SORT_POLL_BY'): /* istanbul ignore next */++_cover__().b['1'][0]++_cover__().s['9'];
that is causing an error.
so something is transforming it by adding those _cover__() statements.
do you have a reproducible project on github or anything i can look at?
I'm behind an enterprise GH instance, but I'm going to pull it out into a clean repo, I'll test and update here. Thanks!
Ok. I set up a clean repo and it handles the same file without issue.
Must be something in my codebase, I'll dig in and post when I find the issue. Thanks again @dmitriiabramov
thanks for investigating @seethroughtrees!
So I found by adding 'coveragePathIgnorePatterns', my problems seemed to go away! Not sure if that's an expected solution, but it does give me the expected compile and reporting.
"coveragePathIgnorePatterns": [
"<rootDir>/build/",
"<rootDir>/node_modules/"
],
Going to close, feel free to ping or re-open if you want me to test anything additionally.
I have the same problem and @seethroughtrees' solution doesn't work for me.
I debugged this for 4 hours 馃槱. In the end, I found out the problem is in this code:
return babel.transform(
src,
Object.assign({}, options, {filename, plugins}),
).code;
The options object looks like this:
{
auxiliaryCommentBefore: " istanbul ignore next ",
presets: [ { plugins: [ null ] } ],
retainLines: true
}
If I set retailLines to false it works.
From Babel's documentation:
retainLinesRetain line numbers. This will lead to wacky code but is handy for scenarios where you can鈥檛 use source maps. (NOTE: This will not retain the columns)
I tried to set retainLines to false from my package.json but it gets ignored. It seems the options was set to true in https://github.com/babel/babel-jest/pull/23
I'm really out of ideas as of what to do now. If you can help me somehow I would appreciate it.
@Gpx that doesn't sound right. Can you throw up a project on github that shows this issue? Are you sure your .babelrc is properly set-up and that you ran Jest with --no-cache while modifying your .babelrc?
@cpojer, here you have it https://github.com/Gpx/broken-jest
There are two branches master which is broken and fix-jest that runs.
In this PR https://github.com/Gpx/broken-jest/pull/1 you can see the only change I made.
I'm assuming your problem is not using semicolons. Please use semicolons or use your own preprocessor that doesn't use retainLines. Further, if this is a bug with retainLines you can also report it on the babel issue tracker and hope they'll fix it. The reason we use retainLines is to get reasonably accurate stack traces from transformed files.
I tried to add semicolons to the component and the test file but I still get the same error. Only if I don't use switch it works.
On the Babel repo I haven't found any related issue.
I guess it's something due to my configuration. I'm going to tweak the package.json to see if something comes up.
Found the error 馃帀 I was using babel-plugin-__coverage__. Removing it fixed everything.
Thanks @cpojer for the help.
Most helpful comment
Found the error 馃帀 I was using
babel-plugin-__coverage__. Removing it fixed everything.Thanks @cpojer for the help.