我正在使用Jest测试自定义变压器的性能.目前,变换器只会返回它从Jest获取的代码.变换器实现了getCacheKey函数. 这是变压器的完整代码: function process(src, path, config, transformOptions) { return
这是变压器的完整代码:
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配置如下:
"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是否会产生最小的开销,这会使这个问题蒙上阴影?
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’编码.
