From b9402dd3330a9db3534b7e8ab5ae26fff5517d25 Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Fri, 23 Jun 2023 08:04:08 +0200 Subject: [PATCH] test: re-create mocks for drizzle, fs-extra and redis using vi.mock --- tests/mocks/drizzle.ts | 7 +++++++ tests/mocks/fs-extra.ts | 33 +++++++++++++++++++++++++++++++++ tests/mocks/redis.ts | 32 ++++++++++++++++++++++++++++++++ tsconfig.json | 3 +++ 4 files changed, 75 insertions(+) create mode 100644 tests/mocks/drizzle.ts create mode 100644 tests/mocks/fs-extra.ts create mode 100644 tests/mocks/redis.ts diff --git a/tests/mocks/drizzle.ts b/tests/mocks/drizzle.ts new file mode 100644 index 00000000..e6157e08 --- /dev/null +++ b/tests/mocks/drizzle.ts @@ -0,0 +1,7 @@ +import { vi } from 'vitest'; + +export const mockSelect = (returnValue: T) => vi.fn(() => ({ from: vi.fn(() => ({ where: vi.fn(() => returnValue) })) })); + +export const mockInsert = (returnValue: T) => vi.fn(() => ({ values: vi.fn(() => ({ returning: vi.fn(() => returnValue) })) })); + +export const mockQuery = (returnValue: T) => ({ userTable: { findFirst: vi.fn(() => returnValue) } }); diff --git a/tests/mocks/fs-extra.ts b/tests/mocks/fs-extra.ts new file mode 100644 index 00000000..26dd3e68 --- /dev/null +++ b/tests/mocks/fs-extra.ts @@ -0,0 +1,33 @@ +import path from 'path'; + +let mockFiles = Object.create(null); +export const fsExtraMock = { + default: { + __createMockFiles: (newMockFiles: Record) => { + mockFiles = Object.create(null); + + // Create folder tree + Object.keys(newMockFiles).forEach((file) => { + const dir = path.dirname(file); + + if (!mockFiles[dir]) { + mockFiles[dir] = []; + } + + mockFiles[dir].push(path.basename(file)); + mockFiles[file] = newMockFiles[file]; + }); + }, + existsSync: (p: string) => mockFiles[p] !== undefined, + promises: { + unlink: async (p: string) => { + if (mockFiles[p] instanceof Array) { + mockFiles[p].forEach((file: string) => { + delete mockFiles[path.join(p, file)]; + }); + } + delete mockFiles[p]; + }, + }, + }, +}; diff --git a/tests/mocks/redis.ts b/tests/mocks/redis.ts new file mode 100644 index 00000000..aaf05962 --- /dev/null +++ b/tests/mocks/redis.ts @@ -0,0 +1,32 @@ +import { vi } from 'vitest'; + +export const redisMock = { + createClient: vi.fn(() => { + const values = new Map(); + const expirations = new Map(); + return { + isOpen: true, + connect: vi.fn(), + set: (key: string, value: string, exp: number) => { + values.set(key, value); + expirations.set(key, exp); + }, + get: (key: string) => values.get(key), + quit: vi.fn(), + del: (key: string) => values.delete(key), + ttl: (key: string) => expirations.get(key), + on: vi.fn(), + keys: (key: string) => { + const keyprefix = key.substring(0, key.length - 1); + const keys = []; + // eslint-disable-next-line no-restricted-syntax + for (const [k] of values) { + if (k.startsWith(keyprefix)) { + keys.push(k); + } + } + return keys; + }, + }; + }), +}; diff --git a/tsconfig.json b/tsconfig.json index 549eed29..f67613df 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,6 +18,9 @@ "@/shared/*": [ "./src/shared/*" ], + "@/tests/*": [ + "./tests/*" + ], }, "lib": [ "dom",