feat(tests): add mock database setup for Vitest testing environment

- Introduced a new mock database setup file to simulate database interactions during tests.
- Updated Vitest configuration to include the mock setup file, enhancing test reliability and isolation.
This commit is contained in:
Mauricio Siu
2026-02-04 09:20:19 -06:00
parent dc8148ae51
commit 8335f40238
2 changed files with 57 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
import { vi } from "vitest";
// Mock postgres to prevent actual database connections
vi.mock("postgres", () => {
return {
default: vi.fn(() => ({
end: vi.fn(),
unsafe: vi.fn(),
})),
};
});
// Mock the db export from @dokploy/server/db
vi.mock("@dokploy/server/db", () => {
const createChainableMock = (): any => {
const chain: any = {
set: vi.fn(() => chain),
where: vi.fn(() => chain),
returning: vi.fn().mockResolvedValue([{}]),
execute: vi.fn().mockResolvedValue([{}]),
};
return chain;
};
return {
db: {
select: vi.fn(() => createChainableMock()),
insert: vi.fn(() => createChainableMock()),
update: vi.fn(() => createChainableMock()),
delete: vi.fn(() => createChainableMock()),
query: {
applications: {
findFirst: vi.fn(),
findMany: vi.fn(),
},
deployments: {
findFirst: vi.fn(),
findMany: vi.fn(),
},
servers: {
findFirst: vi.fn(),
findMany: vi.fn(),
},
users: {
findFirst: vi.fn(),
findMany: vi.fn(),
},
organizations: {
findFirst: vi.fn(),
findMany: vi.fn(),
},
},
},
dbUrl: "postgres://mock:mock@localhost:5432/mock",
};
});

View File

@@ -7,6 +7,7 @@ export default defineConfig({
include: ["__test__/**/*.test.ts"], // Incluir solo los archivos de test en el directorio __test__
exclude: ["**/node_modules/**", "**/dist/**", "**/.docker/**"],
pool: "forks",
setupFiles: ["./__test__/setup/mock-db.ts"],
},
define: {
"process.env": {