From 6110c2efe4be86fe88d851c073992a74ff09441d Mon Sep 17 00:00:00 2001 From: Alex / KATT Date: Sun, 1 Oct 2023 00:13:20 +0200 Subject: [PATCH] chore: fix codecov (#11) --- .gitignore | 1 + src/extend/temporal.test.ts | 1 + src/handlers/tsonUnknownObjectGuard.ts | 2 +- src/isPlainObject.test.ts | 31 ++++++++++++++++++++++++ src/{utils.ts => isPlainObject.ts} | 0 src/tson.test.ts | 33 ++++++++++++++++++++++++++ src/tson.ts | 4 ++-- 7 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/isPlainObject.test.ts rename src/{utils.ts => isPlainObject.ts} (100%) create mode 100644 src/tson.test.ts diff --git a/.gitignore b/.gitignore index a8bff3bd..ff61a71a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ coverage/ lib/ node_modules/ +.DS_Store diff --git a/src/extend/temporal.test.ts b/src/extend/temporal.test.ts index 49e90744..ec1e257e 100644 --- a/src/extend/temporal.test.ts +++ b/src/extend/temporal.test.ts @@ -18,6 +18,7 @@ const instant: TsonType = { }; const tson = createTson({ + nonce: () => "__tson", types: [plainDate, instant], }); test("PlainDate", () => { diff --git a/src/handlers/tsonUnknownObjectGuard.ts b/src/handlers/tsonUnknownObjectGuard.ts index eb5e7956..06c3a1fc 100644 --- a/src/handlers/tsonUnknownObjectGuard.ts +++ b/src/handlers/tsonUnknownObjectGuard.ts @@ -1,5 +1,5 @@ +import { isPlainObject } from "../isPlainObject.js"; import { TsonType } from "../types.js"; -import { isPlainObject } from "../utils.js"; export class UnknownObjectGuardError extends Error { /** diff --git a/src/isPlainObject.test.ts b/src/isPlainObject.test.ts new file mode 100644 index 00000000..dabeaf94 --- /dev/null +++ b/src/isPlainObject.test.ts @@ -0,0 +1,31 @@ +import { expect, test } from "vitest"; + +import { isPlainObject } from "./isPlainObject.js"; + +test("should return true for plain objects", () => { + expect(isPlainObject({})).toBe(true); + expect(isPlainObject({ a: 1 })).toBe(true); + expect(isPlainObject(Object.create(null))).toBe(true); +}); + +test("should return false for non-objects", () => { + expect(isPlainObject(null)).toBe(false); + expect(isPlainObject(undefined)).toBe(false); + expect(isPlainObject(42)).toBe(false); + expect(isPlainObject("foo")).toBe(false); + expect(isPlainObject(true)).toBe(false); + expect(isPlainObject(Symbol())).toBe(false); +}); + +test("should return false for non-plain objects", () => { + expect(isPlainObject([])).toBe(false); + expect(isPlainObject(new Date())).toBe(false); + expect(isPlainObject(/foo/)).toBe(false); + expect( + isPlainObject(() => { + // noop + }), + ).toBe(false); + + expect(isPlainObject(Object.prototype)).toBe(false); +}); diff --git a/src/utils.ts b/src/isPlainObject.ts similarity index 100% rename from src/utils.ts rename to src/isPlainObject.ts diff --git a/src/tson.test.ts b/src/tson.test.ts new file mode 100644 index 00000000..452bb9c6 --- /dev/null +++ b/src/tson.test.ts @@ -0,0 +1,33 @@ +import { expect, test } from "vitest"; + +import { createTson } from "./tson.js"; +import { TsonType } from "./types.js"; + +test("multiple handlers for primitive string found", () => { + const stringHandler: TsonType = { + primitive: "string", + }; + expect(() => { + createTson({ + types: [stringHandler, stringHandler], + }); + }).toThrowErrorMatchingInlineSnapshot( + '"Multiple handlers for primitive string found"', + ); +}); + +test("duplicate keys", () => { + const stringHandler: TsonType = { + deserialize: (v) => v, + key: "string", + serialize: (v) => v, + test: (v) => typeof v === "string", + }; + expect(() => { + createTson({ + types: [stringHandler, stringHandler], + }); + }).toThrowErrorMatchingInlineSnapshot( + '"Multiple handlers for key string found"', + ); +}); diff --git a/src/tson.ts b/src/tson.ts index 8336881d..928890fc 100644 --- a/src/tson.ts +++ b/src/tson.ts @@ -1,5 +1,6 @@ -// eslint-disable-next-line eslint-comments/disable-enable-pair +/* eslint-disable eslint-comments/disable-enable-pair */ /* eslint-disable @typescript-eslint/no-explicit-any */ +import { isPlainObject } from "./isPlainObject.js"; import { TsonAllTypes, TsonDeserializeFn, @@ -16,7 +17,6 @@ import { TsonTypeTesterCustom, TsonTypeTesterPrimitive, } from "./types.js"; -import { isPlainObject } from "./utils.js"; function isTsonTuple(v: unknown, nonce: string): v is TsonTuple { return Array.isArray(v) && v.length === 3 && v[2] === nonce;