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.

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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:27 (19 by maintainers)

github_iconTop GitHub Comments

36reactions
akxcommented, Nov 29, 2018

esModuleInterop": true was the key for me. Thank you @diegoddox!

1reaction
diegoddoxcommented, Nov 5, 2018

@blujedis I can’t remember exactly since was a while ago but here is the new config.

"jest": {
    "preset": "react-native",
    "verbose": true,
    "setupFiles": [
      "./setupJest.js"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-navigation)"
    ],
    "testPathIgnorePatterns": [
      "build",
      "tests",
      "node_modules/"
    ],
    "cacheDirectory": ".jest/cache",
    "transform": {
      "^.+\\.js$": "babel-jest",
      ".+\\.(css|png)$": "jest-transform-stub",
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "moduleFileExtensions": [
      "ts",
      "js",
      "jsx",
      "tsx",
      "json",
      "node"
    ],
    "globals": {
      "__DEV__": true,
      "ts-jest": {
        "useBabelrc": true,
        "tsConfigFile": "tsconfig.test.json"
      }
    }
  }

tsconfig.test.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TSConfig Option: allowSyntheticDefaultImports - TypeScript
This option brings the behavior of TypeScript in-line with Babel, where extra code is emitted to make using a default export of a...
Read more >
error TS1192: Module '" A.module"' has no default export
For me this issue was addressed using "allowSyntheticDefaultImports": true within compilerOptions in the project's tsconfig.json. Our specific error related ...
Read more >
Getting Started with (and Surviving) Node.js ESM
json:exports field do? How do I import CommonJS in ESM? How do I import ESM in CommonJS? Fig. 1 - Importing overview ...
Read more >
Content Types - ESBuild
When code in the ESM format that has a default export is converted to the CommonJS format, and then that CommonJS code is...
Read more >
Understanding (all) JavaScript module formats and tools
g-force: 豚鼠特工队 ... By default, each .js file is a CommonJS module. ... ES module's named import/export and default import/export:.
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