max*_*pre 6 typescript reactjs jestjs babeljs react-native
似乎每个人和他们的母亲都遇到了这个问题的变体。我从所有 SO 问题和 GH 票证中尝试过的一切都没有任何效果。
它实际上应该很简单,因为我的项目几乎是一个新的准系统项目。然而,我仍然无法弄清楚我的生活有什么问题。
当我运行时jest:
/Desktop/Dropbox/Programming/iphone-app/fe/App.spec.tsx:11
const tree = react_test_renderer_1.default.create(<App_1.default />).toJSON();
^
SyntaxError: Unexpected token <
Run Code Online (Sandbox Code Playgroud)
我的配置文件:
// jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
Run Code Online (Sandbox Code Playgroud)
——
// tsconfig.json
{
"compilerOptions": {
"allowJs": false,
"allowSyntheticDefaultImports": true,
"noFallthroughCasesInSwitch": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"isolatedModules": true,
"jsx": "react-native",
"lib": [
"es6"
],
"moduleResolution": "node",
"noEmit": true,
"strict": true,
"target": "esnext"
},
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)
——
// babel.config.js
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
};
};
Run Code Online (Sandbox Code Playgroud)
编辑#1
为了清楚起见,我尝试使用react-nativejest 配置文件中的预设,但没有成功(同样的错误):
// jest.config.js
module.exports = {
preset: 'react-native',
transform: {
'^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
'^.+\\.tsx?$': 'ts-jest'
},
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json'
}
},
testEnvironment: 'node',
};
Run Code Online (Sandbox Code Playgroud)
它与此配置文件一起使用:
const { defaults } = require('jest-config');
module.exports = {
preset: 'react-native',
transform: {
'^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
'^.+\\.tsx?$': 'ts-jest'
},
moduleFileExtensions: [
'tsx',
...defaults.moduleFileExtensions
],
};
Run Code Online (Sandbox Code Playgroud)
我需要添加
...
moduleFileExtensions: [
'tsx',
...defaults.moduleFileExtensions
],
...
Run Code Online (Sandbox Code Playgroud)
否则import App from './App';我的测试中的 会解析为其他文件。通过添加tsx为选项中的最高优先级moduleFileExtensions,应用程序可以解析正确的文件。
还需要将jsx编译器选项tsconfig.json设置为"jsx": "react". 我不知道为什么我还不能设置它react-native,但现在一切似乎都有效。
编辑
'^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js'在较新版本的react-native. 保留它也可能会导致问题。
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |