我今天遇到了 jest 的小问题,我的文件夹结构遵循这个约定
components/
Paragraph/
index.js
style.js
test.js
当我运行 jest 快照测试时,它会在每个组件文件夹中生成它们,即
components/
Paragraph/
__snapshots__
index.js
style.js
test.js
有没有办法强制 jest 将所有快照放在根 lvl __snapshot__
文件夹中?所以最终的输出看起来更像这样
__snapshots__
// All snapshots here?
components/
Paragraph/
index.js
style.js
test.js
最佳答案
在 package.json(或在 jest.config.js,如果你使用)中添加到 snapshotResolver 文件的路径
"jest": {
"snapshotResolver": "./snapshotResolver.js"
}
在根项目中使用以下代码创建 snapshotResolver.js 文件:
const path = require('path')
const rootDir = path.resolve(__dirname, '..')
module.exports = {
testPathForConsistencyCheck: 'some/__tests__/example.test.js',
/** resolves from test to snapshot path */
resolveSnapshotPath: (testPath, snapshotExtension) => {
return testPath.replace('src/', '__snapshots__/') + snapshotExtension
},
/** resolves from snapshot to test path */
resolveTestPath: (snapshotFilePath, snapshotExtension) => {
return snapshotFilePath
.replace('__snapshots__/', 'src/')
.slice(0, -snapshotExtension.length)
},
}
https://gist.github.com/prescottprue/5ece5e173bcfba7ed86dae6a91444451
关于node.js - 如何强制 Jest 在根 lvl __snapshots__ 文件夹中输出快照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50200694/