node.js - 如何强制 Jest 在根 lvl __snapshots__ 文件夹中输出快照?

标签 node.js testing jestjs

我今天遇到了 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/

相关文章:

testing - 使用构建服务器时如何在本地测试代码?

android - 文件存在测试对缓存文件夹不起作用...?

c# - 每次调用最小起订量时,模拟方法输出不同的输出参数

reactjs - 如何使用 react-native-testing-library getByRole

javascript - 使用 Jest 运行 Puppeteer 测试时如何增加导航超时

javascript - puppeteer API 未触发点击事件

node.js - 按日期时间字段获取最新的 MongoDB 记录

javascript - 使用数组推送获取额外的括号

javascript - MEAN API 请求方法问题

node.js - 为什么 server.close 回调从未被调用?