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.

Imports broken in babel-jest when using babel and @babel/typescript

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? It appears that Jest doesn’t play nice with TypeScript that’s compiled exclusively via Babel – e.g. without tsc.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

I’ve got a minimal repro here: https://github.com/japhar81/JestRepro

#3202 indicates that the issue is with {"modules":false}. Indeed, yarn test with that setting yields an error; SyntaxError: Unexpected token import. Removing that setting (as in the repro code), yields a different error TypeError: (0 , express) is not a function.

Digging further lead me to this issue: https://github.com/Microsoft/TypeScript/issues/5458 – and I can confirm that if I change my import to be import express from 'express', things work, though of course TypeScript is angry as there’s no real default export in the express definition.

https://github.com/facebook/jest/issues/3202#issuecomment-318260688 references using

"plugins": [
        "transform-es2015-modules-commonjs",
        "dynamic-import-node"
      ]

Adding those in this same repo has no effect. The error remains TypeError: express is not a function. It’s possible these are no longer valid for Babel 7 and different plugins should be used – I’ve had no luck identifying if that’s the case.

What is the expected behavior? There should be some way to keep TypeScript happy with import * as express from 'express' and still have Jest operate. As yarn start demonstrates in this sample, it does compile and work with {"modules":false} in webpack. I’m not entirely sure where the difference is, but presumably if Webpack can do it, Jest can as well.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system. Mac OS X 10.13.3 Yarn: 1.3.2 npm: 5.6.0 node: 9.3.0 Jest: 22.2.2 See Repo for Config

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
casey-speercommented, May 21, 2018

It appears that jest won’t look for typescript files to compile with babel by default, so in addition to any other typescript-related config in jest, I had to add

"transform": {
    "^.+\\.ts$": "babel-jest"
}

to my jest config in package.json.

Also, I had to npm install @babel/core babel-core@7.0.0-bridge.0 to get it working with babel 7 (needed for babel transpilation of typescript).

1reaction
AndrewSouthpawcommented, Feb 17, 2018

yarn --check-files notes an incorrect peer dependency between babel-jest and babel-core

yarn install v1.0.2
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
warning "ajv-keywords@2.1.1" has incorrect peer dependency "ajv@^5.0.0".
warning "ajv-keywords@1.2.0" has incorrect peer dependency "ajv@>=4.9.0".
warning "babel-jest@22.2.2" has incorrect peer dependency "babel-core@^6.0.0 || ^7.0.0-0".
warning "request-promise-native@1.0.5" has incorrect peer dependency "request@^2.34".
warning "request-promise-core@1.1.1" has incorrect peer dependency "request@^2.34".

Otherwise it’s fine. I’ve tried doing a fresh install of node_modules multiple times now. 😕 Any other ideas?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is this transpilation issue correctly filed against babel-jest?
You are introducing a dependency cycle in your code by adding the new import { INITIAL_APPSTATE } from '../../logic'. import in Button.tsx ....
Read more >
babel/preset-typescript
Replace the function used when compiling JSX fragment expressions. This is so that we know that the import is not a type import,...
Read more >
Adding TypeScript Support to a Babel React App - Medium
In this post, I'm going to walk through all of the steps we used to upgrade three of our applications so that we...
Read more >
babel-jest | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
babel-jest - Awesome JS
[babel-plugin-jest-hoist] Support imported jest in mock factory (#13188) ... [jest-config] Fix testing multiple projects with TypeScript config files ...
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