reactjs - 无法在 Jest 中测试反应原生组件
问题描述
当我尝试运行此测试时,我收到了这个烦人的错误消息:
import React from "react";
import renderer from "react-test-renderer";
import App from "../App";
describe("<App />", () => {
it("has 1 child", () => {
const tree = renderer.create(<App />).toJSON();
expect(tree.children.length).toBe(1);
});
});
错误是:
FAIL __test__/App.test.js
✓ works (4ms)
<App />
✕ has 1 child (17ms)
● <App /> › has 1 child
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
我在这里找到了解决方案https://github.com/expo/expo/issues/5268但那个对我不起作用。我的package.json
样子是这样的:
"devDependencies": {
"@types/react": "^16.8.23",
"@types/react-native": "^0.57.65",
"babel-preset-expo": "^6.0.0",
"jest": "^24.8.0",
"jest-expo": "^34.0.0",
"react-dom": "^16.8.6",
"react-test-renderer": "^16.8.6"
},
"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|sentry-expo|native-base)"
]
},
解决方案
推荐阅读
- hyperledger-fabric - 创建同意者时出错:无法恢复持久的 raft 数据:无法创建或读取 WAL:无法打开 WAL:fileutil:文件已锁定
- flutter - 为什么我的 Navigator.push,在抽屉里没有上下文?
- php - 浏览器呼叫代理会议
- sql - 将两个不同文件中的 XML 放入一个 SQL 表中
- android - 添加 jar 库时出错。什么可能出错?
- javascript - 如何在 Angular 中实现 SYNC 方法,等待响应,无需订阅,就像 C# 中的 SYNC 方法一样?
- c++ - std::stoi 的基本参数
- amazon-web-services - 将 Tensroflow 库部署为 AWS Lambda 层
- html - 如何在 ng2smart 表中使用图标而不是按钮
- regex - 如何在属性中找到具有随机数值的 XPATH?