Skip to content

Commit

Permalink
test: align to compiled results
Browse files Browse the repository at this point in the history
  • Loading branch information
davidenke committed Dec 14, 2024
1 parent ad2cf92 commit 0e8d60e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
17 changes: 9 additions & 8 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import z from 'zod';
import tsConfig from './tsconfig.json';

declare global {
function parseShape(shape: string): z.ZodType<any, any, any>;
function serializeShape(type: z.ZodType<any, any, any>): string;
function transpileFrom(source: string): string;
}

globalThis.parseShape = global.parseShape = function (shape) {
// 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('z', `return ${outputText};`)(z);
};

globalThis.serializeShape = global.serializeShape = function (type) {
const wrapped = z.strictObject({ type });
return JSON.stringify(wrapped.shape);
};

globalThis.transpileFrom = global.transpileFrom = function (source) {
// 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(source, options as unknown as TranspileOptions);
return outputText;
};

export {};
2 changes: 1 addition & 1 deletion src/transformers/field-code.transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('field-string.transform', () => {
it('always sets the code along with the language', () => {
const field = { name: 'foo', widget: 'code' } as CmsFieldBase & CmsFieldCode;
const { compiled } = transformCodeField(field);
const runtime = eval(compiled);
const runtime = parseShape(compiled);
const { shape } = runtime as ZodObject<any>;
expect(shape).toHaveProperty('code');
expect(shape).toHaveProperty('language');
Expand Down
10 changes: 6 additions & 4 deletions src/transformers/field-list.transform.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CmsField, CmsFieldBase, CmsFieldList } from 'decap-cms-core';
import z from 'zod';

import { transformListField } from './field-list.transform.js';

Expand All @@ -10,7 +9,8 @@ describe('field-list.transform', () => {
widget: 'list',
field: { name: 'foo', widget: 'text' } as CmsField,
} as CmsFieldBase & CmsFieldList;
const { runtime } = transformListField(field, z);
const { compiled } = transformListField(field);
const runtime = parseShape(compiled);
const result = ['foo', 'bar', 'baz'];
expect(() => runtime.parse(result)).not.toThrow();
});
Expand All @@ -24,7 +24,8 @@ describe('field-list.transform', () => {
{ name: 'bar', widget: 'number' } as CmsField,
],
} as CmsFieldBase & CmsFieldList;
const { runtime } = transformListField(field, z);
const { compiled } = transformListField(field);
const runtime = parseShape(compiled);
const result = [{ foo: 'foo', bar: 123 }];
expect(() => runtime.parse(result)).not.toThrow();
});
Expand All @@ -43,7 +44,8 @@ describe('field-list.transform', () => {
},
],
} as CmsFieldBase & CmsFieldList;
const { runtime } = transformListField(field, z);
const { compiled } = transformListField(field);
const runtime = parseShape(compiled);
const result = [{ type: 'foo', foo: 'foo', bar: 123 }];
expect(() => runtime.parse(result)).not.toThrow();
});
Expand Down
10 changes: 6 additions & 4 deletions src/transformers/field-number.transform.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CmsFieldBase, CmsFieldNumber } from 'decap-cms-core';
import z from 'zod';

import { transformNumberField } from './field-number.transform.js';

Expand All @@ -12,23 +11,26 @@ describe('field-number.transform', () => {

it('can be integer', () => {
const intField = { ...field, value_type: 'int' };
const { runtime } = transformNumberField(intField, z);
const { compiled } = transformNumberField(intField);
const runtime = parseShape(compiled);
const { checks } = runtime._def;
expect(checks).toHaveLength(2);
expect(checks).toEqual(expect.arrayContaining([{ kind: 'finite' }, { kind: 'int' }]));
});

it('can have a min value', () => {
const minField = { ...field, min: 1 };
const { runtime } = transformNumberField(minField, z);
const { compiled } = transformNumberField(minField);
const runtime = parseShape(compiled);
const { checks } = runtime._def;
expect(checks).toHaveLength(3);
expect(checks).toEqual(expect.arrayContaining([{ kind: 'min', inclusive: true, value: 1 }]));
});

it('can have a max value', () => {
const maxField = { ...field, max: 5 };
const { runtime } = transformNumberField(maxField, z);
const { compiled } = transformNumberField(maxField);
const runtime = parseShape(compiled);
const { checks } = runtime._def;
expect(checks).toHaveLength(3);
expect(checks).toEqual(expect.arrayContaining([{ kind: 'max', inclusive: true, value: 5 }]));
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/field-object.transform.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CmsField, CmsFieldBase, CmsFieldObject } from 'decap-cms-core';
import z from 'zod';

import { transformObjectField } from './field-object.transform.js';

Expand All @@ -13,7 +12,8 @@ describe('field-object.transform', () => {
{ name: 'bar', widget: 'number' } as CmsField,
],
} as CmsFieldBase & CmsFieldObject;
const { runtime } = transformObjectField(field, z);
const { compiled } = transformObjectField(field);
const runtime = parseShape(compiled);
const result = { foo: 'foo', bar: 123 };
expect(() => runtime.parse(result)).not.toThrow();
});
Expand Down
28 changes: 13 additions & 15 deletions src/transformers/field.transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@ describe('field.transform', () => {
});

it('delivers a transform result', () => {
expect(transformField(field, z)).toHaveProperty('runtime');
expect(transformField(field, z)).toHaveProperty('cptime');
expect(transformField(field)).toHaveProperty('compiled');
});

it('exposes a runtime Zod object', () => {
expect(transformField(field, z).runtime).toBeInstanceOf(z.ZodString);
it('exposes a Zod object at runtime', () => {
const runtime = parseShape(transformField(field).compiled);
expect(runtime).toBeInstanceOf(z.ZodString);
});

it('adds a description', () => {
// from hint, label or name
// FIXME: implement or remove if not needed
});

it('can be optional', () => {
// fttb 'nullish' → `null` or `undefined`
// FIXME: implement or remove if not needed
});

it('can have a default value', () => {});
it('can have a default value', () => {
// FIXME: implement or remove if not needed
});
});

describe.each`
Expand Down Expand Up @@ -79,18 +83,12 @@ describe('field.transform', () => {
});

it(`delivers a transform result ${desc}`, () => {
expect(transform(field, z)).toHaveProperty('runtime');
expect(transform(field, z)).toHaveProperty('cptime');
});

it(`interchangeable results ${desc}`, () => {
const { compiled: cptime, runtime } = transform(field, z);
const compiled = new Function('z', `return ${transpileFrom(cptime)};`)(z);
expect(serializeShape(compiled)).toEqual(serializeShape(runtime));
expect(transform(field)).toHaveProperty('compiled');
});

it(`exposes a runtime Zod object ${desc}`, () => {
expect(transform(field, z).runtime).toBeInstanceOf(runtype);
it(`exposes the correct runtime Zod object ${desc}`, () => {
const runtime = parseShape(transform(field).compiled);
expect(runtime).toBeInstanceOf(runtype);
});
});
});

0 comments on commit 0e8d60e

Please sign in to comment.