-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjest.setup.ts
24 lines (19 loc) · 976 Bytes
/
jest.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { transpileModule, type TranspileOptions } from 'typescript';
import z from 'zod';
import tsConfig from './tsconfig.json';
declare global {
function parseShape(shape: string, deps?: Record<string, unknown>): z.ZodType<any, any, any>;
function serializeShape(type: z.ZodType<any, any, any>): string;
}
globalThis.parseShape = global.parseShape = function (shape, deps = {}) {
// remove strict mode for inline transpilation, as it would add `use strict` to the output
const options = { ...tsConfig, compilerOptions: { ...tsConfig.compilerOptions, strict: false } };
const { outputText } = transpileModule(shape, options as unknown as TranspileOptions);
// evaluate adding zod to the scope
return new Function(...Object.keys(deps), `return ${outputText};`)(...Object.values(deps));
};
globalThis.serializeShape = global.serializeShape = function (type) {
const wrapped = z.strictObject({ type });
return JSON.stringify(wrapped.shape);
};
export {};