【发布时间】:2015-05-06 09:01:57
【问题描述】:
我使用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;
现在,我想使用jest 测试这个组件。这是我的package.json 的相关内容:
"scripts": {
"test": "jest"
},
"jest": {
"rootDir": ".",
"testDirectoryName": "tests",
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"unmockedModulePathPatterns": [
"react"
]
}
运行npm test时,出现以下错误:
语法错误:/Users/mishamoroshko/react-component/src/tests/MyComponent.js:/Users/mishamoroshko/react-component/src/MyComponent.js:/Users/mishamoroshko/react-component/src/MyComponent。 less:意外的令牌非法
看起来 webpack 需要在 jest 运行测试之前处理 require('./MyComponent.less')。
我想知道我是否需要使用jest-webpack 之类的东西。如果是,有没有办法指定多个scriptPreprocessors? (注意我已经用babel-jest了)
【问题讨论】:
-
您是否尝试过使用 webpack 将测试文件编译到单独的目录中,然后在生成的文件上运行 jest?
-
我也遇到过这个问题,并且有一个类似的问题here 概述了我尝试过的方法(这里也提到了)以及为什么它们还不够。