我的团队在我们的项目中一直使用coffeescript和ES6 / ES2015(通过Babel)。由于Babel最终兼容文件,因此它的工作效果非常好。但我无法弄清楚我们如何编写允许两者的开玩笑测试。
我找到了如何使用咖啡或ES6功能的笑话的示例,但不是两者都有:
babel-jest
,如果我只将它与ES6一起使用,这对我来说很好。这些都有效。但同样,我无法弄清楚如何将它们组合起来
我尝试了自己的解决方案:
在package.json
我有:
"jest": {
"scriptPreprocessor": "<rootDir>/jest-script-preprocessor",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react",
"<rootDir>/node_modules/react-dom",
"<rootDir>/node_modules/react-addons-test-utils",
"<rootDir>/node_modules/fbjs"
],
"testFileExtensions": ["coffee", "cjsx", "js", "jsx"],
"moduleFileExtensions": ["coffee", "cjsx", "js", "jsx"]
}
在jest-script-preprocessor.js
中,我有:
var coffee = require('coffee-react');
var transform = require('coffee-react-transform');
var babelJest = require("babel-jest");
module.exports = {
process: function(src, path) {
if (coffee.helpers.isCoffee(path)) {
console.log(path);
return coffee.compile(transform(src), {
'bare': true
});
} else {
console.log(path);
return babelJest.process(src, {
filename: path,
stage: 0
});
}
}
};
如果我运行npm test __tests__/sometest.jsx
之类的测试,它会加载ES6测试文件。该测试文件将导入被测模块,也就是ES6,以及它爆炸的地方。只要Unexpected reserved word
符合import
,export
等语法,它就会简单地说export default 'mystring'
。有no additional line information,但我知道ES6会导致问题是因为如果我将被测模块更改为仅var q = 5 + 5; module.exports = q;
,它会爆炸,如果我将其更改为非{ES}语法console.log()
,它会导入模块。 (当然,这不是一个真正可测试的模块,但它不需要用于这个概念验证。)
注意那里的Unexpected reserved word
行。我从来没有见过他们。因此,追踪下来非常棘手的一个原因是我不能放入任何自己的调试逻辑。我确信这些行会运行,因为如果我在这些行上输入一些随机语法,它就会窒息。但没有控制台输出。
Map<String, bar> map = new HashMap<>();
map.put("main1", new bar("foo1", "foo2", "foo3"));
map.get("main1").getFoo1();
的相同错误。我写了an issue on their github repo。答案 0 :(得分:0)
这就是我正在做的,这对我有用。
npm install --save-dev babel-jest
npm install --save-dev coffee-react
在package.json
:
"jest": {
"scriptPreprocessor": "<rootDir>/jest-script-preprocessor",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/."
],
"testFileExtensions": ["coffee", "cjsx", "js", "jsx"],
"moduleFileExtensions": ["coffee", "cjsx", "js", "jsx"]
}
记下 unmockedModulePathPatterns
。我将其设置为匹配node_modules
中的所有内容。您可能不希望如此,但如果您开始收到Error: Failed to get mock metadata:...
之类的错误,请将其视为recommended here。
在jest-script-preprocessor.js
:
var babelJest = require('babel-jest');
var coffee = require('coffee-react');
module.exports = {
process: function(src, filename) {
if (filename.indexOf('node_modules') === -1) {
if (filename.match(/\.coffee|\.cjsx/)) {
src = coffee.compile(src, {bare: true});
} else {
src = babelJest.process(src, filename);
}
}
return src;
}
};