test: refactor mocks to use spyOn for better isolation

This commit is contained in:
syntaxbullet
2025-12-19 13:31:14 +01:00
parent 4e228bb7a3
commit 1f7679e5a1
4 changed files with 62 additions and 72 deletions

View File

@@ -1,7 +1,8 @@
import { describe, it, expect, mock, beforeEach, afterEach } from "bun:test";
import { describe, it, expect, mock, beforeEach, afterEach, spyOn } from "bun:test";
import { lootdropService } from "./lootdrop.service";
import { lootdrops } from "@/db/schema";
import { eq, and, isNull } from "drizzle-orm";
import { economyService } from "./economy.service";
// Mock dependencies BEFORE using service functionality
const mockInsert = mock();
@@ -34,16 +35,6 @@ mock.module("@/lib/DrizzleClient", () => {
};
});
// Mock EconomyService
const mockModifyUserBalance = mock();
mock.module("./economy.service", () => {
return {
economyService: {
modifyUserBalance: mockModifyUserBalance
}
};
});
// Mock Config
mock.module("@/lib/config", () => ({
config: {
@@ -63,6 +54,7 @@ mock.module("@/lib/config", () => ({
describe("lootdropService", () => {
let originalRandom: any;
let mockModifyUserBalance: any;
beforeEach(() => {
mockInsert.mockClear();
@@ -74,7 +66,6 @@ describe("lootdropService", () => {
mockWhere.mockClear();
mockSelect.mockClear();
mockFrom.mockClear();
mockModifyUserBalance.mockClear();
// Reset internal state
(lootdropService as any).channelActivity = new Map();
@@ -82,10 +73,14 @@ describe("lootdropService", () => {
// Mock Math.random
originalRandom = Math.random;
// Spy
mockModifyUserBalance = spyOn(economyService, 'modifyUserBalance').mockResolvedValue({} as any);
});
afterEach(() => {
Math.random = originalRandom;
mockModifyUserBalance.mockRestore();
});
describe("processMessage", () => {