Custom matchers and snapshot serializers to enhance vitest
npm install @altano/vitest-plugins !Typed with TypeScript !ESM only
Custom matchers and snapshot serializers to enhance vitest.
```
npm install -D @altano/vitest-plugins
Modify vite.config.ts / vitest.config.ts:
`ts`
export default defineConfig({
test: {
setupFiles: [
"@altano/vitest-plugins/matchers",
// ...
],
// ...
},
});
Add matcher types to your tsconfig.json (if using TypeScript):
`json`
{
"compilerOptions": {
"types": ["@altano/vitest-plugins/matchers"]
}
}
Modify vite.config.ts / vitest.config.ts:
`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.
Will format the contents of a vfile using Prettier (auto-detecting the type from the vfile's filename), e.g.
`snap`
FormattedVFile {
"cwd": "
"data": {},
"history": [
"tests/unit/__fixtures__/basic/input.js",
],
"map": undefined,
"messages": [],
"value": "function face() { }"
}
Will replace any instances of process.cwd() with in the snapshot. Useful when serializing strings that contain absolute paths, since those will be different on other machines running the tests.
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 is prettier formatted. Only works on well-formed HTML that starts with or . To avoid infinitely reformatting the HTML, it is preprended with .
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:
Verify any part of an error object (e.g. the stack):
`ts`
expect(new Error("face")).toMatchError(
expect.objectContaining({
stack: expect.stringContaining("readme.spec.ts"),
}),
);
Verify any part of a _thrown_ error object (e.g. the stack):
`ts`
expect(() => {
throw new Error("face");
}).toThrowErrorMatching(
expect.objectContaining({
stack: expect.stringContaining("readme.spec.ts"),
}),
);
Verify the realpath (canonical path) of expected. More lenient than a string check when dealing with logical paths, symlinks, etc.
`ts`
expect("/private/some/path").toBePath("/some/path");
Verify a file exists (on the filesystem)
`ts`
expect(import.meta.filename).toBeFile();
Verify a directory exists (on the filesystem)
`ts`
expect("/").toBeDirectory();
Verify that the contents of a file at a given path match the contents of a file at a another path
`ts`
expect("/some/file.txt").toEqualFile("/other/file.txt");
Similar to the toHaveProperty matcher, but checks exif properties on the given buffer (or any object parseable by the exifr` library).
Verify that Response has an expected header and optional value
Verify that a Response has an expected status code