Ts-jest: Tests failing with SyntaxError on import statements

Created on 13 Dec 2017  ·  6Comments  ·  Source: kulshekhar/ts-jest

  • 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

Most helpful comment

@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

All 6 comments

@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

Ah, so everything needs to be handled in the transform config. I figured the regular ole js stuff would just get handled on its own.

When I use ts-jest to process the js files I continue to get errors when using an imported library.
Here trying to use moment and highlight.js

 FAIL  views\foo\reducer.spec.js
  ● Test suite failed to run

    TypeError: moment_1.default is not a function

      at Object.<anonymous> (views/foo/reducer.js:13:32)
      at Object.<anonymous> (views/foo/reducer.spec.js:4:17)
      at next (native)
      at next (native)

 FAIL  views\foo\component.spec.jsx
  ● Test suite failed to run

    TypeError: Cannot read property 'registerLanguage' of undefined

      at Object.<anonymous> (shared/components/HighlightedCode.jsx:10:20)
      at Object.<anonymous> (views/foo/component.jsx:7:21)
      at Object.<anonymous> (views/foo/component.spec.jsx:5:19)
      at next (native)
      at next (native)

However, using babel-jest seems to work perfectly. Not sure if this is something that interests you, I could try to update the repo to reproduce the issue I'm seeing if you'd like.

And yes, the jest/ts-jest versions were really old. I just grabbed the example code from using typescript in the jest github. Our actual project is up to date :)

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)`

This issue was fixed in the minimal repo that was created for it.

@erinknight242 please open another issue (link to this one if that helps) with a minimal repo that demonstrates the problem you're facing. We'll be much better placed to debug and fix the problem that way

@kulshekhar , where is the minimal repo? Is this the one? or could you please provide link to it. I am getting the same issue, have tried all the solutions suggested here and on the ts-jest repo's readme but nothing working for me.

@b-tiwari no, it was the one in the original post (https://github.com/kwilkins/ts-error-example) which doesn't seem to exists any longer

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bySabi picture bySabi  ·  4Comments

golddranks picture golddranks  ·  3Comments

jbreckmckye picture jbreckmckye  ·  3Comments

stangerjm picture stangerjm  ·  4Comments

AlexGellert picture AlexGellert  ·  4Comments