如何在coffeescript和ES6 / ES2015中使用jest(例如通过Babel)?

时间:2016-02-10 22:23:47

标签: coffeescript ecmascript-6 webpack jestjs jest-webpack-alias

我的团队在我们的项目中一直使用coffeescript和ES6 / ES2015(通过Babel)。由于Babel最终兼容文件,因此它的工作效果非常好。但我无法弄清楚我们如何编写允许两者的开玩笑测试。

我找到了如何使用咖啡或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符合importexport等语法,它就会简单地说export default 'mystring'。有no additional line information,但我知道ES6会导致问题是因为如果我将被测模块更改为仅var q = 5 + 5; module.exports = q;,它会爆炸,如果我将其更改为非{ES}语法console.log(),它会导入模块。 (当然,这不是一个真正可测试的模块,但它不需要用于这个概念验证。)

    注意那里的Unexpected reserved word行。我从来没有见过他们。因此,追踪下来非常棘手的一个原因是我不能放入任何自己的调试逻辑。我确信这些行会运行,因为如果我在这些行上输入一些随机语法,它就会窒息。但没有控制台输出。

  • 我尝试了jest-webpack-alias,这是officially recommended by jest,理论上听起来很棒:你在webpack中使用jest,然后允许你使用你已经设置的任何预处理器。它给了我Map<String, bar> map = new HashMap<>(); map.put("main1", new bar("foo1", "foo2", "foo3")); map.get("main1").getFoo1(); 的相同错误。我写了an issue on their github repo

注释

  • 我找到jestpack,但我不想使用它,因为它需要Node&gt; = 5,我想使用Node 4.2.3。它也不适用于Jest&gt; = 0.8,我想使用Jest 0.8,因为它是目前最新的,我认为最有可能与实时文档同步并反应版本(0.14.6)。

1 个答案:

答案 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;
    }
};
相关问题