From 305381ab044571cc76f1446cea557867970d883d Mon Sep 17 00:00:00 2001 From: Danny Choudhury Date: Tue, 3 Sep 2024 19:10:32 +0700 Subject: [PATCH] Configure and add type tests --- packages/storage/package.json | 2 + .../storage/src/__typetests__/types.test.ts | 62 +++++++++++++++++++ packages/storage/vitest.config.mts | 2 +- yarn.lock | 1 + 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 packages/storage/src/__typetests__/types.test.ts diff --git a/packages/storage/package.json b/packages/storage/package.json index faccc32091fe..bd8ab0dc5601 100644 --- a/packages/storage/package.json +++ b/packages/storage/package.json @@ -49,6 +49,7 @@ "check:package": "concurrently npm:check:attw yarn publint", "setup:test": "npx prisma db push --accept-data-loss --schema ./src/__tests__/unit-test-schema.prisma", "test": "vitest run", + "test:types": "tstyche", "test:watch": "vitest watch" }, "dependencies": { @@ -64,6 +65,7 @@ "concurrently": "8.2.2", "esbuild": "0.23.0", "publint": "0.2.10", + "tstyche": "2.1.1", "tsx": "4.17.0", "typescript": "5.5.4", "vitest": "2.0.5" diff --git a/packages/storage/src/__typetests__/types.test.ts b/packages/storage/src/__typetests__/types.test.ts new file mode 100644 index 000000000000..b369ed2f0ade --- /dev/null +++ b/packages/storage/src/__typetests__/types.test.ts @@ -0,0 +1,62 @@ +import { expect, test } from 'tstyche' + +import { MemoryStorage } from '../adapters/MemoryStorage/MemoryStorage.js' +import { setupStorage } from '../index.js' +import type { UploadsConfig } from '../prismaExtension.js' + +const uploadsConfig: UploadsConfig = { + dummy: { + fields: 'uploadField', + }, + dumbo: { + fields: ['firstUpload', 'secondUpload'], + }, +} + +const { saveFiles } = setupStorage({ + uploadsConfig, + storageAdapter: new MemoryStorage({ + baseDir: '/tmp', + }), +}) + +// const prismaClient = new PrismaClient().$extends(storagePrismaExtension) + +test('only configured models have savers', async () => { + expect(saveFiles).type.toHaveProperty('forDummy') + expect(saveFiles).type.toHaveProperty('forDumbo') + + saveFiles.forDummy({}) + // @ts-expect-error + saveFiles.forNoUploadFields({}) + + // These weren't configured above + expect(saveFiles).type.not.toHaveProperty('forNoUploadFields') + expect(saveFiles).type.not.toHaveProperty('forBook') + // expect(saveFiles).type.not.toHaveProperty('forBookCover') +}) + +test('UploadsConfig accepts all available models with their fields', async () => { + expect().type.toHaveProperty('dummy') + expect().type.toHaveProperty('dumbo') + expect().type.toHaveProperty('book') + expect().type.toHaveProperty('bookCover') + expect().type.toHaveProperty('noUploadFields') + + expect().type.toBeAssignableWith<{ + fields: ['firstUpload'] // one of the fields, but not all of them + }>() + + expect().type.toBeAssignableWith<{ + fields: ['firstUpload', 'secondUpload'] // one of the fields, but not all of them + }>() + + expect().type.toBeAssignableWith<{ + fields: ['photo'] + }>() + + // If you give it something else, it won't accept it + expect().type.not.toBeAssignableWith<{ + fields: ['bazinga'] + }>() +}) diff --git a/packages/storage/vitest.config.mts b/packages/storage/vitest.config.mts index ee7fa5cb1f93..3ed9ddfa2e83 100644 --- a/packages/storage/vitest.config.mts +++ b/packages/storage/vitest.config.mts @@ -8,7 +8,7 @@ const __dirname = path.dirname(__filename) export default defineConfig({ test: { - exclude: [...configDefaults.exclude, '**/fixtures'], + exclude: [...configDefaults.exclude, '**/fixtures', '**/__typetests__'], deps: { interopDefault: false, }, diff --git a/yarn.lock b/yarn.lock index f5072efc348b..564b91246252 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8615,6 +8615,7 @@ __metadata: esbuild: "npm:0.23.0" mime-types: "npm:2.1.35" publint: "npm:0.2.10" + tstyche: "npm:2.1.1" tsx: "npm:4.17.0" typescript: "npm:5.5.4" ulid: "npm:2.3.0"