Imports broken in babel-jest when using babel and @babel/typescript
See original GitHub issueDo 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:
- Created 6 years ago
- Comments:12 (4 by maintainers)
Top GitHub Comments
It appears that
jest
won’t look fortypescript
files to compile withbabel
by default, so in addition to any othertypescript
-related config injest
, I had to addto my
jest
config inpackage.json
.Also, I had to
npm install @babel/core babel-core@7.0.0-bridge.0
to get it working withbabel 7
(needed forbabel
transpilation oftypescript
).yarn --check-files
notes an incorrect peer dependency betweenbabel-jest
andbabel-core
…Otherwise it’s fine. I’ve tried doing a fresh install of
node_modules
multiple times now. 😕 Any other ideas?