6

环境描述:

我必须创建一个 react-native 组件并将其发布到 npm。在我的过程中,我必须计划执行单元测试,但经过多次尝试,我无法为我的项目配置 jest。

请参阅下面我的 .Therepackage.json没有依赖项,但只有devDependenciespeerDependencies

    "scripts": {
        "test": "jest",
        "test-cov": "jest --coverage=true",
    },
    "peerDependencies": {
        "react": "^16.13.0",
        "react-native": "^0.63.0"
    },
    "devDependencies": {
        "eslint": "^7.6.0",
        "eslint-config-airbnb-base": "^14.2.0",
        "eslint-config-prettier": "^6.11.0",
        "eslint-plugin-import": "^2.22.0",
        "eslint-plugin-jest": "^23.20.0",
        "eslint-plugin-prettier": "^3.1.4",
        "prettier": "^2.0.5",
        "react": "16.13.1",
        "react-native": "^0.63.1",
        "@babel/core": "^7.11.4",
        "@babel/runtime": "^7.11.2",
        "babel-jest": "^26.3.0",
        "jest": "^26.4.2",
        "metro-react-native-babel-preset": "^0.63.0"
    },
    "jest": {
        "preset": "react-native"
    },

我还有一个.babelrc包含以下内容的文件:

{
    "presets": ["module:metro-react-native-babel-preset"]
}

在我的src/文件夹中,我使用es6语法来导入我的其他文件或 react-native 的组件,如下所示:

import { Dimensions } from 'react-native';

import { colors } from './colors';

问题:

当我运行命令时,npm run test我总是出现这个错误:

 FAIL  __tests__/index.test.js
  ● Test suite failed to run

    SyntaxError: C:\Users\flori\dev\phf_components\phf-native-style\node_modules\react-native\Libraries\polyfills\error-guard.js: Unexpected token, expected ";" (14:5)

      12 | let _inGuard = 0;
      13 | 
    > 14 | type ErrorHandler = (error: mixed, isFatal: boolean) => void;
         |      ^
      15 | type Fn<Args, Return> = (...Args) => Return;
      16 | 
      17 | /**

      at Parser._raise (node_modules/@babel/parser/lib/index.js:766:17)
      at Parser.raiseWithData (node_modules/@babel/parser/lib/index.js:759:17)
      at Parser.raise (node_modules/@babel/parser/lib/index.js:753:17)
      at Parser.unexpected (node_modules/@babel/parser/lib/index.js:8966:16)
      at Parser.semicolon (node_modules/@babel/parser/lib/index.js:8948:40)
      at Parser.parseExpressionStatement (node_modules/@babel/parser/lib/index.js:11970:10)
      at Parser.parseStatementContent (node_modules/@babel/parser/lib/index.js:11566:19)
      at Parser.parseStatement (node_modules/@babel/parser/lib/index.js:11430:17)
      at Parser.parseBlockOrModuleBlockBody (node_modules/@babel/parser/lib/index.js:12012:25)
      at Parser.parseBlockBody (node_modules/@babel/parser/lib/index.js:11998:10)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.504 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! phf-native-style@1.0.3 test: `jest`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the phf-native-style@1.0.3 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\flori\AppData\Roaming\npm-cache\_logs\2020-08-24T09_00_35_268Z-debug.log

你有什么想法可以帮助我吗?谢谢

编辑1:

我通过将我在 json 中的 jest 配置转换为jest.config.js具有以下内容的 js 文件来解决我的问题:

module.exports = {
  preset: 'react-native',
  transform: {
    '^.+\\.js$': require.resolve('react-native/jest/preprocessor.js'),
  },
};
4

0 回答 0