我尝试使用jest(v19.0.2)w / my react native(v0.38.0)项目但是当我运行jest命令时,我收到了以下错误:
● Test suite failed to run
/Users/kyledecot/code/root-react-native/node_modules/react-native/jest/setup.js:40
)
^
SyntaxError: Unexpected token )
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
at handle (node_modules/worker-farm/lib/child/index.js:41:8)
at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
at emitTwo (events.js:106:13)
这是他们抱怨的文件:
30 .mock('ReactNativeDefaultInjection')
31 .mock('Image', () => mockComponent('Image'))
32 .mock('Text', () => mockComponent('Text'))
33 .mock('TextInput', () => mockComponent('TextInput'))
34 .mock('Modal', () => mockComponent('Modal'))
35 .mock('View', () => mockComponent('View'))
36 .mock('ScrollView', () => mockComponent('ScrollView'))
37 .mock(
38 'ActivityIndicator',
39 () => mockComponent('ActivityIndicator'),
40 )
41 .mock('ListView', () => {
42 const RealListView = require.requireActual('ListView');
43 const ListView = mockComponent('ListView');
44 ListView.prototype.render = RealListView.prototype.render;
45 return ListView;
46 })
47 .mock('ListViewDataSource', () => {
48 const DataSource = require.requireActual('ListViewDataSource');
49 DataSource.prototype.toJSON = function() {
50 function ListViewDataSource(dataBlob) {
是否还有其他人遇到此错误或知道如何修复它?
答案 0 :(得分:0)
我将近一年后回答这个问题所以我无法验证此修复程序是否适用于这些版本,但我遇到了与React Native 53,Jest 21和TypeScript 2.7相同的问题
我的解决方案有3个部分:
package.json
,将以下内容添加到您的jest配置中:```
{
jest: {
"moduleFileExtensions": ["ts", "tsx", "js", "json"],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.(ts|tsx)$": "<rootDir>/preprocessor.js"
},
}
}
```
preprocessor.js
文件(请注意使用现有的tsconfig.json
文件):```
const tsc = require('react-native-typescript-transformer');
const tsConfig = require('./tsconfig.json');
module.exports = {
process(src, filename) {
if (filename.match(/\.png/)) {
return '';
}
if (filename.endsWith('.ts') || filename.endsWith('.tsx')) {
return tsc.transform(src, filename, tsConfig.compilerOptions);
}
return src;
},
};
```
npm install --S react-native-typescript-transformer
安装变压器库