Yi *_*Ren 70 unit-testing mocha.js reactjs webpack webpack-dev-server
我是Mocha的新手,我正在尝试用它来测试一个简单的React组件.如果react组件没有任何CSS样式,则测试将通过,但如果React组件中的标记包含任何className,则会抛出语法错误:
Testing.react.js
import React from 'react';
export default class Testing extends React.Component {
render() {
return (
<section>
<form>
<input type="text" />
</form>
</section>
);
}
}
Run Code Online (Sandbox Code Playgroud)
testing.jsx
import {
React,
sinon,
assert,
expect,
TestUtils
} from '../../test_helper';
import TestingSample from '../../../app/components/Testing.react.js';
describe('TestingSample component', function(){
before('render and locate element', function(){
var renderedComponent = TestUtils.renderIntoDocument(
<TestingSample />
);
var inputComponent = TestUtils.findRenderedDOMComponentWithTag(
renderedComponent, 'input'
);
this.inputElement = inputComponent.getDOMNode();
});
it('<input> should be of type "text"', function () {
assert(this.inputElement.getAttribute('type') === 'text');
});
})
Run Code Online (Sandbox Code Playgroud)
测试将通过:
> mocha --opts ./test/javascripts/mocha.opts --compilers js:babel/register --recursive test/javascripts/**/*.jsx
TestSample component
? <input> should be of type "text"
1 passing (44ms)
Run Code Online (Sandbox Code Playgroud)
在输入标记内添加className后,出现错误:
import React from 'react';
import testingStyle from '../../scss/components/landing/testing.scss';
export default class Testing extends React.Component {
render() {
return (
<section>
<form>
<input type="text" className="testingStyle.color" placeholder="Where would you like to dine" />
</form>
</section>
);
}
}
Run Code Online (Sandbox Code Playgroud)
测试结果:
SyntaxError: /Users/../../../Documents/project/app/scss/components/landing/testing.scss: Unexpected token (1:0)
> 1 | .color {
| ^
2 | color: red;
3 | }
Run Code Online (Sandbox Code Playgroud)
我在网上搜索但到目前为止没有运气.我错过了什么吗?请帮助我或指出我正确的方向将不胜感激.我目前正在使用:
Node Express Server
React
React-router
Webpack
Babel
Mocha
Chai
Sinon
Sinon-Chai
rzu*_*ger 146
有一个babel/register样式钩子可以忽略样式导入:
https://www.npmjs.com/package/ignore-styles
安装它:
npm install --save-dev ignore-styles
运行没有样式的测试:
mocha --require ignore-styles
zha*_*ing 12
你可以使用css编译器运行mocha,编译器js如下:
CSS-DNT-compiler.js
function donothing() {
return null;
}
require.extensions['.css'] = donothing;
require.extensions['.less'] = donothing;
require.extensions['.scss'] = donothing;
// ..etc
Run Code Online (Sandbox Code Playgroud)
并像这样运行mocha命令:
mocha --compilers js:babel-core/register,css:css-dnt-compiler.js --recursive
Run Code Online (Sandbox Code Playgroud)
我在这里的答案相同,这就是我以前在Babel 6上工作的原因
的package.json
"scripts": {
"test": "mocha --compilers js:babel-core/register
--require ./tools/testHelper.js 'src/**/*-spec.@(js|jsx)'",
Run Code Online (Sandbox Code Playgroud)
工具/ testHelper.js
// Prevent mocha from interpreting CSS @import files
function noop() {
return null;
}
require.extensions['.css'] = noop;
Run Code Online (Sandbox Code Playgroud)
这使您可以在组件旁边的src文件夹中进行测试.您可以使用require.extensions添加任意数量的扩展名.
| 归档时间: |
|
| 查看次数: |
16048 次 |
| 最近记录: |