Jest won't transform the module - SyntaxError: Cannot use import statement outside a module
Eventhough I have tried them separately, I haven't tried them together (transform
and transformIgnorePatterns
). So this jest configuration solved my issue:
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"transform": {
"node_modules/variables/.+\\.(j|t)sx?$": "ts-jest"
},
"transformIgnorePatterns": [
"node_modules/(?!variables/.*)"
]
},
My mistakes were:
- Not using
transform
andtransformIgnorePatterns
together. - And defining
babel-jest
as the transformer instead ofts-jest
(I guess that is a problem when the preset of jest is defined asts-jest
. Because if I change it to bebabel-jest
it throws the same error again.):
--- "node_modules/variables/.+\\.(j|t)sx?$": "babel-jest"
+++ "node_modules/variables/.+\\.(j|t)sx?$": "ts-jest"