添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Jest: Module import syntax error #2103

Closed
Closed

Description

What version of React, ReactDOM/React Native, Redux, and React Redux are you using?

  • react: 18.2.0
  • react-native: 0.72.4
  • redux: 5.0.0
  • react-redux: 9.0.1
  • jest: 29.7.0
  • babel-jest: 29.7.0
  • What is the current behavior?

    So this is probably not a Redux issue per se but since this is a popular project and this just came up with the new update I'm hoping somebody else has run into this and may offer some help. I've been dealing with this exact issue in other projects and was not able to resolve it.

    When running unit tests with Jest I'm seeing the following error after upgrading to react-redux v9.0.0.

    .../node_modules/react-redux/dist/react-redux.react-native.mjs:2
        import * as ReactOriginal from "react";
        ^^^^^^
        SyntaxError: Cannot use import statement outside a module
           8 | } from 'react';
           9 | import { AppState, Linking } from 'react-native';
        > 10 | import { useSelector } from 'react-redux';
          11 | import {
          12 |      LinkingOptions,
          13 |      createNavigationContainerRef,
          at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
          at Object.require (src/navigation/root/RootNavigator.tsx:10:1)
    

    jest.config.js:

    module.exports = {
    	preset: 'react-native',
    	moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'mjs', 'json', 'node'],
    	transformIgnorePatterns: ['<roodDir>/__mocks__', 'jest-runner'],
    	setupFiles: [
    		'<rootDir>/jest.setup.js',
    		'./node_modules/react-native-gesture-handler/jestSetup.js',
    		'dotenv/config',
    	moduleNameMapper: {
    		'\\.(jpg|jpeg|svg|png)$': '<rootDir>/__mocks__/fileMock.js',
    

    Anybody able to share a working Jest config?

    What is the expected behavior?

    Expecting Jest unit tests to be able to parse all modules.

    Which browser and OS are affected by this issue?

    No response

    Did this work in previous versions of React Redux?

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Assignees

    No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions