【发布时间】:2019-12-02 13:33:33
【问题描述】:
我已经苦苦挣扎了几天,以通过 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
因为我在我的文件中导入了这样的反应:
import React from 'react'
如果我像 import * as React from 'react' 这样导入
它有效。
任何帮助将不胜感激,因为我已经在这个项目中花费了几天时间。
【问题讨论】:
-
这个有什么锁吗?
标签: react-native jestjs expo ts-jest