Skip to content

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

Closed
@japhar81

Description

@japhar81

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: microsoft/TypeScript#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.

#3202 (comment) 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions