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.

Invariant can't be imported in TypeScript under jest vs metro-bundler/packager (in react-native)

See original GitHub issue

Using invariant imported with the following two patterns fails opposite depending on how we run the build (either as unit-tests under jest, or as a react-native view on android).

import * as invariant from "invariant";
import invariant from "invariant";

Swapping the import from one to the other enables/disables using invariant in the opposite target.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

3reactions
bradleyayerscommented, Feb 13, 2018

Prior to TypeScript 2.7, CommonJS modules that exported a function as the module (i.e. module.exports = function () { … }) could not be imported as an ES module. This meant that it was not possible to import this type of modules (“callable CommonJS module”) using ES module import syntax (e.g. import foo from 'foo' or import * as foo from 'foo').

This changed in TypeScript 2.7 with the introduction of --esModuleInterop that enables TypeScript modules to import “callable CommonJS modules” using ES module default import syntax.

This boils down to:

  • If you’re using TS 2.7 or newer with --esModuleInterop this module can be imported via import invariant from 'invariant';
  • Otherwise you must use the import invariant = require('invariant'); syntax.
0reactions
andrezsanchezcommented, Mar 31, 2018

I had to use allowSyntheticDefaultImports: true in my config for import invariant from 'invariant' to work (with TS version 2.7.2).

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Expo Jest TypesScript - Cannot use import ...
I am in the process of converting an expo react-native application to TypeScript and have run into some problems with getting jest tests...
Read more >
invariant - Bountysource
Using invariant imported with the following two patterns fails opposite depending on how we run the build (either as unit-tests under jest, or...
Read more >
ts-jest - npm
ts-jest is a TypeScript preprocessor with source map support for Jest that lets you use Jest to test projects written in TypeScript.
Read more >
Using with React Native | ts-jest - GitHub Pages
To use ts-jest with React Native + TypeScript and Babel 7, you'll first need to follow this tutorial.
Read more >
Mocking in Jest with TypeScript and React
import * as React from "react"; import { render, screen } from "@testing-library/react"; import { User } from "./user"; describe("User component ...
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