refactor: update test files to use bun:test and remove vitest configuration

This commit is contained in:
Arunavo Ray
2025-06-12 10:24:55 +05:30
parent 51de51baa0
commit 2eda800a7c
5 changed files with 70 additions and 60 deletions

View File

@@ -127,7 +127,9 @@ describe("GitHub Test Connection API", () => {
expect(response.status).toBe(500);
const data = await response.json();
expect(data.success).toBe(false);
expect(data.message).toContain("Bad credentials");
// The createSecureErrorResponse function returns an error field, not success
// It sanitizes error messages for security, so we expect the generic message
expect(data.error).toBeDefined();
expect(data.error).toBe("An internal server error occurred");
});
});

View File

@@ -1,34 +1,68 @@
import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test";
import type { MirrorRepoRequest } from "@/types/mirror";
import { POST } from "./mirror-repo";
// Mock the database module
const mockDb = {
select: mock(() => ({
from: mock(() => ({
where: mock(() => ({
limit: mock(() => Promise.resolve([{
id: "config-id",
userId: "user-id",
githubConfig: {
token: "github-token",
preserveOrgStructure: false,
mirrorIssues: false
from: mock((table: any) => ({
where: mock(() => {
// Return config for configs table
if (table === mockConfigs) {
return {
limit: mock(() => Promise.resolve([{
id: "config-id",
userId: "user-id",
githubConfig: {
token: "github-token",
preserveOrgStructure: false,
mirrorIssues: false
},
giteaConfig: {
url: "https://gitea.example.com",
token: "gitea-token",
username: "giteauser"
}
}]))
};
}
// Return repositories for repositories table
return Promise.resolve([
{
id: "repo-id-1",
name: "test-repo-1",
visibility: "public",
status: "pending",
organization: null,
lastMirrored: null,
errorMessage: null,
forkedFrom: null,
mirroredLocation: ""
},
giteaConfig: {
url: "https://gitea.example.com",
token: "gitea-token",
username: "giteauser"
{
id: "repo-id-2",
name: "test-repo-2",
visibility: "public",
status: "pending",
organization: null,
lastMirrored: null,
errorMessage: null,
forkedFrom: null,
mirroredLocation: ""
}
}]))
}))
]);
})
}))
}))
};
const mockConfigs = {};
const mockRepositories = {};
mock.module("@/lib/db", () => ({
db: mockDb,
configs: {},
repositories: {}
configs: mockConfigs,
repositories: mockRepositories
}));
// Mock the gitea module
@@ -98,12 +132,13 @@ describe("Repository Mirroring API", () => {
})
});
const response = await mockModule.POST({ request } as any);
const response = await POST({ request } as any);
expect(response.status).toBe(400);
const data = await response.json();
expect(data.error).toBe("Missing userId or repositoryIds.");
expect(data.success).toBe(false);
expect(data.message).toBe("userId and repositoryIds are required.");
});
test("returns 400 if repositoryIds is missing", async () => {
@@ -117,12 +152,13 @@ describe("Repository Mirroring API", () => {
})
});
const response = await mockModule.POST({ request } as any);
const response = await POST({ request } as any);
expect(response.status).toBe(400);
const data = await response.json();
expect(data.error).toBe("Missing userId or repositoryIds.");
expect(data.success).toBe(false);
expect(data.message).toBe("userId and repositoryIds are required.");
});
test("returns 200 and starts mirroring repositories", async () => {
@@ -137,13 +173,13 @@ describe("Repository Mirroring API", () => {
})
});
const response = await mockModule.POST({ request } as any);
const response = await POST({ request } as any);
expect(response.status).toBe(200);
const data = await response.json();
expect(data.success).toBe(true);
expect(data.message).toBe("Repository mirroring started");
expect(data.batchId).toBe("test-batch-id");
expect(data.message).toBe("Mirror job started.");
expect(data.repositories).toBeDefined();
});
});

View File

@@ -1,8 +1,8 @@
// example.test.ts
import { describe, it, expect } from "vitest";
import { describe, test, expect } from "bun:test";
describe("Example Test", () => {
it("should pass", () => {
test("should pass", () => {
expect(true).toBe(true);
});
});

View File

@@ -1,8 +0,0 @@
import "@testing-library/jest-dom";
import { expect, afterEach } from "vitest";
import { cleanup } from "@testing-library/react";
// Run cleanup after each test case (e.g. clearing jsdom)
afterEach(() => {
cleanup();
});

View File

@@ -1,20 +0,0 @@
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';
import { fileURLToPath } from 'url';
import path from 'path';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export default defineConfig({
plugins: [react()],
test: {
environment: 'jsdom',
globals: true,
setupFiles: ['./src/tests/setup.ts'],
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
});