vitest-cbor
v1.1.2
Published
Custom Vitest matchers for testing CBOR data structures
Readme
vitest-cbor
Custom Vitest matchers for testing CBOR data structures.
Installation
npm install --save-dev vitest-cborSetup
Extending in a Setup File
Import vitest-cbor/extend-expect in your test setup file:
// vitest.setup.ts
import "vitest-cbor/extend-expect";// vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
setupFiles: ["vitest.setup.ts"],
},
});Extending in Individual Tests
If you prefer not to use a setup file, you can extend expect in individual test files:
import { expect, test } from "vitest";
import * as matchers from "vitest-cbor/matchers";
expect.extend(matchers);
test("my test", () => {
// ...
});Usage with TypeScript
When using the setup file approach, ensure your setup file is included in your tsconfig.json:
{
"include": ["vitest.setup.ts"]
}Matchers
toBeCbor(expected?)
Asserts that a Uint8Array contains valid CBOR data. When called with an expected value, also asserts that the decoded value matches.
import { encode } from "cbor2";
const data = new Uint8Array([0x63, 0x66, 0x6f, 0x6f]);
expect(data).toBeCbor();
const encoded = encode({ name: "alice", age: 30 });
expect(encoded).toBeCbor({ name: "alice", age: 30 });
expect({ payload: data }).toEqual({ payload: expect.toBeCbor() });
expect({ payload: encoded }).toEqual({
payload: expect.toBeCbor({ name: "alice", age: 30 }),
});