语法错误:意外令牌)w / Jest&React Native

问题描述 投票:2回答:1

我正在尝试使用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) {

有没有其他人遇到这个错误或知道我将如何修复它?

react-native jestjs babel
1个回答
0
投票

我差不多一年后回答这个问题所以我无法验证这个版本是否可以在这些版本上运行,但我遇到了与React Native 53,Jest 21和TypeScript 2.7相同的问题

我的解决方案有3个部分:

  1. 你的package.json,将以下内容添加到你的jest配置:

```

{
    jest: {
        "moduleFileExtensions": ["ts", "tsx", "js", "json"],
        "transform": {
            "^.+\\.js$": "babel-jest",
            "^.+\\.(ts|tsx)$": "<rootDir>/preprocessor.js"
        },
    }
}

```

  1. 在根文件夹中(参见上面的路径),创建一个包含以下内容的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;
    },
};

```

  1. 使用npm install --S react-native-typescript-transformer安装变压器库
© www.soinside.com 2019 - 2024. All rights reserved.