test: migrate from bun to pnpm and vitest

This commit is contained in:
2025-12-06 18:14:01 -05:00
parent 20ad68669c
commit bdac969be9
31 changed files with 228 additions and 2103 deletions

View File

@@ -1,4 +1,4 @@
import { describe, expect, it } from "bun:test";
import { describe, expect, it } from "vitest";
import { Case, changeStringCase } from "./changeStringCase";

View File

@@ -1,10 +1,10 @@
import { describe, expect, it } from "bun:test";
import { describe, expect, it } from "vitest";
import { fetchFromMultipleSources } from "./fetchFromMultipleSources";
describe("fetchFromMultipleSources", () => {
it("no promises, throws exception", () => {
expect(() => fetchFromMultipleSources([])).toThrow(
it("no promises, throws exception", async () => {
await expect(fetchFromMultipleSources([])).rejects.toThrow(
"fetchPromises cannot be empty",
);
});
@@ -30,7 +30,7 @@ describe("fetchFromMultipleSources", () => {
() => Promise.resolve(3),
]);
expect(errorOccurred).toBeFalse();
expect(errorOccurred).toBe(false);
});
it("has promises that all throw, returns null", async () => {
@@ -48,7 +48,7 @@ describe("fetchFromMultipleSources", () => {
() => Promise.reject(new Error("error")),
]);
expect(errorOccurred).toBeTrue();
expect(errorOccurred).toBe(true);
});
it("has promises but cache has value, returns cached value", async () => {
@@ -80,7 +80,7 @@ describe("fetchFromMultipleSources", () => {
},
);
expect(errorOccurred).toBeFalse();
expect(errorOccurred).toBe(false);
});
it("has promises, no cached value, no valid response, should not save in cache", async () => {

View File

@@ -1,12 +1,8 @@
import { describe, expect, it } from "bun:test";
import { server } from "~/mocks";
import { describe, expect, it } from "vitest";
import type { AdminSdkCredentials } from "./getAdminSdkCredentials";
import { verifyFcmToken } from "./verifyFcmToken";
server.listen();
const FAKE_ADMIN_SDK_JSON: AdminSdkCredentials = {
type: "service_account",
projectId: "test-26g38",
@@ -28,14 +24,14 @@ describe("verifyFcmToken", () => {
// "7v8sy43aq0re4r8xe7rmr0cn1fsmh6phehnfla2pa73z899zmhyarivmkt4sj6pyv0py43u6p2sim6wz2vg9ypjp9rug1keoth7f6ll3gdvas4q020u3ah51r6bjgn51j6bd92ztmtof3ljpcm8q31njvndy65enm68";
// const res = await verifyFcmToken(token, FAKE_ADMIN_SDK_JSON);
// expect(res).toBeTrue();
// expect(res).toBe(true);
// });
it("invalid token, returns false", async () => {
const token = "abc123";
const res = await verifyFcmToken(token, FAKE_ADMIN_SDK_JSON);
expect(res).toBeFalse();
expect(res).toBe(false);
});
it("invalid ADMIN_SDK_JSON, returns false", async () => {
@@ -46,6 +42,6 @@ describe("verifyFcmToken", () => {
clientEmail: "",
});
expect(res).toBeFalse();
expect(res).toBe(false);
});
});

View File

@@ -1,6 +1,5 @@
import { DateTime } from "luxon";
import { describe, expect, it } from "bun:test";
import { describe, expect, it } from "vitest";
import { getCurrentAndNextSeason } from "./getCurrentAndNextSeason";

View File

@@ -1,4 +1,4 @@
import { describe, expect, it } from "bun:test";
import { describe, expect, it } from "vitest";
import { lazy } from "./lazy";
@@ -16,7 +16,7 @@ describe("lazy", () => {
return "value";
});
expect(setValue).toBeFalse();
expect(setValue).toBe(false);
});
it("lazy function called if get is called", () => {
@@ -26,7 +26,7 @@ describe("lazy", () => {
return "value";
}).get();
expect(setValue).toBeTrue();
expect(setValue).toBe(true);
});
it("lazy function called only once if get is called multiple times", () => {

View File

@@ -1,4 +1,4 @@
import { describe, expect, it } from "bun:test";
import { describe, expect, it } from "vitest";
import { PromiseTimedOutError, promiseTimeout } from "./promiseTimeout";

View File

@@ -1,4 +1,4 @@
import { describe, expect, it } from "bun:test";
import { describe, expect, it } from "vitest";
import { readEnvVariable } from "./readEnvVariable";

View File

@@ -1,4 +1,4 @@
import { describe, expect, it } from "bun:test";
import { describe, expect, it } from "vitest";
import { sortByProperty } from "./sortByProperty";

View File

@@ -1,6 +1,5 @@
import { DateTime } from "luxon";
import { beforeEach, describe, expect, it, mock } from "bun:test";
import { beforeEach, describe, expect, it, mock } from "vitest";
import type { DelayedTaskMetadata } from "./delayedTask";
import {

View File

@@ -1,28 +1,28 @@
import { beforeEach, describe, expect, it, mock } from "bun:test";
import { type Mock, beforeEach, describe, expect, it, vi } from "vitest";
import { processDelayedTasks } from "./processDelayedTasks";
describe("processDelayedTasks", () => {
let mockEnv: Cloudflare.Env;
let mockCtx: ExecutionContext;
let kvGetSpy: ReturnType<typeof mock>;
let kvDeleteSpy: ReturnType<typeof mock>;
let kvPutSpy: ReturnType<typeof mock>;
let queueSendSpy: ReturnType<typeof mock>;
let kvGetSpy: ReturnType<typeof vi.fn>;
let kvDeleteSpy: ReturnType<typeof vi.fn>;
let kvPutSpy: ReturnType<typeof vi.fn>;
let queueSendSpy: ReturnType<typeof vi.fn>;
beforeEach(() => {
kvGetSpy = mock(() => Promise.resolve(null));
kvDeleteSpy = mock(() => Promise.resolve());
kvPutSpy = mock(() => Promise.resolve());
queueSendSpy = mock(() => Promise.resolve());
kvGetSpy = vi.fn(() => Promise.resolve(null));
kvDeleteSpy = vi.fn(() => Promise.resolve());
kvPutSpy = vi.fn(() => Promise.resolve());
queueSendSpy = vi.fn(() => Promise.resolve());
mockEnv = {
DELAYED_TASKS: {
get: kvGetSpy,
delete: kvDeleteSpy,
put: kvPutSpy,
list: mock(() => Promise.resolve({ keys: [], list_complete: true })),
getWithMetadata: mock(() =>
list: vi.fn(() => Promise.resolve({ keys: [], list_complete: true })),
getWithMetadata: vi.fn(() =>
Promise.resolve({ value: null, metadata: null }),
),
} as any,
@@ -30,13 +30,13 @@ describe("processDelayedTasks", () => {
send: queueSendSpy,
} as any,
ANILIST_UPDATES: {
send: mock(() => Promise.resolve()),
send: vi.fn(() => Promise.resolve()),
} as any,
} as any;
mockCtx = {
waitUntil: mock(() => {}),
passThroughOnException: mock(() => {}),
waitUntil: vi.fn(() => {}),
passThroughOnException: vi.fn(() => {}),
} as any;
});
@@ -61,7 +61,7 @@ describe("processDelayedTasks", () => {
retryCount: 0,
};
mockEnv.DELAYED_TASKS.list = mock(() =>
mockEnv.DELAYED_TASKS.list = vi.fn(() =>
Promise.resolve({
keys: [{ name: `delayed-task:${scheduledTime}:task-1` }],
list_complete: true,
@@ -93,7 +93,7 @@ describe("processDelayedTasks", () => {
retryCount: 0,
};
mockEnv.DELAYED_TASKS.list = mock(() =>
mockEnv.DELAYED_TASKS.list = vi.fn(() =>
Promise.resolve({
keys: [{ name: `delayed-task:${scheduledTime}:task-2` }],
list_complete: true,
@@ -122,7 +122,7 @@ describe("processDelayedTasks", () => {
retryCount: 0,
};
mockEnv.DELAYED_TASKS.list = mock(() =>
mockEnv.DELAYED_TASKS.list = vi.fn(() =>
Promise.resolve({
keys: [{ name: `delayed-task:${scheduledTime}:task-3` }],
list_complete: true,
@@ -141,7 +141,7 @@ describe("processDelayedTasks", () => {
});
it("logs alert after 3 failed attempts", async () => {
const consoleErrorSpy = mock(() => {});
const consoleErrorSpy = vi.fn(() => {});
const originalConsoleError = console.error;
console.error = consoleErrorSpy as any;
@@ -158,7 +158,7 @@ describe("processDelayedTasks", () => {
retryCount: 2, // Will become 3 after this failure
};
mockEnv.DELAYED_TASKS.list = mock(() =>
mockEnv.DELAYED_TASKS.list = vi.fn(() =>
Promise.resolve({
keys: [{ name: `delayed-task:${scheduledTime}:task-4` }],
list_complete: true,
@@ -202,7 +202,7 @@ describe("processDelayedTasks", () => {
retryCount: 0,
};
mockEnv.DELAYED_TASKS.list = mock(() =>
mockEnv.DELAYED_TASKS.list = vi.fn(() =>
Promise.resolve({
keys: [
{ name: `delayed-task:${task1Metadata.scheduledEpochTime}:task-1` },
@@ -223,7 +223,7 @@ describe("processDelayedTasks", () => {
});
it("skips tasks with null values in KV", async () => {
mockEnv.DELAYED_TASKS.list = mock(() =>
mockEnv.DELAYED_TASKS.list = vi.fn(() =>
Promise.resolve({
keys: [{ name: "delayed-task:123:invalid" }],
list_complete: true,

View File

@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, mock, spyOn } from "bun:test";
import { type Mock, beforeEach, describe, expect, it, spyOn, vi } from "vitest";
import { queueTask } from "./queueTask";
@@ -6,20 +6,20 @@ describe("queueTask - delayed task handling", () => {
const MAX_DELAY_SECONDS = 12 * 60 * 60; // 43,200 seconds
let mockEnv: Cloudflare.Env;
let kvPutSpy: ReturnType<typeof mock>;
let queueSendSpy: ReturnType<typeof mock>;
let kvPutSpy: ReturnType<typeof vi.fn>;
let queueSendSpy: ReturnType<typeof vi.fn>;
beforeEach(() => {
kvPutSpy = mock(() => Promise.resolve());
queueSendSpy = mock(() => Promise.resolve());
kvPutSpy = vi.fn(() => Promise.resolve());
queueSendSpy = vi.fn(() => Promise.resolve());
mockEnv = {
DELAYED_TASKS: {
put: kvPutSpy,
get: mock(() => Promise.resolve(null)),
delete: mock(() => Promise.resolve()),
list: mock(() => Promise.resolve({ keys: [], list_complete: true })),
getWithMetadata: mock(() =>
get: vi.fn(() => Promise.resolve(null)),
delete: vi.fn(() => Promise.resolve()),
list: vi.fn(() => Promise.resolve({ keys: [], list_complete: true })),
getWithMetadata: vi.fn(() =>
Promise.resolve({ value: null, metadata: null }),
),
} as any,
@@ -27,12 +27,12 @@ describe("queueTask - delayed task handling", () => {
send: queueSendSpy,
} as any,
ANILIST_UPDATES: {
send: mock(() => Promise.resolve()),
send: vi.fn(() => Promise.resolve()),
} as any,
} as any;
// Mock crypto.randomUUID
globalThis.crypto.randomUUID = mock(() => "test-uuid-123");
globalThis.crypto.randomUUID = vi.fn(() => "test-uuid-123");
});
describe("tasks with delay <= 12 hours", () => {