2

I'm getting SyntaxError: Unexpected token import on my setup file for my Jest tests. I know the fix is via the .babelrc file. This actually works in some cases. I copy pasted it from https://github.com/zeit/next.js/tree/canary/examples/with-jest.

When I run npm test, it fails. When I run npm run test, it also fails. When I run npm run test:coverage, it works. My scripts are these:

"test": "NODE_ENV=test jest",
"test:coverage": "NODE_ENV=test jest  --coverage --coverageDirectory=coverage",

If I change test:coverage to just NODE_ENV=test jest, that fails. So somehow requesting a coverage report magically fixes the issue.

Strangely, the coverage one was failing while the basic one was working. I removed my package lock, removed modules, and tried npm install again. Then the fail cases reversed to what I wrote above.

My setup file is this:

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import jestFetch from 'jest-fetch-mock';

configure({ adapter: new Adapter() });
global.fetch = jestFetch;

I've modified the Nextjs sample app to be nearly identical, yet the error doesn't appear. I have no idea what the difference could be.

UPDATE

I literally copied all contents of the sample and it still didn't work. I then RENAMED my folder from my-app to my-appy and it did work. I went back all my original code, including the folder name my-app and it failed. I renamed the folder and it worked. How is the folder impacting what happens when I run npm test

UPDATE 2

Other people downloading my repo have no issue. Have never run into an issue with a folder name quite like this.

UPDATE 3

Cloning to /Users/dstein/Repositories/my-app fails. Cloning to /Users/dstein/my-app works. Cloning to /Users/dstein/Repositories/abc/my-app works. Not sure what could be targeting that one random path to failure, esp given that renaming the folder of my-app itself was working.

Dave Stein
  • 8,653
  • 13
  • 56
  • 104

1 Answers1

2

I thought about the only thing that could be different on my computer - cache. I tried doing "test": "NODE_ENV=test BABEL_DISABLE_CACHE=1 jest", to no avail. I then searched the Jest codebase for the cache babel and found https://github.com/facebook/jest/blob/6ee2a14b83393c9e3e3408beb5c4848489f04cf6/docs/Troubleshooting.md. Running jest with --no-cache had it work. I then found How to clear Jest cache?. So I cleared the cache and the issue went away entirely.

Filed a possible bug or docs issue at https://github.com/facebook/jest/issues/5796

Dave Stein
  • 8,653
  • 13
  • 56
  • 104