Invariant can't be imported in TypeScript under jest vs metro-bundler/packager (in react-native)
See original GitHub issueUsing 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:
- Created 6 years ago
- Reactions:1
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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'
orimport * 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:
--esModuleInterop
this module can be imported viaimport invariant from 'invariant';
import invariant = require('invariant');
syntax.I had to use
allowSyntheticDefaultImports: true
in my config forimport invariant from 'invariant'
to work (with TS version2.7.2
).