- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 React 项目,正在尝试设置一些测试
由于以下设置指南/问题:
我已经安装了以下依赖项:
我的 package.json 包含以下 Jest 配置:
"jest": {
"bail": false,
"collectCoverage": true,
"collectCoverageFrom": [
"<rootDir>/src/**/*.js",
"!<rootDir>/node_modules/**"
],
"coverageDirectory": "coverage",
"coveragePathIgnorePatterns": [
"<rootDir>/node_modules"
],
"coverageThreshold": {
"global": {
"branches": 50,
"functions": 50,
"lines": 50,
"statements": 50
}
},
"globals": {
"SOURCEMAP": true
},
"modulePaths": [
"<rootDir>/src",
"<rootDir>/sass",
"<rootDir>/public",
"<rootDir>/node_modules"
],
"resetModules": true,
"testPathDirs": [
"<rootDir>/test"
],
"testRegex": "(/test/.*|\\.(test|spec))\\.(js|jsx)$",
"verbose": true
}
我将组件 (GenericButton) 放置在 <rootDir>/components/buttons/GenericButton.js
中和我在 <rootDir>/test/componnets/buttons/GenericButtonTest.js
中的测试内容如下:
import React from 'react';
import GenericButton from 'components/buttons/GenericButton';
import { shallow } from 'enzyme';
import { shallowToJson } from 'enzyme-to-json';
import ReactTestUtils from 'react-addons-test-utils'
describe('Generic Button', () => {
test('Button action called when clicked', () => {
var clicked = false;
const component = shallow(
<GenericButton action={() => {
clicked = true;
}}/>
);
console.log("Clicking the button!");
ReactTestUtils.Simulate.click(component);
expect(clicked).toBeTruthy();
});
})
我的babelrc文件如下:
{
"presets": [
"es2015-loose",
"react",
"stage-0"
],
"plugins": [
"babel-root-slash-import",
"transform-decorators-legacy",
"react-hot-loader/babel",
"transform-runtime"
],
"compact": true,
"ignore": [
"/node_modules/(?!react-number-input)"
]
}
但是,当我运行测试时,出现以下错误:
> admin-console@4.1.0 test C:\path\to\project
> jest
FAIL test\components\buttons\GenericButtonTest.js
● Test suite failed to run
Cannot find module 'react/lib/ReactTestUtils' from 'index.js'
at Resolver.resolveModule (node_modules\jest-resolve\build\index.js:144:17)
at Object.<anonymous> (node_modules\react-addons-test-utils\index.js:1:107)
at node_modules\enzyme\build\react-compat.js:128:19
----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files | Unknown | Unknown | Unknown | Unknown | |
----------|----------|----------|----------|----------|----------------|
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 5.843s
Ran all test suites.
console.error node_modules\enzyme\build\react-compat.js:131
react-addons-test-utils is an implicit dependency in order to support react@0.13-14. Please add the appropriate version to your devDependencies. See https://github.com/airbnb/enzyme#installation
npm ERR! Test failed. See above for more details.
我该怎么做才能通过这个简单的测试?
编辑:react-addons-test-utils 需要的文件 react/lib/ReactTestUtils 不在 node_modules/react/lib 文件夹中。
这是通过将 react-addons-test-utils 升级到 15.4.0-rc.3 来解决的,但后来我得到了:
TypeError: Cannot read property '__reactInternalInstance$t476n6b4jes0zxw0n18vbzkt9' of undefined
at getClosestInstanceFromNode (node_modules\react-dom\lib\ReactDOMComponentTree.js:106:11)
at Object.getInstanceFromNode (node_modules\react-dom\lib\ReactDOMComponentTree.js:140:14)
at Object.click (node_modules\react-dom\lib\ReactTestUtils.js:326:74)
at Object.<anonymous> (test\components\buttons\GenericButtonTest.js:17:67)
最佳答案
目前您不能在测试中同时使用 ReactTestUtils
和 enzyme
。幸运的是,您也可以使用 enzyme 来模拟点击。唯一的问题是 simulate
不会传播到子组件,因此您需要先找到子组件并对其调用 simulate
。
import React from 'react';
import GenericButton from 'components/buttons/GenericButton';
import { shallow } from 'enzyme';
import { shallowToJson } from 'enzyme-to-json';
describe('Generic Button', () => {
test('Button action called when clicked', () => {
var clicked = false;
const component = shallow(
<GenericButton action={() => {
clicked = true;
}}/>
);
component.find('selectrorOfChildWithClickHandler').first().simulate('click')
expect(clicked).toBeTruthy();
});
})
关于javascript - 设置 Jest 和 Enzyme 来测试 React 15 找不到模块 react/lib/ReactTestUtils,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40466134/
根据 g++ -print-search-dirs,我的 C++ 编译器正在许多目录中搜索库,包括 ... /lib/../lib/: /usr/lib/../lib/: /库/: /usr/lib/
我在 GLUT 演示的顶部看到了代码,我很好奇它的功能。 为什么有人要编写#pragma 而不是只包含库? 最佳答案 此 pragma 允许库作者根据一系列可在编译时分析的标准定义库导入。例如,您可以
Intel describes libm.lib 作为静态库,libmmt.lib 作为多线程静态库 (/MT) 和 libmmds.lib 作为多线程静态库 (/MD) 都实现了 LibM Math
我在 Windows 7 32 位上使用 Visual Studio 2010。 我在 http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-
我正在尝试将一些 html 转换为 pdf。经过一些谷歌搜索后,我发现 dompdf但是当我尝试转换时,我会检索 PHP-font-lib must either be installed via c
目前我的visual studio基本上是在生成Engine.dll和Game.exe Engine.dll 链接到其他一些基本库,例如:d3dx9d.libComCtl32.libWinMM.lib
我正在尝试将 material-ui 用于项目。使用它时会抛出以下错误: 我曾尝试通过 github 存储库解决类似问题,但没有任何运气。任何人都有任何想法可能是什么问题?我正在添加 package.
为了简化,我有两个库:libA.lib、libB.lib libA 具有以下功能: void read(num,*val){ *val=Globalval[num]; ... } libB: void
我有一个包含两个项目的 VS2010 解决方案。第一个项目 [DevLib] 是一个库,它基本上是另一个我们没有源代码的库 [ExtLib] 的包装器。构建这个库工作正常。 第二个项目 [TestAp
我想用我做了一些小更改的系统库替换它。这两个库都是使用相同版本的cyanogenmod 源代码编译的。我复制到 SD 卡的那个可以在早期的 CM 闪存上运行。 现在我尝试运行以下命令: $ adb s
我正在使用源代码中的Postgres 13(Rel_13_STRATE分支),并且我使用的是来自apachea/age源代码的(Release/PG13/1.3.0分支)中的1.3.0版的Apache
看起来 Jetty 团队想要在版本 6 和 7 之间进行一些 Spring 清理,并且看起来好像一个有用的系统属性“jetty.lib”不存在、不起作用,或者只是在未指定的情况下发生了更改以便使我的
我正在尝试使用 unixODBC 连接到 Oracle,但它不起作用。到目前为止,我已经尝试关注 How do I setup Oracle ODBC drivers on RHEL 6/Linux
我在我的 Linux Mint 14 Nadia 中安装了 Matlab(a uname -a 显示:Linux Ideapad-Z570 3.5.0-17-generic#28-Ubuntu SMP
我有一个需要使用 macdeployqt 设置的应用程序,但是当我调用它时,它告诉我它无法在/usr/lib/目录中找到一些库。 在调用 macdeployqt 之前,我必须将 3 个库 (.tx)
此特定问题是由于指定对路径中包含空格的 lib 文件的依赖项引起的。路径需要用引号括起来,项目才能正确编译。 在项目属性的Configuration Properties -> Linker -> I
我正在努力尝试让 OpenCV 2.2 库与我的项目(运行 VSC++,2010)一起工作。我按照 http://opencv.willowgarage.com/wiki/VisualC%2B%2B
总而言之,我想要的是避免在多个 .dll 中出现重复的 .lib 文件,因为生成的 .dll 将在一个 .exe 文件中一起使用,这会使 .lib 文件中的全局状态重复。 具体问题: 我有一个包含 3
我正在尝试使用 Intel TBB 和 IPP 构建 OpenCV v2.3.1。我使用 CMake 生成 Visual Studio 2010 解决方案。构建成功,我在输出中有 opencv_cor
我不明白 use local::lib 经常做 use lib 没有。有人可以解释一下吗? 最佳答案 常规 use lib foo几乎就像这样简单: BEGIN { unshift(@INC, foo
我是一名优秀的程序员,十分优秀!