Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6288

Jest (ESM) issue loading files from React Native in unit tests

$
0
0

Running Jest with full ESM support (now available!) and TypeScript using ts-jest as described in thedocs. It's working beautifully well! Until I try to use it with React Native.

In any tests that have RN components, I get an error saying SyntaxError: Cannot use import statement outside a module in node_modules/react-native/index.js (see below). I think this is because without "type": "module" in RN's package.json, Jest can't load it as an ES module with a .js file extension (.ts and .tsx are listed in extensionsToTreatAsEsm, and when I put .js in there it complains that this is controlled by the type field in package.json).

In the same project, tests that don't have RN components are just fine, all being written in TS + ESM and with no Jest or babel transformation (ts-jest only). Running tests with node --experimental-vm-modules node_modules/jest/bin/jest.js.

Any ideas on how to get it working with only certain ES modules with .js file extensions in node_modules? Thanks so much in advance!

Here's a minimal repro.

Have tried using transformIgnorePatterns as described here. Have also tried setting the preset to react-native (this makes it throw errors about "unexpected identifiers" in type declarations, I guess because it's no longer using ts-jest).

Error in Jest:

 FAIL  src/__tests__/native.test.tsx● Test suite failed to run    Jest encountered an unexpected token    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.    By default "node_modules" folder is ignored by transformers.    Here's what you can do:• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.• If you need a custom transformation specify a "transform" option in your config.• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.    You'll find more details and examples of these config options in the docs:    https://jestjs.io/docs/configuration    For information about custom transformations, see:    https://jestjs.io/docs/code-transformation    Details:    /Users/justin/dev/sandbox/jest-esm-react-native/node_modules/react-native/index.js:14    import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo';    ^^^^^^    SyntaxError: Cannot use import statement outside a module

Jest config in package.json:

"jest": {"resetMocks": true,"testEnvironment": "node","testMatch": ["**/src/**/*.(spec|test).[tj]s?(x)"    ],"preset": "ts-jest/presets/default-esm","transform": {},"extensionsToTreatAsEsm": [".ts",".tsx"    ],"globals": {"ts-jest": {"useESM": true      }    }  },

Viewing all articles
Browse latest Browse all 6288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>