我正在尝试对现有的 React Native 项目进行测试。当我使用 Jest 运行测试时,测试会出现以下错误:
Test suite failed to run
...etc/__app__/node_modules/react-native/Libraries/polyfills/error-guard.js:14
type ErrorHandler = (error: mixed, isFatal: boolean) => void;
^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (node_modules/react-native/jest/setup.js:16:6)
Run Code Online (Sandbox Code Playgroud)
意外的标识符如下:
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict
* @polyfill
*/
let _inGuard = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Jest 并仅测试渲染,这是我的测试文件:
import "react-native";
import React from "react";
import App from"../src/Root/";
import { create } from "react-test-renderer";
describe("App", () => {
const tree = create(<App />).toJSON();
it("renders correctly", () => {
expect(tree).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
当我运行时yarn test,出现错误
错误信息:
/dic/node_modules/react-native/Libraries/polyfills/error-guard.js:14
type ErrorHandler = (error: mixed, isFatal: boolean) => void;
^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (node_modules/react-native/jest/setup.js:16:6)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.128 s
Run Code Online (Sandbox Code Playgroud)
babel.config.js
module.exports = {
presets: …Run Code Online (Sandbox Code Playgroud)