Skip to content

Commit

Permalink
test: update jest; update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrisnl committed Dec 12, 2022
1 parent ec3ae01 commit d627749
Show file tree
Hide file tree
Showing 5 changed files with 842 additions and 923 deletions.
19 changes: 0 additions & 19 deletions config/jest.config.ts

This file was deleted.

5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
collectCoverageFrom: ['lib/**/*.{js,ts}'],
testRegex: './lib/.*\\.(test|spec)\\.(js|ts)$',
};
105 changes: 59 additions & 46 deletions lib/ValidationError.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as zod from 'zod';
import { ZodError } from 'zod';

import {
fromZodError,
Expand All @@ -13,21 +14,23 @@ describe('fromZodError()', () => {
try {
emailSchema.parse('foobar');
} catch (err) {
const validationError = fromZodError(err);
expect(validationError).toBeInstanceOf(ValidationError);
expect(validationError.message).toMatchInlineSnapshot(
`"Validation error: Invalid email"`
);
expect(validationError.details).toMatchInlineSnapshot(`
Array [
Object {
if (err instanceof ZodError) {
const validationError = fromZodError(err);
expect(validationError).toBeInstanceOf(ValidationError);
expect(validationError.message).toMatchInlineSnapshot(
`"Validation error: Invalid email"`
);
expect(validationError.details).toMatchInlineSnapshot(`
[
{
"code": "invalid_string",
"message": "Invalid email",
"path": Array [],
"path": [],
"validation": "email",
},
]
`);
}
}
});

Expand All @@ -43,35 +46,39 @@ describe('fromZodError()', () => {
name: 'a',
});
} catch (err) {
const validationError = fromZodError(err);
expect(validationError).toBeInstanceOf(ValidationError);
expect(validationError.message).toMatchInlineSnapshot(
`"Validation error: Number must be greater than 0 at \\"id\\"; String must contain at least 2 character(s) at \\"name\\""`
);
expect(validationError.details).toMatchInlineSnapshot(`
Array [
Object {
if (err instanceof ZodError) {
const validationError = fromZodError(err);
expect(validationError).toBeInstanceOf(ValidationError);
expect(validationError.message).toMatchInlineSnapshot(
`"Validation error: Number must be greater than 0 at "id"; String must contain at least 2 character(s) at "name""`
);
expect(validationError.details).toMatchInlineSnapshot(`
[
{
"code": "too_small",
"exact": false,
"inclusive": false,
"message": "Number must be greater than 0",
"minimum": 0,
"path": Array [
"path": [
"id",
],
"type": "number",
},
Object {
{
"code": "too_small",
"exact": false,
"inclusive": true,
"message": "String must contain at least 2 character(s)",
"minimum": 2,
"path": Array [
"path": [
"name",
],
"type": "string",
},
]
`);
}
}
});

Expand All @@ -81,42 +88,44 @@ describe('fromZodError()', () => {
try {
objSchema.parse([1, 'a', true, 1.23]);
} catch (err) {
const validationError = fromZodError(err);
expect(validationError).toBeInstanceOf(ValidationError);
expect(validationError.message).toMatchInlineSnapshot(
`"Validation error: Expected number, received string at \\"[1]\\"; Expected number, received boolean at \\"[2]\\"; Expected integer, received float at \\"[3]\\""`
);
expect(validationError.details).toMatchInlineSnapshot(`
Array [
Object {
if (err instanceof ZodError) {
const validationError = fromZodError(err);
expect(validationError).toBeInstanceOf(ValidationError);
expect(validationError.message).toMatchInlineSnapshot(
`"Validation error: Expected number, received string at "[1]"; Expected number, received boolean at "[2]"; Expected integer, received float at "[3]""`
);
expect(validationError.details).toMatchInlineSnapshot(`
[
{
"code": "invalid_type",
"expected": "number",
"message": "Expected number, received string",
"path": Array [
"path": [
1,
],
"received": "string",
},
Object {
{
"code": "invalid_type",
"expected": "number",
"message": "Expected number, received boolean",
"path": Array [
"path": [
2,
],
"received": "boolean",
},
Object {
{
"code": "invalid_type",
"expected": "integer",
"message": "Expected integer, received float",
"path": Array [
"path": [
3,
],
"received": "float",
},
]
`);
}
}
});

Expand All @@ -138,46 +147,50 @@ describe('fromZodError()', () => {
},
});
} catch (err) {
const validationError = fromZodError(err);
expect(validationError).toBeInstanceOf(ValidationError);
expect(validationError.message).toMatchInlineSnapshot(
`"Validation error: Number must be greater than 0 at \\"id\\"; Expected number, received string at \\"arr[1]\\"; String must contain at least 2 character(s) at \\"nestedObj.name\\""`
);
expect(validationError.details).toMatchInlineSnapshot(`
Array [
Object {
if (err instanceof ZodError) {
const validationError = fromZodError(err);
expect(validationError).toBeInstanceOf(ValidationError);
expect(validationError.message).toMatchInlineSnapshot(
`"Validation error: Number must be greater than 0 at "id"; Expected number, received string at "arr[1]"; String must contain at least 2 character(s) at "nestedObj.name""`
);
expect(validationError.details).toMatchInlineSnapshot(`
[
{
"code": "too_small",
"exact": false,
"inclusive": false,
"message": "Number must be greater than 0",
"minimum": 0,
"path": Array [
"path": [
"id",
],
"type": "number",
},
Object {
{
"code": "invalid_type",
"expected": "number",
"message": "Expected number, received string",
"path": Array [
"path": [
"arr",
1,
],
"received": "string",
},
Object {
{
"code": "too_small",
"exact": false,
"inclusive": true,
"message": "String must contain at least 2 character(s)",
"minimum": 2,
"path": Array [
"path": [
"nestedObj",
"name",
],
"type": "string",
},
]
`);
}
}
});
});
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"build": "rimraf dist && concurrently \"yarn build:types\" \"yarn build:esm\" \"yarn build:cjs\"",
"lint": "eslint lib --ext .ts",
"format": "prettier --config ./.prettierrc --ignore-path .gitignore -w .",
"test": "jest --config ./config/jest.config.ts",
"coverage": "jest --config jest.config.ts --coverage",
"test": "jest",
"coverage": "jest --coverage",
"changeset": "changeset",
"prerelease": "yarn build && yarn test",
"release": "changeset publish"
Expand All @@ -68,8 +68,8 @@
"@changesets/cli": "^2.18.1",
"@commitlint/cli": "^15.0.0",
"@commitlint/config-conventional": "^15.0.0",
"@types/jest": "^27.0.3",
"@types/node": "^17.0.0",
"@types/jest": "^29.2.4",
"@types/node": "^18.11.13",
"@typescript-eslint/eslint-plugin": "^5.7.0",
"@typescript-eslint/parser": "^5.7.0",
"concurrently": "^6.5.1",
Expand All @@ -78,11 +78,11 @@
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"husky": "4",
"jest": "^27.0.6",
"jest": "^29.3.1",
"lint-staged": "^12.1.2",
"prettier": "^2.2.1",
"rimraf": "^3.0.0",
"ts-jest": "^27.0.5",
"ts-jest": "^29.0.3",
"typescript": "^4.9.4",
"zod": "^3.20.0"
},
Expand Down
Loading

0 comments on commit d627749

Please sign in to comment.