@datashard/snapshot

中文文档
3.0.0-beta.1发布于 2024/05/03MITCommonJS近 30 天下载 3731

Adds JSON Snapshot testing support to Cypress

原始使用文档
文档目录6 节
  1. ⚠️ Breaking Changes ⚠️
  2. Install
  3. Import
  4. Config
  5. Usage
  6. Example

@datashard/snapshot

Adds JSON Snapshot comparison to Cypress

⚠️ Breaking Changes ⚠️

  • With V3, the readFileMaybe task has been removed, we now rely on cy.fixture internally.
  • the previous SNAPSHOT_UPDATE Environment/Config Variable has been changed to updateSnapshots

[!DANGER] This means that previous tests will likely be broken, please make sure that your tests pass before updating to the latest version of this Plugin.

This current release will be released as 3.0.0-beta, should Bugs be found by me or my Employer, I will open Issues/PRs to fix those, should anyone else find Bugs/Edgecases, etc. please open an Issue.

Install

Requires Node 16 or above

npm i --save-dev @datashard/snapshot

Import

After Installing, you'll need to add the following import into your Commands/Support File

by default this will be cypress/support/e2e.js

require('@datashard/snapshot').regsiter()

This will register a new Command .snapshot(), to create and compare JSON Snapshots.

Config

You can pass updateSnapshots and useFolders as options in the cypress.config.js file

Example Settings for the Module Alternatively, you can also add snapshotUpdate as an Environment Variable to update your snapshots.

Simply pass --env updateSnapshots=true when running Cypress.

Usage

If properly added, usage of this plugin is rather simple, just add .snapshot() to cypress functions that return valid JSON. (i.e. cy.wrap)

When properly added, you can chain .snapshot() off of cy functions like cy.wrap, just make sure they return valid JSON.

Example

describe("my test", () => {
  it("works", () => {
    cy.log("first snapshot");
    cy.wrap({ foo: 42 }).snapshot("foo");
    cy.log("second snapshot");
    cy.wrap({ bar: 101 }).snapshot("bar");
  });
});

This Plugin will then save your snapshots as

// useFolders: false
cypress/fixtures/my-test__works__foo.json
cypress/fixtures/my-test__works__bar.json

// useFolders: true
cypress/fixtures/my-test/works/foo.json
cypress/fixtures/my-test/works/bar.json

// {fixtureFolder}/<Context>-<Describe>-<It>-<Name?>.json
// {fixtureFolder}/<Context>/<Describe>/<It>/<Name?>.json

Snapshots will generally be saved using this the Convention mentioned in the Comment of the above Codeblock, which is provided by the named Cypress Test Steps.

Passing a name to the Snapshot function is required, but not checked, if you want to take multiple snapshots in the same block.

If you have two or more Snapshots in the same Block, the next one WILL always overwrite the previous one while updating, causing the First Snapshot in the Block to Fail. While running your Tests, if a value changed, it will, of course, no longer match the snapshot and throw an Error.

Which will look like this:

When the Test succeeds, it will instead log a Success in the Log and let you know where the File has been saved to, relative to the Fixture Snapshot Folder

版本与发布记录

@datashard/snapshot 共发布 7 个版本,首次发布于 2023/02/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0-beta.1latest2024/05/032 年前
3.0.0-beta2024/04/252 年前
2.3.22023/11/202 年前
2.2.22023/05/303 年前
2.2.12023/05/253 年前
2.2.02023/03/273 年前

关于 @datashard/snapshot 的常见问题

@datashard/snapshot 是什么?

Adds JSON Snapshot testing support to Cypress

如何安装 @datashard/snapshot?

在项目目录运行 npm install @datashard/snapshot,或使用 pnpm add / yarn add / bun add。

@datashard/snapshot 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/datashard__snapshot 是否存在。

@datashard/snapshot 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@datashard/snapshot 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@datashard/snapshot 可以商用吗?

@datashard/snapshot 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@datashard/snapshot 的下载量有多少?

根据 npm 官方下载统计,@datashard/snapshot 在 2026/10/12 – 2026/11/10 这 30 天内被下载 3,731 次,日均约 124 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@datashard/snapshot 还在维护吗?

最新版本 3.0.0-beta.1 发布于 2024/05/03(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/02/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。