@seedts/testing
Version:
Testing utilities for SeedTS - Snapshot testing, deterministic seeding, and test helpers
92 lines • 3.1 kB
JavaScript
;
/**
* Jest integration for SeedTS testing
*
* Provides custom matchers and utilities for testing seeds with Jest
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.extendJestMatchers = extendJestMatchers;
exports.createSeedTest = createSeedTest;
const snapshot_js_1 = require("../snapshot.js");
/**
* Custom Jest matcher: toMatchSeedSnapshot
*
* Usage with Jest:
* ```typescript
* import { expect } from '@jest/globals';
* import { extendJestMatchers } from '@seedts/testing/jest';
*
* extendJestMatchers(expect);
*
* test('users seed snapshot', async () => {
* const result = await executor.execute();
* await expect(result[0].data).toMatchSeedSnapshot('users');
* });
* ```
*/
function extendJestMatchers(expect) {
expect.extend({
async toMatchSeedSnapshot(received, snapshotName, options = {}) {
const updateSnapshot = (0, snapshot_js_1.shouldUpdateSnapshots)() ||
options.updateSnapshot ||
this.snapshotState?._updateSnapshot === 'all';
const result = await (0, snapshot_js_1.snapshot)(snapshotName, received, {
...options,
updateSnapshot,
});
if (result.isNew) {
return {
pass: true,
message: () => `New snapshot created: ${result.snapshotPath}`,
};
}
if (result.wasUpdated) {
return {
pass: true,
message: () => `Snapshot updated: ${result.snapshotPath}`,
};
}
if (!result.pass && result.diff) {
return {
pass: false,
message: () => `Snapshot mismatch for "${snapshotName}":\n\n${result.diff?.diff}\n\n` +
`Snapshot: ${result.snapshotPath}\n` +
`Run with --updateSnapshot or -u to update`,
};
}
return {
pass: true,
message: () => `Snapshot matches: ${result.snapshotPath}`,
};
},
});
}
/**
* Jest helper to create seed snapshot tests
*
* @example
* ```typescript
* import { describe, it } from '@jest/globals';
* import { createSeedTest } from '@seedts/testing/jest';
*
* describe('Seed Snapshots', () => {
* const testSeed = createSeedTest();
*
* it('users seed', async () => {
* await testSeed('users', UsersSeed, adapter);
* });
* });
* ```
*/
function createSeedTest() {
return async (name, _seedFn, _adapter, data, options) => {
const snapshotResult = await (0, snapshot_js_1.snapshot)(name, data, options);
if (!snapshotResult.pass && snapshotResult.diff) {
throw new Error(`Snapshot mismatch for "${name}":\n\n${snapshotResult.diff.diff}\n\n` +
`Snapshot: ${snapshotResult.snapshotPath}\n` +
`Run with --updateSnapshot or -u to update`);
}
return snapshotResult;
};
}
//# sourceMappingURL=jest.js.map