test: quiet and speed up test runs
This commit is contained in:
24
packages/core/test/delay.test.ts
Normal file
24
packages/core/test/delay.test.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { afterEach, describe, expect, mock, test } from "bun:test";
|
||||
import { delay } from "../src/utils/delay";
|
||||
|
||||
describe("delay", () => {
|
||||
const originalNodeEnv = process.env.NODE_ENV;
|
||||
const originalSetTimeout = globalThis.setTimeout;
|
||||
|
||||
afterEach(() => {
|
||||
process.env.NODE_ENV = originalNodeEnv;
|
||||
globalThis.setTimeout = originalSetTimeout;
|
||||
});
|
||||
|
||||
test("does not schedule throttle timers during tests", async () => {
|
||||
process.env.NODE_ENV = "test";
|
||||
const setTimeoutMock = mock(() => {
|
||||
throw new Error("setTimeout should not be called during tests");
|
||||
});
|
||||
globalThis.setTimeout = setTimeoutMock as unknown as typeof setTimeout;
|
||||
|
||||
await delay(1000);
|
||||
|
||||
expect(setTimeoutMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -44,9 +44,6 @@ describe("eBay Scraper Cookie Handling", () => {
|
||||
});
|
||||
|
||||
test("should ignore request cookie overrides and rely on EBAY_COOKIE", async () => {
|
||||
const warnMock = mock(() => {});
|
||||
console.warn = warnMock;
|
||||
|
||||
await fetchEbayItems("laptop", 1000);
|
||||
|
||||
expect(global.fetch).toHaveBeenCalledTimes(1);
|
||||
@@ -61,9 +58,6 @@ describe("eBay Scraper Cookie Handling", () => {
|
||||
const headers = (init as RequestInit).headers as Record<string, string>;
|
||||
|
||||
expect(headers.Cookie).toBeUndefined();
|
||||
expect(warnMock).toHaveBeenCalledWith(
|
||||
"No valid eBay cookies found in EBAY_COOKIE. eBay may block requests without a raw Cookie header string.",
|
||||
);
|
||||
});
|
||||
|
||||
test("keeps relative item links on the ebay.ca host", async () => {
|
||||
|
||||
@@ -177,10 +177,6 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
|
||||
});
|
||||
|
||||
test("should handle authentication errors", async () => {
|
||||
const originalWarn = console.warn;
|
||||
const warnMock = mock(() => {});
|
||||
console.warn = warnMock;
|
||||
|
||||
global.fetch = mock(() =>
|
||||
Promise.resolve({
|
||||
ok: false,
|
||||
@@ -192,16 +188,9 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
|
||||
}),
|
||||
) as unknown as typeof fetch;
|
||||
|
||||
try {
|
||||
const result = await fetchFacebookItem("123");
|
||||
expect(result).toBeNull();
|
||||
expect(global.fetch).toHaveBeenCalledTimes(1);
|
||||
expect(warnMock).toHaveBeenCalledWith(
|
||||
"Authentication error: Invalid or expired cookies. Update FACEBOOK_COOKIE with a fresh raw Cookie header string.",
|
||||
);
|
||||
} finally {
|
||||
console.warn = originalWarn;
|
||||
}
|
||||
const result = await fetchFacebookItem("123");
|
||||
expect(result).toBeNull();
|
||||
expect(global.fetch).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("should handle item not found", async () => {
|
||||
@@ -1708,10 +1697,6 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
|
||||
});
|
||||
|
||||
test("should handle malformed ads gracefully", () => {
|
||||
const originalWarn = console.warn;
|
||||
const warnMock = mock(() => {});
|
||||
console.warn = warnMock;
|
||||
|
||||
const ads = [
|
||||
{
|
||||
node: {
|
||||
@@ -1739,9 +1724,6 @@ describe("Facebook Marketplace Scraper Core Tests", () => {
|
||||
);
|
||||
expect(results).toHaveLength(1);
|
||||
expect(results[0]?.title).toBe("Valid Ad");
|
||||
expect(warnMock).toHaveBeenCalledTimes(1);
|
||||
|
||||
console.warn = originalWarn;
|
||||
});
|
||||
|
||||
test("parses formatted fallback prices with multiple commas", () => {
|
||||
|
||||
41
packages/core/test/http.test.ts
Normal file
41
packages/core/test/http.test.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { afterEach, describe, expect, mock, test } from "bun:test";
|
||||
import { fetchHtml } from "../src/utils/http";
|
||||
|
||||
describe("fetchHtml", () => {
|
||||
const originalFetch = global.fetch;
|
||||
const originalNodeEnv = process.env.NODE_ENV;
|
||||
const originalSetTimeout = globalThis.setTimeout;
|
||||
const originalClearTimeout = globalThis.clearTimeout;
|
||||
|
||||
afterEach(() => {
|
||||
global.fetch = originalFetch;
|
||||
process.env.NODE_ENV = originalNodeEnv;
|
||||
globalThis.setTimeout = originalSetTimeout;
|
||||
globalThis.clearTimeout = originalClearTimeout;
|
||||
});
|
||||
|
||||
test("does not schedule throttle timers during tests", async () => {
|
||||
process.env.NODE_ENV = "test";
|
||||
const scheduledDelays: number[] = [];
|
||||
|
||||
global.fetch = mock(() =>
|
||||
Promise.resolve({
|
||||
ok: true,
|
||||
headers: { get: () => null },
|
||||
text: () => Promise.resolve("<html></html>"),
|
||||
}),
|
||||
) as unknown as typeof fetch;
|
||||
globalThis.setTimeout = mock((handler: TimerHandler, timeout?: number) => {
|
||||
scheduledDelays.push(Number(timeout));
|
||||
if (timeout !== 30_000 && typeof handler === "function") {
|
||||
handler();
|
||||
}
|
||||
return 0 as unknown as ReturnType<typeof setTimeout>;
|
||||
}) as unknown as typeof setTimeout;
|
||||
globalThis.clearTimeout = mock(() => {}) as unknown as typeof clearTimeout;
|
||||
|
||||
await fetchHtml("https://example.com", 1000, { timeoutMs: 30_000 });
|
||||
|
||||
expect(scheduledDelays).not.toContain(1000);
|
||||
});
|
||||
});
|
||||
29
packages/core/test/logger.test.ts
Normal file
29
packages/core/test/logger.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { afterEach, describe, expect, mock, test } from "bun:test";
|
||||
|
||||
describe("logger", () => {
|
||||
const originalNodeEnv = process.env.NODE_ENV;
|
||||
const originalConsoleLog = console.log;
|
||||
const originalConsoleWarn = console.warn;
|
||||
|
||||
afterEach(() => {
|
||||
process.env.NODE_ENV = originalNodeEnv;
|
||||
console.log = originalConsoleLog;
|
||||
console.warn = originalConsoleWarn;
|
||||
});
|
||||
|
||||
test("suppresses log and warn output during tests", async () => {
|
||||
process.env.NODE_ENV = "test";
|
||||
const logMock = mock(() => {});
|
||||
const warnMock = mock(() => {});
|
||||
console.log = logMock;
|
||||
console.warn = warnMock;
|
||||
|
||||
const { logger } = await import("../src/utils/logger");
|
||||
|
||||
logger.log("hidden log");
|
||||
logger.warn("hidden warn");
|
||||
|
||||
expect(logMock).not.toHaveBeenCalled();
|
||||
expect(warnMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user