1
0

generated-examples.test.ts 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import { describe, it, expect } from 'vitest';
  2. import type { ZodType } from 'zod';
  3. import { EXAMPLES } from '@/generated/examples';
  4. import * as zodSchemas from '@/generated/zod';
  5. const registry = zodSchemas as unknown as Record<string, ZodType>;
  6. const names = Object.keys(EXAMPLES);
  7. describe('generated response examples', () => {
  8. it('has at least one example to validate', () => {
  9. expect(names.length).toBeGreaterThan(0);
  10. });
  11. it('pairs every example with a generated zod schema', () => {
  12. const missing = names.filter((name) => typeof registry[`${name}Schema`]?.safeParse !== 'function');
  13. expect(missing).toEqual([]);
  14. });
  15. it.each(names)('EXAMPLES.%s satisfies its generated zod schema', (name) => {
  16. const schema = registry[`${name}Schema`];
  17. const result = schema.safeParse(EXAMPLES[name]);
  18. if (!result.success) {
  19. throw new Error(
  20. `EXAMPLES.${name} does not match ${name}Schema:\n${JSON.stringify(result.error.issues, null, 2)}`,
  21. );
  22. }
  23. expect(result.success).toBe(true);
  24. });
  25. });