[bug?] Why moduleFileExtensions: [json] causes transformation error (Unexpected token import)?
See original GitHub issueHere are weird behaviors happening in my environment.
At first, I tried to set up my Angular CLI project to use jest-preset-angular, but it didn’t work.
/Users/laco/Works/angular-jest-example/src/setupJest.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import 'jest-preset-angular';
^^^^^^
SyntaxError: Unexpected token import
My src/setupJest.ts
is only one line.
import 'jest-preset-angular';
And my jest.config.js
is very simple. Other files are never modified from Angular CLI default.
module.exports = {
preset: 'jest-preset-angular',
setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts'
};
But when I added moduleFileExtensions
to jest.config.js
, it works well.
module.exports = {
preset: 'jest-preset-angular',
moduleFileExtensions: ['ts', 'js', 'html'],
setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts'
};
Notable point is that moduleFileExtensions
must not include json
.
In jest-preset.json
, moduleFileExtensions
is defined as below.
"moduleFileExtensions": [
"ts",
"js",
"html",
"json"
],
As well as that, if I add json
to my moduleFileExtensions
, it doesn’t work.
module.exports = {
preset: 'jest-preset-angular',
moduleFileExtensions: ['ts', 'js', 'html', 'json'],
setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts'
};
json
causes a transformation error. It’s very weird.
My question:
- Why
json
causes a transformation error? - Is my workaround (
moduleFileExtensions: ['ts', 'js', 'html']
) bad solution?
Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Node.js - SyntaxError: Unexpected token import - Stack Overflow
Seems Node v13.2.0 can't import or import() ES modules as '.js' except when there's a package.json with type: module somewhere above it.
Read more >syntaxerror unexpected token 'export' jest typescript - You.com
This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS file with TS syntax, or it...
Read more >jest-preset-angular - npm
Unexpected token [import|export|other] ... This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS file ...
Read more >Troubleshooting | jest-preset-angular - GitHub Pages
You can update your tsconfig.spec.json to include the ... Unexpected token [import|export|other] ... A typical Jest error is like this:.
Read more >babel-jest | Yarn - Package Manager
Jest plugin to use babel for transformation. readme. babel-jest. Babel jest plugin. Usage. If you are already using jest- ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
your latest changes in your repo worked for me. I think you need to run
yarn jest --clearCache
first. Jest caches your configuration.also when I move the config to
js
file, runyarn jest --clearCache
and thenyarn test
it also works for me. I think first of all before you changetsconfig.spec.json
, it failed with the error unexpected token import. After thatjest
already cached your configuration so when you changetsconfig.spec.json
it didn’t reflect to the new changes.