【问题标题】:Jest custom transformer - can its performance be improved?Jest 定制变压器 - 可以提高其性能吗?
【发布时间】:2017-07-29 01:11:09
【问题描述】:

我正在使用 Jest 测试自定义变压器的性能。目前,transformer 只返回从 Jest 获得的代码。 Transformer 实现了getCacheKey 函数。

这是转换器的完整代码:

function process(src, path, config, transformOptions) {
  return src;
}
exports.process = process;

function getCacheKey(fileData, filePath, configStr, options) {
  return crypto.createHash('md5')
    .update(fileData + filePath + configStr, 'utf8')
    .digest('hex');
}
exports.getCacheKey = getCacheKey;

Link to the transformer

package.json 中的笑话配置如下:

"jest": {
  "transform": {
    "^.+\\.tsx?$": "<rootDir>/ts-transformer.js"
  },
  "testMatch": [
    "<rootDir>/test-jest/**/*.ts"
  ],
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "json"
  ]
}

Link to package.json

使用 Jest 测试此设置时,使用和不使用 --no-cache 所需的时间相同(大约 9 秒)

使用 Mocha 测试此设置时,第一次运行大约需要 7 秒,后续运行大约需要 4 秒。

在这两种情况下(使用 jest 和 mocha),后续运行都在不更改任何源或测试文件的情况下进行了测试。

我的问题:

  • 由于缓存,后续 Jest 运行不应该更快吗?
  • 变压器中是否有什么东西阻碍了测试持续时间的改进?
  • Jest 产生的最小开销是否会影响这个问题?

【问题讨论】:

    标签: jestjs


    【解决方案1】:

    单独更新片段(fileData、filePath、configStr)可能会更快,因此串联时不必有文件内容的副本。

    function getCacheKey(fileData, filePath, configStr, options) {
        const hash = crypto.createHash('md5');
        hash.update(fileData);
        hash.update(filePath);
        hash.update(configStr);
        return hash.digest('hex');
    }
    

    注意:如果未提供编码,并且数据是字符串,则强制使用“utf8”编码。

    【讨论】:

    • 这样做对持续时间没有任何明显影响
    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2017-02-05
    • 2012-11-23
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    相关资源
    最近更新 更多