question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug?] Why moduleFileExtensions: [json] causes transformation error (Unexpected token import)?

See original GitHub issue

Here 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:closed
  • Created 5 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ahnpnlcommented, Sep 21, 2018

your latest changes in your repo worked for me. I think you need to run yarn jest --clearCache first. Jest caches your configuration.

1reaction
ahnpnlcommented, Sep 21, 2018

also when I move the config to js file, run yarn jest --clearCache and then yarn test it also works for me. I think first of all before you change tsconfig.spec.json, it failed with the error unexpected token import. After that jest already cached your configuration so when you change tsconfig.spec.json it didn’t reflect to the new changes.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found