Check if this adapter can handle the given test context.
This is used for adapter selection. Adapters are checked in priority order,
and the first adapter that returns true is used.
Test context object from the test framework
true if this adapter can handle the context
Extract normalized snapshot context from framework-specific context.
This method should extract the test name/path, file path, and update mode from the framework's test context object.
Test context object from the test framework
Normalized snapshot context
Perform the snapshot assertion.
This method should either:
The value to snapshot
Test context object from the test framework
Optionaloptions: SnapshotOptionsOptional serialization and naming options
AssertionFailure If the snapshot doesn't match, void otherwise
assertSnapshot(value: unknown, context: unknown, options?: SnapshotOptions): void {
const ctx = this.getContext(context);
const serialized = options?.serializer?.(value) ?? defaultSerializer(value);
if (ctx.isUpdateMode) {
saveSnapshot(ctx.testPath, serialized);
} else {
const expected = loadSnapshot(ctx.testPath);
if (serialized !== expected) {
throw new AssertionError('Snapshot mismatch');
}
}
}
Adapter for framework-specific snapshot operations.
Each test framework (node:test, Jest, Vitest, Mocha, etc.) has its own way of providing test context and storing snapshots. Adapters abstract these differences and provide a unified interface.
Example