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",