Forced commonjs breaks synthetic default imports
See original GitHub issue- Issue
My React Native code writes imports as follows:
import React from "react";
import RN from "react-native";
...
class MyComponent extends React.Component<{}, {}> {
This works fine in the app, but fails in Jest with the error Cannot read property 'Component' of undefined
. This seems to be because while my app’s tsconfig has "module": "es6"
and relies on Babel to convert the import statements, ts-jest
forces commonjs
which translates this to
const react_1 = require("react");
...
class MyComponent extends react_1.default.Component {
which is of course wrong. Until recently, I worked around this by writing a preprocessor which would run ts-jest
and babel-jest
in sequence, but this has been broken by the change https://github.com/kulshekhar/ts-jest/commit/192b493af8843a2387476d8de9a474067afc229e, which forces commonjs
even if my tsconfig says otherwise.
You might ask why I don’t just write import * as RN from "react-native"
instead? Because of https://github.com/facebook/react-native/issues/12018. Besides, this conceptually makes for a better test in my mind- the app and the test code go through the same transformation pipeline (TypeScript followed by Babel).
- Expected behavior
There should be a way to override the "module": "commonjs"
setting.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:27 (19 by maintainers)
Top GitHub Comments
esModuleInterop": true
was the key for me. Thank you @diegoddox!@blujedis I can’t remember exactly since was a while ago but here is the new config.
tsconfig.test.json