这个插件"version": "0.0.0-development" 似乎还为时过早。
好像没有自定义比较功能的选项,看source目前仅限于snap-shot-compare
cypress-io/snapshot/src/index.js
'use strict'
/* global cy, Cypress */
...
const compare = require('snap-shot-compare')
...
function compareValues ({ expected, value }) {
const noColor = true
const json = true
return compare({ expected, value, noColor, json })
}
但是可以在比较函数周围添加一个包装器。
您需要制作插件的本地副本并对其进行修改以引用自定义 compare 函数。
这意味着您将无法直接升级到插件的下一个正式版本,但需要为每个版本重复这些步骤
在我的 React 应用程序中,我想在比较之前从快照中删除 css 模块哈希。
我采取的步骤如下:
npm install --save-dev @cypress/snapshot
将安装的文件夹node_modules\@cypress\snapshot复制到cypress\support\snapshot
-
将cypress\support\snapshot\src\index.js 中的compare 导入更改为指向自定义比较器,如下所示:
// const compare = require('snap-shot-compare')
const compare = require('./my-compare')
-
使用以下代码将my-compare.js 添加到文件夹cypress\support\snapshot\src\:
const snapshotCompare = require('snap-shot-compare')
function compare(data) {
const filterRegex = /__[^"]+/gm;
filtered = {
...data,
expected: data.expected.replace(filterRegex, ''),
value: data.value.replace(filterRegex, '')
}
return snapshotCompare(filtered)
}
module.exports = compare
最棘手的一点是正确使用正则表达式。您可能可以使用以下内容,请查看regex101。
const filterRegex = /data-v-[^=]+=""/gm;
您可以通过在第二次运行之前编辑第一个保存的 snapshot.js 来粗略地测试 - 例如,将 data-v- 更改为 datav-,并观察测试失败,因为正则表达式没有获取 mod。
请注意,snapshot.js 包含全文而不是过滤后的文本,正则表达式仅用于比较目的。