gpt4 book ai didi

coffeescript - 如何将 jest 与 CoffeeScript 和 ES6/ES2015 一起使用(例如通过 Babel)?

转载 作者:行者123 更新时间:2023-12-03 17:41:21 32 4
gpt4 key购买 nike

我的团队在我们的项目中一直在使用 coffeescript 和 ES6/ES2015(通过 Babel)。由于 Babel 文件最终是兼容的,因此它运行良好。但我不知道我们如何编写一个允许两者的 Jest 测试。

我找到了如何将 jest 与咖啡或 ES6 功能一起使用的示例,但不能同时使用这两种功能:

  • Example with coffeescript
  • Another example with coffeescript
  • Example with Babel
  • 有人建议将预处理器设置为 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只要它遇到 ES6-only 语法,例如 import , export等有no additional line information ,但我知道是 ES6 导致了问题,因为如果我将被测模块更改为 ONLY export default 'mystring' ,它会爆炸,如果我将其更改为非 ES6 语法,如 var q = 5 + 5; module.exports = q; ,它可以很好地导入模块。 (当然,这并不是一个真正的可测试模块,但它不需要用于这个概念验证。)

    注意 console.log()那里的线。我从来没有见过他们。所以这很难追踪的一个原因是我无法放入我自己的任何调试逻辑。我确信这些行会运行,因为如果我在这些行上添加一些随机语法,它会窒息。但没有控制台输出。
  • 我试过jest-webpack-alias ,即 officially recommended by jest ,这在理论上听起来很棒:您将 jest 与 webpack 一起使用,然后您就可以使用您已经设置的任何预处理器。它给了我与 Unexpected reserved word 相同的错误.我写了an issue on their github repo .

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

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

    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;
    }
    };

    关于coffeescript - 如何将 jest 与 CoffeeScript 和 ES6/ES2015 一起使用(例如通过 Babel)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35327165/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com