我已经努力了几天,以便使用 expo + typescript + jest + ts-jest 为简单的 react-native 运行测试。
我已经问过相关问题 here
这是我的项目的设置:
{
"compilerOptions": {
"noEmit": true,
"lib": ["dom", "esnext"],
"jsx": "react-native",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}
module.exports = function(api) {
api.cache(true);
return {
presets: ["babel-preset-expo"]
};
};
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/