我使用webpack开发一个React组件.这是一个简单的版本:
'use strict';
require('./MyComponent.less');
var React = require('react');
var MyComponent = React.createClass({
render() {
return (
<div className="my-component">
Hello World
</div>
);
}
});
module.exports = MyComponent;
Run Code Online (Sandbox Code Playgroud)
现在,我想使用jest测试这个组件.以下是我的相关内容package.json:
"scripts": {
"test": "jest"
},
"jest": {
"rootDir": ".",
"testDirectoryName": "tests",
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"unmockedModulePathPatterns": [
"react"
]
}
Run Code Online (Sandbox Code Playgroud)
运行时npm test,我收到以下错误:
SyntaxError:/Users/mishamoroshko/react-component/src/tests/MyComponent.js:/Users/mishamoroshko/react-component/src/MyComponent.js:/Users/mishamoroshko/react-component/src/MyComponent.less:Unexpected令牌ILLEGAL
看起来webpack需要require('./MyComponent.less')在jest运行测试之前进行处理.
我想知道我是否需要使用像jest-webpack这样的东西.如果是,有没有办法指定多个scriptPreprocessors?(注意我已经使用过babel-jest)
我试图将配置文件传递给Jest,以便仅从给定目录运行测试.
根据文档,您可以使用config.testPathDirs:https://facebook.github.io/jest/docs/api.html#config-testpathdirs-array-string,您可以调用jest --config=<config-file>.
遗憾的是,文档中没有包含配置文件外观的任何描述.
我在一个jest-config.js文件中尝试了这两个选项:
testPathDirs = ['coredata/src'];
Run Code Online (Sandbox Code Playgroud)
和
config.testPathDirs(['coredata/src']);
Run Code Online (Sandbox Code Playgroud)
跑了:
$ jest --config=jest-config.js
Run Code Online (Sandbox Code Playgroud)
...但我得到这种类型的错误:
$ jest --config=jest-config.js
Using Jest CLI v0.4.0
Failed with unexpected error.
/Users/carles/dev/azazo/coredata/node_modules/jest-cli/src/jest.js:179
throw error;
^
SyntaxError: Unexpected token c
at Object.parse (native)
at /Users/carles/dev/azazo/coredata/node_modules/jest-cli/src/lib/utils.js:291:23
at _fulfilled (/Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:798:54)
at self.promiseDispatch.done (/Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:827:30)
at Promise.promise.promiseDispatch (/Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:760:13)
at /Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:574:44
at flush (/Users/carles/dev/azazo/coredata/node_modules/jest-cli/node_modules/q/q.js:108:17)
at process._tickCallback (node.js:419:13)
Run Code Online (Sandbox Code Playgroud) 我有一个React组件,我试图编写一些测试.我把它分解为最简单的测试.
jest.dontMock('../Overlay.react.js');
import React from 'react';
import ReactDOM from 'react-dom';
var Overlay = require('../Overlay.react.js'); // this is the culprit!
describe('Overlay', () => {
it('should work', () => {
expect(true).toEqual(true);
});
});
Run Code Online (Sandbox Code Playgroud)
当我要求测试的组件时,它似乎不是在嘲笑它的子组件.在顶部Overlay.react.js,我有以下导入:import LoadingSpinner from 'loadingIndicator/LoadingIndicatorSpin.react'; 运行我的测试时,我收到以下错误:
- SyntaxError:/Users/dev/work/react-prototype/src/components/root/routes/components/subset1/components/Overlay.react.js:/ Users/dev/work/react-prototype/src/components/root/routes/components/loadingIndicator/LoadingIndicatorSpin.react.js:/Users/dev/work/react-prototype/src/components/root/routes/components/loadingIndicator/sass/style.sass:Unexpected token ILLEGAL
它似乎不是模拟组件,而是直接到子组件的sass文件并抛出适合.我的理解是,除了你告诉它不要嘲笑之外,Jest嘲笑一切.
制定这些测试的正确方法是什么,以便在测试期间导入时子组件不会引起爆炸?
我有一个纯文本文件,可能有数百万行需要自定义解析,我想尽快加载到HBase表中(使用Hadoop或HBase Java客户端).
我目前的解决方案是基于没有Reduce部分的MapReduce作业.我FileInputFormat用来读取文本文件,以便每行传递给map我的Mapper类的方法.此时,该行被解析以形成一个Put写入的对象context.然后,TableOutputFormat获取Put对象并将其插入表中.
该解决方案产生的平均插入速率为每秒1,000行,低于我的预期.我的HBase设置在单个服务器上处于伪分布式模式.
一个有趣的事情是,在插入1,000,000行时,会产生25个Mappers(任务),但它们会连续运行(一个接一个); 这是正常的吗?
这是我当前解决方案的代码:
public static class CustomMap extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
protected void map(LongWritable key, Text value, Context context) throws IOException {
Map<String, String> parsedLine = parseLine(value.toString());
Put row = new Put(Bytes.toBytes(parsedLine.get(keys[1])));
for (String currentKey : parsedLine.keySet()) {
row.add(Bytes.toBytes(currentKey),Bytes.toBytes(currentKey),Bytes.toBytes(parsedLine.get(currentKey)));
}
try {
context.write(new ImmutableBytesWritable(Bytes.toBytes(parsedLine.get(keys[1]))), row);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} …Run Code Online (Sandbox Code Playgroud) 我正在按照Jest教程测试一个react组件,并且遇到了jsx的预处理问题.我假设错误是由于预处理,错误消息不是很有帮助.谷歌搜索显示与旧版本的react/jest相似的错误,这些错误是通过包含/** @jsx React.DOM */docblock来修复的,据我所知,docblock是固定的.
当我运行我的测试时:
Using Jest CLI v0.8.0, jasmine1
FAIL spec/MyComponent_spec.js
Runtime Error
SyntaxError: /Users/asdf/react/stuff-react/spec/MyComponent_spec.js: Unexpected token (13:6)
npm ERR! Test failed. See above for more details.
Run Code Online (Sandbox Code Playgroud)
有问题的行是应该渲染我的组件的行:
jest.dontMock('../src/MyComponent');
let React = require('react');
let ReactDOM = require('react-dom');
let TestUtils = require('react-addons-test-utils');
const MyComponent = require('../src/MyComponent');
describe('MyComponent', function(){
it('render', function(){
var myComponent = TestUtils.renderIntoDocument(
// This is the line referenced in the test error
<MyComponent />
)
var myComponentNode = ReactDOM.findDOMNode(myComponent);
expect(myComponentNode.textContent).toEqual('hi');
});
});
Run Code Online (Sandbox Code Playgroud)
我以为我package.json …
我正在尝试在Jest中模拟一个对象(我创建的),因此我可以在react组件中提供默认行为(因此不使用真正的实现)
这是我的反应组件ChatApp(它很直接)
'use strict';
var React, ChatApp, ChatPanel, i18n;
React = require('react');
ChatPanel = require('./chat_panel');
i18n = require('../support/i18n');
ChatApp = React.createClass({
render() {
return (
<div className="chat-app">
<h1>{i18n.t("app.title")}</h1>
<ChatPanel />
</div>
);
}
});
module.exports = ChatApp;
Run Code Online (Sandbox Code Playgroud)
所以我有一个自定义的I18n依赖项进行翻译(I18n是我写的,它是node-polyglot的包装器).
所以我想做一个基本测试,看看H1中是否有正确的单词,但我不想在我的I18n对象上设置jest.dontMock(),因为我不希望它使用真实对象在ChatApp测试中.
因此,按照开玩笑网站上的基本说明,我创建了一个mocks文件夹,并为i18n创建了一个模拟文件,它从原始对象生成一个模拟,然后覆盖t方法并添加一个方法,允许我设置返回字符串为吨.
这是模拟对象
'use strict';
var i18nMock, _returnString;
i18nMock = jest.genMockFromModule('../scripts/support/i18n');
_returnString = "";
function __setReturnString(string) {
_returnString = string;
}
function t(key, options = null) {
return _returnString;
}
i18nMock.t.mockImplementation(t);
i18nMock.__setReturnString = __setReturnString;
module.exports = i18nMock;
Run Code Online (Sandbox Code Playgroud)
现在在我的ChatApp测试中,我需要在每个之前使用模拟,如下所示:
'use strict'; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的Jest测试,用于验证我的组件是否已呈现.我看到React的TestUtils具有按类或按标签查找渲染组件的功能,但不包括其ID属性.有一个findAllInRenderedTree函数,但文档没有解释回调应该期望什么.
我猜测回调将接收某种元素对象,这不是普通的DOM元素或普通的React组件.究竟是什么传递给了findAllInRenderedTree回调,我应该如何得到它的属性,或者至少它的ID?
我只是console.log回调的论点,但我很难让控制台在Jest测试中工作.
我有一个使用componentDidMount方法调用AJAX的react组件.当我尝试使用它时React.addons.TestUtils,组件在不进行AJAX调用的情况下进行渲染.如何使用jest测试react组件,以便进行AJAX调用?我是否还需要使用phantomJS(或像env这样的浏览器)来提供反应组件的DOM能力?
反应组件:
return React.createClass({
componentDidMount : function() {
$.ajax({
... makes http request
})
}
render : function() {
<div>
//view logic based on ajax response...
</div>
}
});
Run Code Online (Sandbox Code Playgroud)
测试用例:
jest.dontMock(../MyComponent);
var React = require('react/addons');
var TestUtils = React.addons.TestUtils;
var MyComponent = require(../MyComponent);
describe('Sample Test', function(){
it('To Render the component', function() {
var component = <MyComponent />;
var DOM = TestUtils.renderIntoDocument(component);
.... // Some other code...
});
})
Run Code Online (Sandbox Code Playgroud) 我想使用jest框架.所以在karma.conf文件中,我在框架字段中包含了jest.但它给出错误"WARN [记者]:无法加载"开玩笑",它没有注册!" 当我运行业力开始.我在package.json文件的依赖项中包含了jest包("jest":"〜0.1.39","jest-cli":"~0.4.1").
有人可以给我一些在业力中使用笑话的例子吗?
使用JEST进行单元测试,其具有附连到该文档的KEYDOWN听者的成分.
我该怎么测试JEST呢?如何模拟文档上的keydown事件?我需要事件监听器在文档上,因为它应该响应键盘操作而不管焦点元素.
编辑:这里的问题是关于模拟文档或document.body上的事件.所有的例子都是关于一个实际的DOM节点,它工作正常,但文档没有.
目前正在尝试这样做:
TestUtils.Simulate.keyDown(document, {keyCode : 37}); // handler not invoked
Run Code Online (Sandbox Code Playgroud) unit-testing addeventlistener reactjs jestjs reactjs-testutils
jestjs ×9
reactjs ×7
unit-testing ×4
javascript ×3
ajax ×1
hadoop ×1
hbase ×1
java ×1
karma-runner ×1
mapreduce ×1
webpack ×1