@altano/vitest-plugins

中文文档
2.0.0发布于 2026/10/15ISCTypeScriptES Module构建来源已验证近 30 天下载 4664

Custom matchers and snapshot serializers to enhance vitest

原始使用文档
文档目录17 节
  1. To install the custom matchers
  2. To install the snapshot serializers
  3. Snapshot Serializers
  4. VFile
  5. Absolute Paths
  6. URLs
  7. HTML
  8. Matchers
  9. toMatchError
  10. toThrowErrorMatching
  11. toBePath
  12. toBeFile
  13. toBeDirectory
  14. toEqualFile
  15. toHaveExifProperty
  16. toHaveHeader (on Response objects)
  17. toHaveStatus (on Response objects)

vitest-plugins

npm Typed with TypeScript ESM only

Custom matchers and snapshot serializers to enhance vitest.

Installation

npm install -D @altano/vitest-plugins

To install the custom matchers

Modify vite.config.ts / vitest.config.ts:

export default defineConfig({
  test: {
    setupFiles: [
      "@altano/vitest-plugins/matchers",
      // ...
    ],
    // ...
  },
});

Add matcher types to your tsconfig.json (if using TypeScript):

{
  "compilerOptions": {
    "types": ["@altano/vitest-plugins/matchers"]
  }
}

To install the snapshot serializers

Modify vite.config.ts / vitest.config.ts:

export default defineConfig({
  test: {
    setupFiles: [
      "@altano/vitest-plugins/serializers",
      // ...
    ],
    // ...
  },
});

NOTE: You can pick and choose what to install: the matchers and serializers don't depend on each other.

Details

Snapshot Serializers

VFile

Will format the contents of a vfile using Prettier (auto-detecting the type from the vfile's filename), e.g.

FormattedVFile {
  "cwd": "<cwd>",
  "data": {},
  "history": [
    "tests/unit/__fixtures__/basic/input.js",
  ],
  "map": undefined,
  "messages": [],
  "value": "function face() { }"
}

Absolute Paths

Will replace any instances of process.cwd() with <cwd> in the snapshot. Useful when serializing strings that contain absolute paths, since those will be different on other machines running the tests.

URLs

Will replace any instances of http://localhost:1234 with http://localhost in the snapshot. Useful when dealing with test servers that use random ports.

HTML

HTML is prettier formatted. Only works on well-formed HTML that starts with <html or <!doctype. To avoid infinitely reformatting the HTML, it is preprended with <!-- Formatted HTML -->.

Matchers

Vitest's error matchers let you match against the error message, but not the rest of the Error object:

  • toThrow(error?) - error is thrown (docs)
  • toThrowErrorMatching[Inline]Snapshot - an error exactly matches a snapshot (docs)

If you want to assert anything more complicated (e.g. an error contains some substring in the stack) then you'll need these custom matchers:

toMatchError

Verify any part of an error object (e.g. the stack):

expect(new Error("face")).toMatchError(
  expect.objectContaining({
    stack: expect.stringContaining("readme.spec.ts"),
  }),
);

toThrowErrorMatching

Verify any part of a thrown error object (e.g. the stack):

expect(() => {
  throw new Error("face");
}).toThrowErrorMatching(
  expect.objectContaining({
    stack: expect.stringContaining("readme.spec.ts"),
  }),
);

toBePath

Verify the realpath (canonical path) of expected. More lenient than a string check when dealing with logical paths, symlinks, etc.

expect("/private/some/path").toBePath("/some/path");

toBeFile

Verify a file exists (on the filesystem)

expect(import.meta.filename).toBeFile();

toBeDirectory

Verify a directory exists (on the filesystem)

expect("/").toBeDirectory();

toEqualFile

Verify that the contents of a file at a given path match the contents of a file at a another path

expect("/some/file.txt").toEqualFile("/other/file.txt");

toHaveExifProperty

Similar to the toHaveProperty matcher, but checks exif properties on the given buffer (or any object parseable by the exifr library).

toHaveHeader (on Response objects)

Verify that Response has an expected header and optional value

toHaveStatus (on Response objects)

Verify that a Response has an expected status code

版本与发布记录

@altano/vitest-plugins 共发布 10 个版本,首次发布于 2024/04/01;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/10/1523 天前
1.1.32026/08/023 个月前
1.1.22026/03/237 个月前
1.1.12025/07/081.3 年前
1.1.02025/04/021.6 年前
1.0.02025/03/311.6 年前

关于 @altano/vitest-plugins 的常见问题

@altano/vitest-plugins 是什么?

Custom matchers and snapshot serializers to enhance vitest

如何安装 @altano/vitest-plugins?

在项目目录运行 npm install @altano/vitest-plugins,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vitest(5.x.x)、prettier(3.x.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@altano/vitest-plugins 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@altano/vitest-plugins 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@altano/vitest-plugins 可以商用吗?

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

@altano/vitest-plugins 的下载量有多少?

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

@altano/vitest-plugins 还在维护吗?

最新版本 2.0.0 发布于 2026/10/15(23 天前);至今共发布 10 个版本,其中过去 12 个月发布了 3 个;首次发布于 2024/04/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。