简体   繁体   English

如何配置 jest 快照位置

[英]How to configure jest snapshot locations

I'd like for my jest snapshots to be created as a sibling to my test file我希望将我的笑话快照创建为我的测试文件的兄弟

I current have my snapshots being put in the default __snapshots__ folder.我目前将我的快照放在默认的__snapshots__文件夹中。

Current:当前的:

文件夹结构截图:之前

What I would like to achieve:我想达到的目标:

文件夹结构截图:我想要的

I found this post on github: https://github.com/facebook/jest/issues/1650我在 github 上找到了这篇文章: https : //github.com/facebook/jest/issues/1650

In the thread someone says the following should work but I haven't had any luck (even with changing the regex and other changes):在线程中,有人说以下应该有效,但我没有任何运气(即使更改了正则表达式和其他更改):

module.exports = {
  testPathForConsistencyCheck: 'some/example.test.js',

  resolveSnapshotPath: (testPath, snapshotExtension) =>
    testPath.replace(/\.test\.([tj]sx?)/, `${snapshotExtension}.$1`),

  resolveTestPath: (snapshotFilePath, snapshotExtension) =>
    snapshotFilePath.replace(snapshotExtension, '.test'),
}

In package.json (or if you use jest.config.js ) you need to add the path for the snapshotResolver file:package.json (或者如果您使用jest.config.js )中,您需要添加snapshotResolver文件的路径:

"jest": {
  "snapshotResolver": "./snapshotResolver.js"
}

snapshotResolver.js is a file with code that you found in a Github issue. snapshotResolver.js是一个包含您在 Github 问题中找到的代码的文件。

In my case this file was located at the root of the project (near node_modules folder)在我的例子中,这个文件位于项目的根目录(靠近node_modules文件夹)

Also, in addition to the path to the snapshotResolver as suggested in the Vasyl Nahuliak's answer , to achieve having the snapshot and the test files looking like this:此外,除了Vasyl Nahuliak 的回答中建议的 snapshotResolver 路径之外,要实现快照和测试文件如下所示:

file.test.js
file.test.js.snap

your snapshotResolver should look like this:您的 snapshotResolver 应如下所示:

module.exports = {
  testPathForConsistencyCheck: 'some/example.test.js',

  resolveSnapshotPath: (testPath, snapshotExtension) =>
    testPath.replace(/\.test\.([tj]sx?)/, `.test.$1${snapshotExtension}`),

  resolveTestPath: (snapshotFilePath, snapshotExtension) =>
    snapshotFilePath.replace(snapshotExtension, ''),
};

These solutions are more complicated that is needed for what you are trying to do.这些解决方案更复杂,这是您尝试执行的操作所需的。

As per pointed out in https://github.com/facebook/jest/issues/1650正如在https://github.com/facebook/jest/issues/1650 中指出的那样

Solution解决方案

Create a file: I used - 'jest/snapshotResolver.js'创建一个文件:我用过 - 'jest/snapshotResolver.js'

module.exports = {
  resolveSnapshotPath: (testPath, snapshotExtension) =>
     testPath + snapshotExtension,
  resolveTestPath: (snapshotFilePath, snapshotExtension) =>
    snapshotFilePath.replace(snapshotExtension, ''),
  testPathForConsistencyCheck: 'some.test.js',
};

in your jest config set that file to the resolver在你的笑话配置中将该文件设置为解析器

snapshotResolver: './jest/snapshotResolve.js',

or if your jest config in package.json:或者如果你在 package.json 中开玩笑的配置:

"snapshotResolver": "./jest/snapshotResolve.js",

Explanation解释

In short these two functions mirror each other, one takes the test file path and returns the snapshot path, the other takes the snapshot path and returns the test file path.总之这两个函数互为镜像,一个取测试文件路径返回快照路径,另一个取快照路径返回测试文件路径。 The third is a file path example for validation.第三个是用于验证的文件路径示例。

Clearer code To help clarify what is going on更清晰的代码帮助澄清发生了什么

One thing to keep in mind is that the path is the full path not the relative path.要记住的一件事是路径是完整路径而不是相对路径。

/**
 * 
 * @param testPath Path of the test file being test3ed
 * @param snapshotExtension The extension for snapshots (.snap usually)
 */
const resolveSnapshotPath = (testPath, snapshotExtension) => {
    const snapshotFilePath = testPath + snapshotExtension; //(i.e. some.test.js + '.snap')
    return snapshotFilePath;
}

/**
 * 
 * @param snapshotFilePath The filename of the snapshot (i.e. some.test.js.snap) 
 * @param snapshotExtension The extension for snapshots (.snap)
 */
const resolveTestPath = (snapshotFilePath, snapshotExtension) => {
    const testPath = snapshotFilePath.replace(snapshotExtension, '').replace('__snapshots__/', ''); //Remove the .snap
    return testPath;
}

/* Used to validate resolveTestPath(resolveSnapshotPath( {this} )) */
const testPathForConsistencyCheck = 'some.test.js'; 

module.exports = {
    resolveSnapshotPath, resolveTestPath, testPathForConsistencyCheck
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM