Skip to content

Commit

Permalink
Improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin McDonnell committed Jul 9, 2020
1 parent 91a7f26 commit 9ad87d3
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 53 deletions.
5 changes: 5 additions & 0 deletions src/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ test('parse empty array in nonempty', () => {
.parse([] as any),
).toThrow();
});

test('get element', () => {
justTwo.element.parse('asdf');
expect(() => justTwo.element.parse(12)).toThrow();
});
4 changes: 4 additions & 0 deletions src/__tests__/complex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export const crazySchema = z.object({
intersection: z.intersection(z.object({ p1: z.string().optional() }), z.object({ p1: z.number().optional() })),
enum: z.intersection(z.enum(['zero', 'one']), z.enum(['one', 'two'])),
nonstrict: z.object({ points: z.number() }).nonstrict(),
numProm: z.promise(z.number()),
lenfun: z.function(z.tuple([z.string()]), z.boolean()),
});

test('parse', () => {
Expand All @@ -48,6 +50,8 @@ test('parse', () => {
intersection: {},
enum: 'one',
nonstrict: { points: 1234 },
numProm: Promise.resolve(12),
lenfun: (x: string) => x.length,
});
});

Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { util } from '../helpers/util';

test('create enum', () => {
const MyEnum = z.enum(['Red', 'Green', 'Blue']);
MyEnum.Values.Red === 'Red';
expect(MyEnum.Values.Red).toEqual('Red');
expect(MyEnum.Enum.Red).toEqual('Red');
expect(MyEnum.enum.Red).toEqual('Red');
});

test('infer enum', () => {
Expand Down
94 changes: 50 additions & 44 deletions src/__tests__/primitive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ test('literal string correct', () => {
});

test('literal string incorrect', () => {
const f = () => literalStringSchema.parse('not_asdf' as any);
const f = () => literalStringSchema.parse('not_asdf');
expect(f).toThrow();
});

test('literal string number', () => {
const f = () => literalStringSchema.parse(123 as any);
const f = () => literalStringSchema.parse(123);
expect(f).toThrow();
});

test('literal string boolean', () => {
const f = () => literalStringSchema.parse(true as any);
const f = () => literalStringSchema.parse(true);
expect(f).toThrow();
});

test('literal string boolean', () => {
const f = () => literalStringSchema.parse(true as any);
const f = () => literalStringSchema.parse(true);
expect(f).toThrow();
});

test('literal string object', () => {
const f = () => literalStringSchema.parse({} as any);
const f = () => literalStringSchema.parse({});
expect(f).toThrow();
});

Expand All @@ -58,22 +58,22 @@ test('literal number correct', () => {
});

test('literal number incorrect', () => {
const f = () => literalNumberSchema.parse(13 as any);
const f = () => literalNumberSchema.parse(13);
expect(f).toThrow();
});

test('literal number number', () => {
const f = () => literalNumberSchema.parse(val.string as any);
const f = () => literalNumberSchema.parse(val.string);
expect(f).toThrow();
});

test('literal number boolean', () => {
const f = () => literalNumberSchema.parse(val.boolean as any);
const f = () => literalNumberSchema.parse(val.boolean);
expect(f).toThrow();
});

test('literal number object', () => {
const f = () => literalStringSchema.parse({} as any);
const f = () => literalStringSchema.parse({});
expect(f).toThrow();
});

Expand All @@ -82,22 +82,22 @@ test('literal boolean correct', () => {
});

test('literal boolean incorrect', () => {
const f = () => literalBooleanSchema.parse(false as any);
const f = () => literalBooleanSchema.parse(false);
expect(f).toThrow();
});

test('literal boolean number', () => {
const f = () => literalBooleanSchema.parse('asdf' as any);
const f = () => literalBooleanSchema.parse('asdf');
expect(f).toThrow();
});

test('literal boolean boolean', () => {
const f = () => literalBooleanSchema.parse(123 as any);
const f = () => literalBooleanSchema.parse(123);
expect(f).toThrow();
});

test('literal boolean object', () => {
const f = () => literalBooleanSchema.parse({} as any);
const f = () => literalBooleanSchema.parse({});
expect(f).toThrow();
});

Expand All @@ -106,27 +106,27 @@ test('parse stringSchema string', () => {
});

test('parse stringSchema number', () => {
const f = () => stringSchema.parse(val.number as any);
const f = () => stringSchema.parse(val.number);
expect(f).toThrow();
});

test('parse stringSchema boolean', () => {
const f = () => stringSchema.parse(val.boolean as any);
const f = () => stringSchema.parse(val.boolean);
expect(f).toThrow();
});

test('parse stringSchema undefined', () => {
const f = () => stringSchema.parse(val.undefined as any);
const f = () => stringSchema.parse(val.undefined);
expect(f).toThrow();
});

test('parse stringSchema null', () => {
const f = () => stringSchema.parse(val.null as any);
const f = () => stringSchema.parse(val.null);
expect(f).toThrow();
});

test('parse numberSchema string', () => {
const f = () => numberSchema.parse(val.string as any);
const f = () => numberSchema.parse(val.string);
expect(f).toThrow();
});

Expand All @@ -135,32 +135,32 @@ test('parse numberSchema number', () => {
});

test('parse numberSchema bigint', () => {
const f = () => numberSchema.parse(val.bigint as any);
const f = () => numberSchema.parse(val.bigint);
expect(f).toThrow();
});

test('parse numberSchema boolean', () => {
const f = () => numberSchema.parse(val.boolean as any);
const f = () => numberSchema.parse(val.boolean);
expect(f).toThrow();
});

test('parse numberSchema undefined', () => {
const f = () => numberSchema.parse(val.undefined as any);
const f = () => numberSchema.parse(val.undefined);
expect(f).toThrow();
});

test('parse numberSchema null', () => {
const f = () => numberSchema.parse(val.null as any);
const f = () => numberSchema.parse(val.null);
expect(f).toThrow();
});

test('parse bigintSchema string', () => {
const f = () => bigintSchema.parse(val.string as any);
const f = () => bigintSchema.parse(val.string);
expect(f).toThrow();
});

test('parse bigintSchema number', () => {
const f = () => bigintSchema.parse(val.number as any);
const f = () => bigintSchema.parse(val.number);
expect(f).toThrow();
});

Expand All @@ -169,27 +169,27 @@ test('parse bigintSchema bigint', () => {
});

test('parse bigintSchema boolean', () => {
const f = () => bigintSchema.parse(val.boolean as any);
const f = () => bigintSchema.parse(val.boolean);
expect(f).toThrow();
});

test('parse bigintSchema undefined', () => {
const f = () => bigintSchema.parse(val.undefined as any);
const f = () => bigintSchema.parse(val.undefined);
expect(f).toThrow();
});

test('parse bigintSchema null', () => {
const f = () => bigintSchema.parse(val.null as any);
const f = () => bigintSchema.parse(val.null);
expect(f).toThrow();
});

test('parse booleanSchema string', () => {
const f = () => booleanSchema.parse(val.string as any);
const f = () => booleanSchema.parse(val.string);
expect(f).toThrow();
});

test('parse booleanSchema number', () => {
const f = () => booleanSchema.parse(val.number as any);
const f = () => booleanSchema.parse(val.number);
expect(f).toThrow();
});

Expand All @@ -198,29 +198,29 @@ test('parse booleanSchema boolean', () => {
});

test('parse booleanSchema undefined', () => {
const f = () => booleanSchema.parse(val.undefined as any);
const f = () => booleanSchema.parse(val.undefined);
expect(f).toThrow();
});

test('parse booleanSchema null', () => {
const f = () => booleanSchema.parse(val.null as any);
const f = () => booleanSchema.parse(val.null);
expect(f).toThrow();
});

// ==============

test('parse dateSchema string', () => {
const f = () => dateSchema.parse(val.string as any);
const f = () => dateSchema.parse(val.string);
expect(f).toThrow();
});

test('parse dateSchema number', () => {
const f = () => dateSchema.parse(val.number as any);
const f = () => dateSchema.parse(val.number);
expect(f).toThrow();
});

test('parse dateSchema boolean', () => {
const f = () => dateSchema.parse(val.boolean as any);
const f = () => dateSchema.parse(val.boolean);
expect(f).toThrow();
});

Expand All @@ -229,28 +229,34 @@ test('parse dateSchema date', () => {
});

test('parse dateSchema undefined', () => {
const f = () => dateSchema.parse(val.undefined as any);
const f = () => dateSchema.parse(val.undefined);
expect(f).toThrow();
});

test('parse dateSchema null', () => {
const f = () => dateSchema.parse(val.null as any);
const f = () => dateSchema.parse(val.null);
expect(f).toThrow();
});
test('parse dateSchema invalid date', () => {
expect.assertions(1);
dateSchema.parseAsync(new Date('invalid')).catch(err => {
expect(err.errors[0].code).toEqual(z.ZodErrorCode.invalid_date);
});
});
// ==============

test('parse undefinedSchema string', () => {
const f = () => undefinedSchema.parse(val.string as any);
const f = () => undefinedSchema.parse(val.string);
expect(f).toThrow();
});

test('parse undefinedSchema number', () => {
const f = () => undefinedSchema.parse(val.number as any);
const f = () => undefinedSchema.parse(val.number);
expect(f).toThrow();
});

test('parse undefinedSchema boolean', () => {
const f = () => undefinedSchema.parse(val.boolean as any);
const f = () => undefinedSchema.parse(val.boolean);
expect(f).toThrow();
});

Expand All @@ -259,27 +265,27 @@ test('parse undefinedSchema undefined', () => {
});

test('parse undefinedSchema null', () => {
const f = () => undefinedSchema.parse(val.null as any);
const f = () => undefinedSchema.parse(val.null);
expect(f).toThrow();
});

test('parse nullSchema string', () => {
const f = () => nullSchema.parse(val.string as any);
const f = () => nullSchema.parse(val.string);
expect(f).toThrow();
});

test('parse nullSchema number', () => {
const f = () => nullSchema.parse(val.number as any);
const f = () => nullSchema.parse(val.number);
expect(f).toThrow();
});

test('parse nullSchema boolean', () => {
const f = () => nullSchema.parse(val.boolean as any);
const f = () => nullSchema.parse(val.boolean);
expect(f).toThrow();
});

test('parse nullSchema undefined', () => {
const f = () => nullSchema.parse(val.undefined as any);
const f = () => nullSchema.parse(val.undefined);
expect(f).toThrow();
});

Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/record.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ test('string record parse - fail', () => {
asdf: 1234,
} as any);
expect(badCheck).toThrow();

expect(() => booleanRecord.parse('asdf')).toThrow();
});

test('string record parse - fail', () => {
Expand Down
1 change: 1 addition & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ export const ZodParser = (schemaDef: z.ZodTypeDef) => (
throw error;
}
if (isNaN(obj.getTime())) {
console.log('NAN');
error.addError(
makeError({
code: ZodErrorCode.invalid_date,
Expand Down
2 changes: 2 additions & 0 deletions src/playground.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// import * as z from '.';
// z.date().parse(new Date('invalid'));
1 change: 0 additions & 1 deletion src/types/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export class ZodFunction<Args extends ZodTuple<any>, Returns extends z.ZodTypeAn

implement = (func: TypeOfFunction<Args, Returns>): TypeOfFunction<Args, Returns> => {
const validatedFunc = this.parse(func);

return (validatedFunc as any) as TypeOfFunction<Args, Returns>;
};

Expand Down
7 changes: 0 additions & 7 deletions src/types/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,4 @@ export class ZodUnion<T extends [z.ZodTypeAny, z.ZodTypeAny, ...z.ZodTypeAny[]]>
options: types,
});
};

static make = <T extends [z.ZodTypeAny, z.ZodTypeAny, ...z.ZodTypeAny[]]>(...types: T): ZodUnion<T> => {
return new ZodUnion({
t: z.ZodTypes.union,
options: types,
});
};
}

0 comments on commit 9ad87d3

Please sign in to comment.