react 原生,开 Jest ,ts-jest : ReferenceError: React is not defined

标签 react-native jestjs expo ts-jest

我已经努力了几天,以便使用 expo + typescript + jest + ts-jest 为简单的 react-native 运行测试。
我已经问过相关问题 here
这是我的项目的设置:

  • tsconfig.json
  •     {
          "compilerOptions": {
            "noEmit": true,
            "lib": ["dom", "esnext"],
            "jsx": "react-native",
            "moduleResolution": "node",
            "allowSyntheticDefaultImports": true,
            "skipLibCheck": true
          }
        }
    
  • babel.config.json
  • module.exports = function(api) {
      api.cache(true);
      return {
        presets: ["babel-preset-expo"]
      };
    };
    
  • jest.config.js(见 official github setting of react-native + ts-jest)
  • const { defaults: tsjPreset } = require("ts-jest/presets");
    module.exports = {
      ...tsjPreset,
      preset: "react-native",
      transform: {
        ...tsjPreset.transform,
        "\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
      },
      globals: {
        "ts-jest": {
          babelConfig: true
        }
      },
      cacheDirectory: ".jest/cache"
    };
    
    

    我收到这个错误
    ReferenceError: React is not defined
    因为我在我的文件中导入这样的 react :import React from 'react'
    如果我像 import * as React from 'react' 这样导入
    有用。

    任何帮助将不胜感激,因为我已经在这个项目中度过了几天。

    最佳答案

    我遇到了完全相同的问题并通过更改我的 tsconfig.json 来修复它从

    {
      "compilerOptions": {
        /* Basic Options */
        "target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
        "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
        "lib": [
          "es6"
        ] /* Specify library files to be included in the compilation. */,
      ...
      }
    }
    


    {
      "compilerOptions": {
        /* Basic Options */
        "target": "es2017" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
        "module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
        "lib": [
          "es2017"
        ] /* Specify library files to be included in the compilation. */,
      ...
      }
    }
    

    关于 react 原生,开 Jest ,ts-jest : ReferenceError: React is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57182197/

    相关文章:

    reactjs - 使用 jest 对 axios post 进行单元测试

    react-native - expo-permissions 返回所有权限的无法识别的权限

    amazon-web-services - 开 Jest 匆忙 map : Haste module naming collision (AWS, RN)

    reactjs - react native : optimizing flatlist render item with useCallback

    android - 无法与 Expo 签署 android apk

    react-native - 在根项目 'installRelease' 中找不到任务 'ReactNativeStarter'。一些候选人是 : 'uninstallRelease'

    javascript - 如何使用 Jest 将 Axios 模拟为默认导出

    javascript - 子类化并扩展 Jest 模拟的 Promise

    react-native - react navigation v5 如何重置或更换?

    reactjs - 不变违规 : Objects are not valid as a React child (found: object with keys {text, onPress})