mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix(test): mock db/index.js in unit tests that import API modules
Five unit tests crashed because they imported modules that transitively reached db/index.ts, which eagerly calls `new Database()` at module load time. Added vi.mock for db/index.js matching the pattern used by 16 other passing API unit tests. Moved cleanup.test.ts to tests/integration/ since it uses a real SQLite database, runs migrations, and inserts rows.
This commit is contained in:
@@ -1,4 +1,10 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
vi.mock("../../../apps/api/src/db/index.js", () => ({
|
||||||
|
db: {},
|
||||||
|
schema: {},
|
||||||
|
}));
|
||||||
|
|
||||||
import {
|
import {
|
||||||
computeKeyPrefix,
|
computeKeyPrefix,
|
||||||
hashPassword,
|
hashPassword,
|
||||||
|
|||||||
@@ -8,7 +8,13 @@
|
|||||||
import type { Permission, Role } from "@snapotter/shared";
|
import type { Permission, Role } from "@snapotter/shared";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
// Mock the auth plugin to avoid transitively opening a SQLite connection
|
vi.mock("../../../apps/api/src/db/index.js", () => ({
|
||||||
|
db: {
|
||||||
|
select: () => ({ from: () => ({ where: () => ({ get: () => null }) }) }),
|
||||||
|
},
|
||||||
|
schema: { roles: {}, settings: {} },
|
||||||
|
}));
|
||||||
|
|
||||||
vi.mock("../../../apps/api/src/plugins/auth.js", () => ({
|
vi.mock("../../../apps/api/src/plugins/auth.js", () => ({
|
||||||
getAuthUser: () => null,
|
getAuthUser: () => null,
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -8,9 +8,13 @@
|
|||||||
import type { Role } from "@snapotter/shared";
|
import type { Role } from "@snapotter/shared";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
// Mock the auth plugin to avoid transitively opening a SQLite connection
|
vi.mock("../../../apps/api/src/db/index.js", () => ({
|
||||||
// (permissions.ts -> auth.ts -> db/index.ts), which causes lock contention
|
db: {
|
||||||
// when running in parallel with other DB-using test files like cleanup.test.ts.
|
select: () => ({ from: () => ({ where: () => ({ get: () => null }) }) }),
|
||||||
|
},
|
||||||
|
schema: { roles: {}, settings: {} },
|
||||||
|
}));
|
||||||
|
|
||||||
vi.mock("../../../apps/api/src/plugins/auth.js", () => ({
|
vi.mock("../../../apps/api/src/plugins/auth.js", () => ({
|
||||||
getAuthUser: () => null,
|
getAuthUser: () => null,
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -1,4 +1,16 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
vi.mock("../../../apps/api/src/db/index.js", () => ({
|
||||||
|
db: {
|
||||||
|
select: () => ({ from: () => ({ where: () => ({ get: () => null }) }) }),
|
||||||
|
},
|
||||||
|
schema: { roles: {}, settings: {} },
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../../../apps/api/src/plugins/auth.js", () => ({
|
||||||
|
getAuthUser: () => null,
|
||||||
|
}));
|
||||||
|
|
||||||
import { getPermissions, hasPermission } from "../../../apps/api/src/permissions.js";
|
import { getPermissions, hasPermission } from "../../../apps/api/src/permissions.js";
|
||||||
|
|
||||||
describe("role permissions", () => {
|
describe("role permissions", () => {
|
||||||
|
|||||||
@@ -6,7 +6,13 @@
|
|||||||
* - Auth Zod schemas enforce max length on username/password fields
|
* - Auth Zod schemas enforce max length on username/password fields
|
||||||
* - New storage env vars have correct defaults
|
* - New storage env vars have correct defaults
|
||||||
*/
|
*/
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
vi.mock("../../apps/api/src/db/index.js", () => ({
|
||||||
|
db: {},
|
||||||
|
schema: {},
|
||||||
|
}));
|
||||||
|
|
||||||
import { loadEnv } from "../../apps/api/src/lib/env.js";
|
import { loadEnv } from "../../apps/api/src/lib/env.js";
|
||||||
import {
|
import {
|
||||||
changePasswordSchema,
|
changePasswordSchema,
|
||||||
|
|||||||
Reference in New Issue
Block a user