From 46a2d3c7d21a6dedc06ce1a3a6898611e0d4be9c Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Fri, 26 Jan 2024 07:14:22 -0800 Subject: [PATCH] new test for invariant --- packages/astro-ghostcms/package.json | 3 +- .../astro-ghostcms/src/api/invariant.test.ts | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 packages/astro-ghostcms/src/api/invariant.test.ts diff --git a/packages/astro-ghostcms/package.json b/packages/astro-ghostcms/package.json index 1b502877..087457f6 100644 --- a/packages/astro-ghostcms/package.json +++ b/packages/astro-ghostcms/package.json @@ -37,6 +37,7 @@ "email": "issues@astro-ghostcms.xyz" }, "main": "index.ts", + "types": "types.ts", "files": [ "src", ".env.demo", @@ -48,7 +49,7 @@ "exports": { ".": "./index.ts", "./api": "./src/api/index.ts", - "./api-core": "./src/api/content-api", + "./api-core": "./src/api/content-api/index.ts", "./404.astro": "./src/default-routes/404/404.astro", "./rss.xml.ts": "./src/default-routes/rss.xml.ts", "./config": "./src/integrations/virtual-config.ts", diff --git a/packages/astro-ghostcms/src/api/invariant.test.ts b/packages/astro-ghostcms/src/api/invariant.test.ts new file mode 100644 index 00000000..6473c384 --- /dev/null +++ b/packages/astro-ghostcms/src/api/invariant.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it } from "vitest"; + +const isProduction= false; +const prefix: string = 'Invariant failed'; + +const testTrue = true; +const testFalse = false; + +function invariant( + // biome-ignore lint/suspicious/noExplicitAny: we know what we are doing +condition: any, + message?: string | (() => string), +) { + if (condition) { + return; + } + if (isProduction) { + throw new Error(prefix); + } + + const provided: string | undefined = typeof message === 'function' ? message() : message; + + const value: string = provided ? `${prefix}: ${provided}` : prefix; + return value; +} + +describe('test invariant', () => { + it('Test `true` value', () => { + invariant(testTrue, "This should not error") + expect(null) + }) + it('Test `false` value', () => { + invariant(testFalse, "This should Error") + expect(String("Invariant failed")) + }) +}) \ No newline at end of file