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.

[TS 3.8] Jest is unable to work with "type-only imports/exports"

See original GitHub issue

🐛 Bug Report

When using a new “type-only impors/exports” feature introduced in TypeScript 3.8 it is impossible to run tests due to the parsing error:

 FAIL  src/test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    SyntaxError: /jest-ts-type-only-export-import-repro/src/test.ts: Unexpected token (3:12)

      1 | import { a } from './exporter';
      2 | 
    > 3 | import type { Foo } from './exporter';
        |             ^
      4 | 
      5 | test("it should parse", () => {
      6 |     const q: Foo = { a };

      at Object.raise (node_modules/@babel/parser/src/parser/location.js:41:63)
      at Object.unexpected (node_modules/@babel/parser/src/parser/util.js:150:16)
      at Object.expectContextual (node_modules/@babel/parser/src/parser/util.js:94:41)
      at Object.parseImport (node_modules/@babel/parser/src/parser/statement.js:2038:12)
      at Object.parseImport (node_modules/@babel/parser/src/plugins/typescript/index.js:1826:20)
      at Object.parseStatementContent (node_modules/@babel/parser/src/parser/statement.js:248:25)
      at Object.parseStatementContent (node_modules/@babel/parser/src/plugins/typescript/index.js:1893:20)
      at Object.parseStatement (node_modules/@babel/parser/src/parser/statement.js:147:17)
      at Object.parseBlockOrModuleBlockBody (node_modules/@babel/parser/src/parser/statement.js:866:25)
      at Object.parseBlockBody (node_modules/@babel/parser/src/parser/statement.js:842:10)

To Reproduce

  • use a type-only export/import in some file
  • run test that imports (directly or indirectly) that file

Expected behavior

Jest should be able to parse type-only exports/imports and run tests without any problems.

Link to repl or repo (highly encouraged)

https://github.com/surgeboris/jest-ts-type-only-export-import-repro

envinfo

  System:
    OS: Linux 5.3 Ubuntu 19.10 (Eoan Ermine)
    CPU: (4) x64 Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
  Binaries:
    Node: 12.13.1 - ~/.npm-global/bin/node
    Yarn: 1.22.0 - ~/.npm-global/bin/yarn
    npm: 6.13.7 - ~/.npm-global/bin/npm
  npmPackages:
    jest: ^25.1.0 => 25.1.0 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
poltakcommented, Jun 9, 2020

Updating the main jest package also to v26.0.1, same as babel-jest, was what fixed it for me. The transform config now automatically points to to node_modules/babel-jest instead of node_modules/jest-config/node_modules/babel-jest.

Maybe that’s obvious, but putting it out there if not.

1reaction
jeysalcommented, Feb 24, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Ask Question - Stack Overflow
import type * as PrettyFormat from './types';. This seems to be the cause of the error. Share.
Read more >
transform-typed-imports - npm package - Snyk
A code-mod which modifies interface/type imports/exports to use the new `import type` feature introduced in TS 3.8. For more information about how to...
Read more >
type-only imports — A new TypeScript feature that benefits ...
Issues arise when Babel can't possibly know if a particular import is a type that should be removed or an actual value that...
Read more >
TSConfig Reference - Docs on every TSConfig option
This only affects modules; global script files will not attempt to import modules. For example, with this TypeScript: ts. export function fn( ...
Read more >
[Solved]-create-react-app error import type * as PrettyFormat ...
yarn add typescript @types/node @types/react @types/react-dom @types/jest ... Actually this "import type" is a new syntax introduced in Typescript 3.8 ...
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