我正在用Jest测试自定义变压器的性能。目前,转换器只返回从 Jest 获得的代码。变压器实现了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;
链接到变压器
package.json
中的开玩笑配置如下:
"jest": {
"transform": {
"^.+\.tsx?$": "<rootDir>/ts-transformer.js"
},
"testMatch": [
"<rootDir>/test-jest/**/*.ts"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
]
}
链接到包.json
使用 Jest 测试此设置时,使用和不使用--no-cache
需要相同的时间(大约 9 秒(
使用 Mocha 测试此设置时,第一次运行大约需要 7 秒,后续运行大约需要 4 秒。
在这两种情况下(使用 jest 和 mocha(,在不更改任何源或测试文件的情况下测试后续运行。
我的问题:
- 由于缓存,后续的 Jest 运行不应该更快吗?
- 变压器中是否有某些东西阻碍了测试持续时间的改善?
- Jest 是否会产生使此问题蒙上阴影的最小开销?
单独更新片段(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"的编码。