Jest fails with “Unexpected token *” on import statement

前端 未结 5 598
南旧
南旧 2021-02-03 18:15

Why does Jest fail with \"Unexpected token *\" on a simple import statement...

Error log:

Admin@Admin-PC MINGW32 /d/project (master)
$ npm         


        
相关标签:
5条回答
  • 2021-02-03 18:25

    I am using react-native-web and the fix for my issue was to add the react-native-web preset to my jest.config.js:

    module.exports = {
    transform: {
      '^.+\\.tsx?$': 'ts-jest',
    },
    timers: 'fake',
    testPathIgnorePatterns: [
      '<rootDir>/build/',
      '<rootDir>/node_modules/',
      '<rootDir>/rndemo/build/',
    ],
    globals: {
      'ts-jest': {
        diagnostics: {
          warnOnly: true,
        },
      },
    },
    preset: 'react-native-web',
    }
    
    0 讨论(0)
  • 2021-02-03 18:30

    I had a similar issue on a React + Typescript app.

    The first mistake I made was to define the jest.config.js as jest.config.ts

    Running on Node v12.latest

    Then the configuration that worked for me were the following:

    // jest.config.js
    
    module.exports = {
      preset: "ts-jest",
      testEnvironment: "node",
      roots: ["./src"],
      transform: { "\\.ts$": ["ts-jest"] },
      testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
      moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
      globals: {
        "ts-jest": {
          tsConfig: {
            // allow js in typescript
            allowJs: true,
          },
        },
      },
    };
    
    
    
    // tsconfig.json
    {
      "compilerOptions": {
        "target": "es5",
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react",
        "baseUrl": "."
      },
      "include": ["src"],
      "exclude": ["node_modules", "**/*.spec.ts"]
    }
    
    
    // package.json
    "devDependencies": {
        "@types/jest": "^26.0.5",
        "jest": "^26.1.0",
        "ts-jest": "^26.1.3"
    }
    
    
    0 讨论(0)
  • 2021-02-03 18:41

    As it has been said, some modules needs to be transpiled, & some don't. Here is a regex I use that work in a lot of projects

      "jest": {
        "preset": "react-native",
        "transformIgnorePatterns": [
          "node_modules/(?!(jest-)?react-native|react-(native|universal|navigation)-(.*)|@react-native-community/(.*)|@react-navigation/(.*)|bs-platform|(@[a-zA-Z]+/)?(bs|reason|rescript)-(.*)+)"
        ]
      }
    

    It's working for most common react native thing, & include also a specific package (here bs-platform) as an example, when isn't captured by previous patterns.

    0 讨论(0)
  • 2021-02-03 18:46

    Some react-native libraries ship uncompiled ES6 code.

    ES6 code needs to be compiled before it can be run by Jest.

    The Jest doc about Testing React Native Apps includes a section about compiling dependencies that don't ship pre-compiled code.

    You will need to tell Jest to compile react-navigation-tabs by whitelisting it in the transformIgnorePatterns option in your Jest config.

    Example:

    Changing the jest.config.js file into something like below, fixed the issue mentioned in OP.

    But the "react-native-reanimated" module (which requires native integration, as described in another post) needs further work, and we should Mock modules with such native requirements...

    module.exports = {
      preset: 'react-native',
      moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
      transformIgnorePatterns: [
        "node_modules/(?!(react-native"
          + "|react-navigation-tabs"
          + "|react-native-splash-screen"
          + "|react-native-screens"
          + "|react-native-reanimated"
        + ")/)",
      ],
    }
    

    Note: the transformIgnorePatterns option (which is an array of Regular-Expressions) is originally meant to exclude files from being compiled, but using (?!(some-dir-name|another-name)) pattern (the (?!...), negative look-ahead), we do tell Jest to exclude anything in node_modules directory, except the names that we did specify.

    0 讨论(0)
  • 2021-02-03 18:47

    Somewhere in your config file (.babelrc.js or package.json) you have to have "modules" under "presets" set to one of the "amd" | "umd" | "systemjs" | "commonjs" | "cjs" | "auto" | false.

    referer this fragment from the documentation

    something like this:

        "presets": [
      [
        "@babel/preset-env", {
          "targets": process.env.BABEL_TARGET === 'node' ? {
            "node": 'current'
          } : {
            "browsers": [ "last 2 versions" ]
          },
          "loose": true,
          "modules": 'commonjs'
        }
      ]
    ]
    
    0 讨论(0)
提交回复
热议问题