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.

Tests failing with SyntaxError on import statements

See original GitHub issue
  • Issue

After configuring ts-jest all my tests fail with SyntaxError on import statements, even if they didn’t include any typescript modules and were passing before. If I remove the ts-jest config, the tests that don’t import a typescript module passes fine.

    ...\sum.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Sum from './sum';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token import

      at transformAndBuildScript (node_modules\jest-runtime\build\transform.js:320:12)

Random thought as I post this, all our tests are written in js and so are most of our components. We have a handful of shared components that are written in typescript. ts-jest should work for this scenario right?

  • Expected behavior

Tests that run/pass before configuring ts-jest continue to do so and tests typscript module passes too

  • Link to a minimal repo that reproduces this issue

https://github.com/kwilkins/ts-error-example

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

28reactions
kulshekharcommented, Dec 13, 2017

@kwilkins The js files need to be transpiled for node to understand import. There are two ways you can do this:

  • use ts-jest to process js files
    "transform": {
      "^.+\\.(t|j)sx?$": "ts-jest"
    }
  • use another transformer to process js files
    "transform": {
      "^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.tsx?$": "ts-jest"
    }

let me know if this fixes it for you

sidenode: the jest/ts-jest version in the linked repo is too old

5reactions
erinknight242commented, Dec 18, 2017

I am still getting the “Unexpected token import” after updating the transform to include js files (my tests are js but my components are ts/tsx)

Example failure: ` Test suite failed to run

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
                                                                                         ^^^^^^

SyntaxError: Unexpected token import

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)`
Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Cannot use import statement outside a module ...
This is a React, Typescript, Webpack project. I am trying to test a module. But Jest won't transform the module to plain javascript...
Read more >
SyntaxError: Cannot use import statement outside a module
I ran 'yarn rw test' and i output from the testing. called “SyntaxError: Cannot use import statement outside a module”.
Read more >
jest cannot use import statement outside a module - You.com
I'm trying to set up unit testing in Vue using Jest. However, I'm getting this error: Test suite failed to run ... SyntaxError:...
Read more >
Jest vs “import statement outside a module” - David Votrubec
As the name suggests, lodash-es uses ES6 modules syntax. Jest does not like it, and the unit tests will fail with syntax error....
Read more >
Cannot use import statement outside a module (phpStorm/Js ...
SyntaxError: Cannot use import statement outside a module (phpStorm/Js/Jest) Follow ... I'm trying to setup phpStorm to run javascript unit-test ...
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