Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions backend/__tests__/__integration__/setup-integration-tests.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import { afterAll, beforeAll, afterEach, vi } from "vitest";
import { Collection, Db, MongoClient, WithId } from "mongodb";
import { setupCommonMocks } from "../setup-common-mocks";
import { getConnection } from "../../src/init/redis";

process.env["MODE"] = "dev";

let db: Db | undefined;
let client: MongoClient | undefined;

vi.mock("../../src/init/db", () => ({
__esModule: true,
getDb: (): Db => db as Db,
collection: <T>(name: string): Collection<WithId<T>> =>
(db as Db).collection<WithId<T>>(name),
close: () => {
//
},
}));

beforeAll(async () => {
client = new MongoClient(process.env["TEST_DB_URL"] as string);
await client.connect();
db = client.db();

vi.mock("../../src/init/db", () => ({
__esModule: true,
getDb: (): Db => db as Db,
collection: <T>(name: string): Collection<WithId<T>> =>
(db as Db).collection<WithId<T>>(name),
close: () => {
//
},
}));

setupCommonMocks();

//we compare the time in mongodb to calculate premium status, so we have to use real time here
vi.useRealTimers();
});
Expand Down
74 changes: 36 additions & 38 deletions backend/__tests__/setup-common-mocks.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
import { vi } from "vitest";

export function setupCommonMocks(): void {
vi.mock("../src/utils/logger", () => ({
__esModule: true,
default: {
error: console.error,
warning: console.warn,
info: console.info,
success: console.info,
logToDb: console.info,
},
}));

vi.mock("swagger-stats", () => ({
getMiddleware:
() =>
(_: unknown, __: unknown, next: () => unknown): void => {
next();
},
}));
vi.mock("../src/utils/logger", () => ({
__esModule: true,
default: {
error: console.error,
warning: console.warn,
info: console.info,
success: console.info,
logToDb: console.info,
},
}));

// TODO: better approach for this when needed
// https://firebase.google.com/docs/rules/unit-tests#run_local_unit_tests_with_the_version_9_javascript_sdk
vi.mock("firebase-admin", () => ({
__esModule: true,
default: {
auth: (): unknown => ({
verifyIdToken: (
_token: string,
_checkRevoked: boolean,
): unknown /* Promise<DecodedIdToken> */ =>
Promise.resolve({
aud: "mockFirebaseProjectId",
auth_time: 123,
exp: 1000,
uid: "mockUid",
}),
}),
vi.mock("swagger-stats", () => ({
getMiddleware:
() =>
(_: unknown, __: unknown, next: () => unknown): void => {
next();
},
}));
}
}));

// TODO: better approach for this when needed
// https://firebase.google.com/docs/rules/unit-tests#run_local_unit_tests_with_the_version_9_javascript_sdk
vi.mock("firebase-admin", () => ({
__esModule: true,
default: {
auth: (): unknown => ({
verifyIdToken: (
_token: string,
_checkRevoked: boolean,
): unknown /* Promise<DecodedIdToken> */ =>
Promise.resolve({
aud: "mockFirebaseProjectId",
auth_time: 123,
exp: 1000,
uid: "mockUid",
}),
}),
},
}));
40 changes: 19 additions & 21 deletions backend/__tests__/setup-tests.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import { afterAll, beforeAll, afterEach, vi } from "vitest";
import { BASE_CONFIGURATION } from "../src/constants/base-configuration";
import { setupCommonMocks } from "./setup-common-mocks";
import { __testing } from "../src/init/configuration";

process.env["MODE"] = "dev";
process.env.TZ = "UTC";
beforeAll(async () => {
//don't add any configuration here, add to global-setup.ts instead.

vi.mock("../src/init/configuration", async (importOriginal) => {
const orig = (await importOriginal()) as any;
vi.mock("../src/init/configuration", async (importOriginal) => {
const orig = (await importOriginal()) as any;

return {
__testing: orig.__testing,
getLiveConfiguration: () => BASE_CONFIGURATION,
getCachedConfiguration: () => BASE_CONFIGURATION,
patchConfiguration: vi.fn(),
};
});
return {
__testing: orig.__testing,
getLiveConfiguration: () => BASE_CONFIGURATION,
getCachedConfiguration: () => BASE_CONFIGURATION,
patchConfiguration: vi.fn(),
};
});

vi.mock("../src/init/db", () => ({
__esModule: true,
getDb: () => undefined,
collection: () => undefined,
close: () => {
//
},
}));
vi.mock("../src/init/db", () => ({
__esModule: true,
getDb: () => undefined,
collection: () => undefined,
close: () => {
//
},
}));

setupCommonMocks();
beforeAll(async () => {
//don't add any configuration here, add to global-setup.ts instead.
});

afterEach(async () => {
Expand Down
Loading