我如何在咖啡脚本和 ES6/ES2015 中使用 jest(例如通过 Babel)?

Tyl*_*ier 5 coffeescript ecmascript-6 webpack jestjs jest-webpack-alias

我的团队一直在我们的项目中使用 coffeescript 和 ES6/ES2015(通过 Babel)。由于 Babel 使文件最终兼容,因此它运行良好。但我无法弄清楚我们如何编写一个允许两者兼而有之的玩笑测试。

我已经找到了如何在咖啡或 ES6 功能中使用 jest 的示例,但不能同时使用:

这些都有效。但同样,我无法弄清楚如何将它们结合起来

我试过的

  • 我尝试了自己的解决方案:

    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"]
    }
    
    Run Code Online (Sandbox Code Playgroud)

    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
                });
            }
        }
    };
    
    Run Code Online (Sandbox Code Playgroud)

    如果我运行类似 的测试npm test __tests__/sometest.jsx,它会很好地加载 ES6 测试文件。该测试文件将导入被测模块,它也是 ES6,这就是它爆炸的地方。它只是说Unexpected reserved word,一旦遇到 ES6-only 语法,如import,export等。没有额外的行信息,但我知道是 ES6 导致了问题,因为如果我将测试中的模块更改为 ONLY export default 'mystring',它就会爆炸,如果我将其更改为非 ES6 语法,例如var q = 5 + 5; module.exports = q;,它会很好地导入模块。(当然,这并不是一个真正可测试的模块,但对于这个概念验证来说,它不需要。)

    注意里面的console.log()线条。我从来没有见过他们。因此,追踪如此棘手的一个原因是我无法放入任何我自己的调试逻辑。我确信这些行会运行,因为如果我在这些行上加入一些随机语法,它会窒息。但是没有控制台输出。

  • 我试过jest-webpack-alias,这是jest 官方推荐的,理论上听起来很棒:你将 jest 与 webpack 一起使用,然后允许你使用你已经设置的任何预处理器。它给了我同样的错误Unexpected reserved word。我在他们的 github repo 上写了一个问题

笔记

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

Tyl*_*ier 0

这就是我正在做的对我有用的事情。

npm install --save-dev babel-jest
npm install --save-dev coffee-react
Run Code Online (Sandbox Code Playgroud)

package.json

"jest": {
    "scriptPreprocessor": "<rootDir>/jest-script-preprocessor",
    "unmockedModulePathPatterns": [
        "<rootDir>/node_modules/."
    ],
    "testFileExtensions": ["coffee", "cjsx", "js", "jsx"],
    "moduleFileExtensions": ["coffee", "cjsx", "js", "jsx"]
}
Run Code Online (Sandbox Code Playgroud)

请注意unmockedModulePathPatterns. 我将其设置为匹配 中的所有内容node_modules。您可能不希望这样,但如果您开始出现类似的错误Error: Failed to get mock metadata:...,请考虑它,如此处建议的那样

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;
    }
};
Run Code Online (Sandbox Code Playgroud)