The test suite was running just fine until it didn't. The weird thing is happening only in my machine, CI works all right.
Our project uses Typecript, React, Jest and Enzyme and this is what I get when I run yarn jest
:
Test suite failed to run
ReferenceError: enzyme_1 is not defined
6 | // (global as any).jQuery = $
7 |
> 8 | Enzyme.configure({ adapter: new Adapter() })
| ^
9 |
at Object.<anonymous> (spec/javascripts/setupTests.ts:8:1)
I tried cleaning yarn cache, cloning the repo again, reinstalling node. I am absolutely clueless. Any ideas?
Relevant config files:
EDIT:
This error is happening with node 10.19.0
. After installing 12.22.2
the error shows a new message:
ReferenceError: Cannot access 'enzyme_1' before initialization
Apparently there is some inconsistency between the tsconfig
and jest.config
regarding js modules.
In the tsconfig the module resolution is set to Node
and module to ES6
, whereas the jest config file uses the preset ts-jest/presets/js-with-babel
which will transform .ts
files to CommonJS
.
In fact, if setupTests.js
is used instead of setupTests.ts
, the tests run without errors.
Solution A (node oriented):
Set module to CommonJS
in tsconfig.
- "module": "ES6",
+ "module": "CommonJS",
Solution B (web oriented):
Use an appropriate preset, in this case ts-jest/presets/default-esm
to transform .ts
files into ES modules.
- preset: 'ts-jest/presets/js-with-babel',
+ preset: 'ts-jest/presets/default-esm',
Additionally, moduleResolution
should be NodeNext
for ESM, not Node
. See https://www.typescriptlang.org/tsconfig#moduleResolution