fix: resolve CI test failures — explicit cache mock exports, hoisted service mocks, pinned Bun 1.3.6
This commit is contained in:
@@ -1,12 +1,45 @@
|
||||
import { describe, test, expect, mock, beforeEach } from "bun:test";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Top-level mocks — configured before the module is imported so the ESM
|
||||
// linker always sees the mocked version, regardless of Bun's module cache.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const postMock = mock(() => Promise.resolve({ data: { id: 9001 } }));
|
||||
const updateMock = mock(() => Promise.resolve({}));
|
||||
|
||||
mock.module("../../src/constants", () => ({
|
||||
connectWiseApi: { post: postMock },
|
||||
prisma: new Proxy(
|
||||
{},
|
||||
{
|
||||
get: () => mock(() => Promise.resolve(null)),
|
||||
},
|
||||
),
|
||||
}));
|
||||
|
||||
mock.module("../../src/modules/cw-utils/opportunities/opportunities", () => ({
|
||||
opportunityCw: { update: updateMock },
|
||||
}));
|
||||
|
||||
// Import AFTER mocks
|
||||
import {
|
||||
submitTimeEntry,
|
||||
syncOpportunityStatus,
|
||||
} from "../../src/services/cw.opportunityService";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe("cw.opportunityService", () => {
|
||||
beforeEach(() => {
|
||||
mock.restore();
|
||||
postMock.mockReset();
|
||||
postMock.mockImplementation(() =>
|
||||
Promise.resolve({ data: { id: 9001 } }),
|
||||
);
|
||||
updateMock.mockReset();
|
||||
updateMock.mockImplementation(() => Promise.resolve({}));
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
@@ -14,27 +47,6 @@ describe("cw.opportunityService", () => {
|
||||
// -------------------------------------------------------------------
|
||||
describe("submitTimeEntry()", () => {
|
||||
test("submits time entry and returns success", async () => {
|
||||
const postMock = mock(() => Promise.resolve({ data: { id: 9001 } }));
|
||||
mock.module("../../src/constants", () => ({
|
||||
connectWiseApi: { post: postMock },
|
||||
prisma: new Proxy(
|
||||
{},
|
||||
{
|
||||
get: () => mock(() => Promise.resolve(null)),
|
||||
},
|
||||
),
|
||||
}));
|
||||
mock.module(
|
||||
"../../src/modules/cw-utils/opportunities/opportunities",
|
||||
() => ({
|
||||
opportunityCw: {
|
||||
update: mock(() => Promise.resolve({})),
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
const { submitTimeEntry } =
|
||||
await import("../../src/services/cw.opportunityService");
|
||||
const result = await submitTimeEntry({
|
||||
activityId: 100,
|
||||
cwMemberId: 10,
|
||||
@@ -49,25 +61,6 @@ describe("cw.opportunityService", () => {
|
||||
});
|
||||
|
||||
test("strips milliseconds from ISO timestamps", async () => {
|
||||
const postMock = mock(() => Promise.resolve({ data: { id: 9001 } }));
|
||||
mock.module("../../src/constants", () => ({
|
||||
connectWiseApi: { post: postMock },
|
||||
prisma: new Proxy(
|
||||
{},
|
||||
{
|
||||
get: () => mock(() => Promise.resolve(null)),
|
||||
},
|
||||
),
|
||||
}));
|
||||
mock.module(
|
||||
"../../src/modules/cw-utils/opportunities/opportunities",
|
||||
() => ({
|
||||
opportunityCw: { update: mock(() => Promise.resolve({})) },
|
||||
}),
|
||||
);
|
||||
|
||||
const { submitTimeEntry } =
|
||||
await import("../../src/services/cw.opportunityService");
|
||||
await submitTimeEntry({
|
||||
activityId: 100,
|
||||
cwMemberId: 10,
|
||||
@@ -82,26 +75,10 @@ describe("cw.opportunityService", () => {
|
||||
});
|
||||
|
||||
test("returns failure on API error", async () => {
|
||||
mock.module("../../src/constants", () => ({
|
||||
connectWiseApi: {
|
||||
post: mock(() => Promise.reject(new Error("CW down"))),
|
||||
},
|
||||
prisma: new Proxy(
|
||||
{},
|
||||
{
|
||||
get: () => mock(() => Promise.resolve(null)),
|
||||
},
|
||||
),
|
||||
}));
|
||||
mock.module(
|
||||
"../../src/modules/cw-utils/opportunities/opportunities",
|
||||
() => ({
|
||||
opportunityCw: { update: mock(() => Promise.resolve({})) },
|
||||
}),
|
||||
postMock.mockImplementation(() =>
|
||||
Promise.reject(new Error("CW down")),
|
||||
);
|
||||
|
||||
const { submitTimeEntry } =
|
||||
await import("../../src/services/cw.opportunityService");
|
||||
const result = await submitTimeEntry({
|
||||
activityId: 100,
|
||||
cwMemberId: 10,
|
||||
@@ -121,25 +98,6 @@ describe("cw.opportunityService", () => {
|
||||
// -------------------------------------------------------------------
|
||||
describe("syncOpportunityStatus()", () => {
|
||||
test("syncs status to CW and returns success", async () => {
|
||||
const updateMock = mock(() => Promise.resolve({}));
|
||||
mock.module("../../src/constants", () => ({
|
||||
connectWiseApi: { post: mock(() => Promise.resolve({ data: {} })) },
|
||||
prisma: new Proxy(
|
||||
{},
|
||||
{
|
||||
get: () => mock(() => Promise.resolve(null)),
|
||||
},
|
||||
),
|
||||
}));
|
||||
mock.module(
|
||||
"../../src/modules/cw-utils/opportunities/opportunities",
|
||||
() => ({
|
||||
opportunityCw: { update: updateMock },
|
||||
}),
|
||||
);
|
||||
|
||||
const { syncOpportunityStatus } =
|
||||
await import("../../src/services/cw.opportunityService");
|
||||
const result = await syncOpportunityStatus({
|
||||
opportunityId: 1001,
|
||||
statusCwId: 24,
|
||||
@@ -150,26 +108,10 @@ describe("cw.opportunityService", () => {
|
||||
});
|
||||
|
||||
test("returns failure on API error", async () => {
|
||||
mock.module("../../src/constants", () => ({
|
||||
connectWiseApi: { post: mock(() => Promise.resolve({ data: {} })) },
|
||||
prisma: new Proxy(
|
||||
{},
|
||||
{
|
||||
get: () => mock(() => Promise.resolve(null)),
|
||||
},
|
||||
),
|
||||
}));
|
||||
mock.module(
|
||||
"../../src/modules/cw-utils/opportunities/opportunities",
|
||||
() => ({
|
||||
opportunityCw: {
|
||||
update: mock(() => Promise.reject(new Error("API fail"))),
|
||||
},
|
||||
}),
|
||||
updateMock.mockImplementation(() =>
|
||||
Promise.reject(new Error("API fail")),
|
||||
);
|
||||
|
||||
const { syncOpportunityStatus } =
|
||||
await import("../../src/services/cw.opportunityService");
|
||||
const result = await syncOpportunityStatus({
|
||||
opportunityId: 1001,
|
||||
statusCwId: 24,
|
||||
|
||||
Reference in New Issue
Block a user