Skip to content
Merged
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
32 changes: 12 additions & 20 deletions apps/crons/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ describe("index - cron job runner", () => {

it("should configure properties volume with correct chart path", async () => {
process.env.SCRIPT_NAME = "example";
const mockExampleScript = vi.fn();

vi.doMock("./example.js", () => ({
default: mockExampleScript
}));
// Import the example module and spy on its default export
const exampleModule = await import("./example.js");
vi.spyOn(exampleModule, "default");

const { main } = await import("./index.js");
await main();
Expand All @@ -54,12 +53,11 @@ describe("index - cron job runner", () => {
});

it("should execute custom script when SCRIPT_NAME is provided", async () => {
process.env.SCRIPT_NAME = "custom-job";
const mockCustomScript = vi.fn();
process.env.SCRIPT_NAME = "example";

vi.doMock("./custom-job.js", () => ({
default: mockCustomScript
}));
// Import the example module and spy on its default export
const exampleModule = await import("./example.js");
const mockCustomScript = vi.spyOn(exampleModule, "default");

const { main } = await import("./index.js");
await main();
Expand All @@ -68,26 +66,20 @@ describe("index - cron job runner", () => {
});

it("should throw error when script does not export a default function", async () => {
process.env.SCRIPT_NAME = "invalid-script";

vi.doMock("./invalid-script.js", () => ({
default: null,
somethingElse: vi.fn()
}));
process.env.SCRIPT_NAME = "non-existent-script";

const { main } = await import("./index.js");

await expect(main()).rejects.toThrow('The script "invalid-script" does not export a default function.');
await expect(main()).rejects.toThrow();
});

it("should throw error when script execution fails", async () => {
process.env.SCRIPT_NAME = "example";
const mockError = new Error("Script execution failed");
const mockFailingScript = vi.fn().mockRejectedValue(mockError);

vi.doMock("./example.js", () => ({
default: mockFailingScript
}));
// Import the example module and spy on its default export, making it fail
const exampleModule = await import("./example.js");
vi.spyOn(exampleModule, "default").mockRejectedValue(mockError);

const { main } = await import("./index.js");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ describe("monitoringMiddleware", () => {
});

it("should initialize monitoring service when enabled", () => {
vi.mocked(MonitoringService).mockImplementation(
() =>
({
trackRequest: vi.fn(),
trackException: vi.fn(),
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
}) as any
);
vi.mocked(MonitoringService).mockImplementation(function (this: any) {
return {
trackRequest: vi.fn(),
trackException: vi.fn(),
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
};
} as any);

const middleware = monitoringMiddleware(config);

Expand All @@ -79,16 +78,15 @@ describe("monitoringMiddleware", () => {
const mockTrackRequest = vi.fn();
const mockTrackException = vi.fn();

vi.mocked(MonitoringService).mockImplementation(
() =>
({
trackRequest: mockTrackRequest,
trackException: mockTrackException,
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
}) as any
);
vi.mocked(MonitoringService).mockImplementation(function (this: any) {
return {
trackRequest: mockTrackRequest,
trackException: mockTrackException,
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
};
} as any);

const middleware = monitoringMiddleware(config);
let finishCallback: () => void;
Expand Down Expand Up @@ -122,16 +120,15 @@ describe("monitoringMiddleware", () => {
it("should track exception on error event", () => {
const mockTrackException = vi.fn();

vi.mocked(MonitoringService).mockImplementation(
() =>
({
trackRequest: vi.fn(),
trackException: mockTrackException,
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
}) as any
);
vi.mocked(MonitoringService).mockImplementation(function (this: any) {
return {
trackRequest: vi.fn(),
trackException: mockTrackException,
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
};
} as any);

const middleware = monitoringMiddleware(config);
let errorCallback: (err: Error) => void;
Expand All @@ -155,16 +152,15 @@ describe("monitoringMiddleware", () => {
it("should handle request without route", () => {
const mockTrackRequest = vi.fn();

vi.mocked(MonitoringService).mockImplementation(
() =>
({
trackRequest: mockTrackRequest,
trackException: vi.fn(),
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
}) as any
);
vi.mocked(MonitoringService).mockImplementation(function (this: any) {
return {
trackRequest: mockTrackRequest,
trackException: vi.fn(),
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
};
} as any);

delete req.route;

Expand Down Expand Up @@ -197,16 +193,15 @@ describe("monitoringMiddleware", () => {
it("should track failed request", () => {
const mockTrackRequest = vi.fn();

vi.mocked(MonitoringService).mockImplementation(
() =>
({
trackRequest: mockTrackRequest,
trackException: vi.fn(),
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
}) as any
);
vi.mocked(MonitoringService).mockImplementation(function (this: any) {
return {
trackRequest: mockTrackRequest,
trackException: vi.fn(),
trackEvent: vi.fn(),
trackMetric: vi.fn(),
flush: vi.fn()
};
} as any);

res.statusCode = 500;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { readFileSync } from "node:fs";
import { DefaultAzureCredential } from "@azure/identity";
import { SecretClient } from "@azure/keyvault-secrets";
import { load as yamlLoad } from "js-yaml";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { addFromAzureVault } from "./azure-vault.js";

// Mock all external dependencies
// Create a shared mock client reference
const mockClient = {
getSecret: vi.fn()
};

// Mock all external dependencies with proper factory functions
vi.mock("@azure/identity", () => ({
DefaultAzureCredential: vi.fn()
// biome-ignore lint/complexity/useArrowFunction: Vitest 4 requires function keyword for constructors
DefaultAzureCredential: vi.fn().mockImplementation(function () {
return {};
})
}));

vi.mock("@azure/keyvault-secrets", () => ({
SecretClient: vi.fn()
// biome-ignore lint/complexity/useArrowFunction: Vitest 4 requires function keyword for constructors
SecretClient: vi.fn().mockImplementation(function () {
return mockClient;
})
}));

vi.mock("node:fs", () => ({
Expand All @@ -22,29 +29,23 @@ vi.mock("js-yaml", () => ({
load: vi.fn()
}));

const mockDefaultAzureCredential = vi.mocked(DefaultAzureCredential);
// Import mocked modules after vi.mock declarations
const { SecretClient } = await import("@azure/keyvault-secrets");
const { readFileSync } = await import("node:fs");
const { load: yamlLoad } = await import("js-yaml");

const mockSecretClient = vi.mocked(SecretClient);
const mockReadFileSync = vi.mocked(readFileSync);
const mockYamlLoad = vi.mocked(yamlLoad);

describe("addFromAzureVault", () => {
let config: Record<string, any>;
let consoleWarnSpy: any;
let mockClient: any;

beforeEach(() => {
config = { existing: "value" };
consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});

// Set up mock client
mockClient = {
getSecret: vi.fn()
};

// Configure mocks
mockDefaultAzureCredential.mockReturnValue({});
mockSecretClient.mockReturnValue(mockClient);

vi.clearAllMocks();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { expressSessionRedis } from "./redis-store.js";

vi.mock("connect-redis", () => ({
RedisStore: vi.fn().mockImplementation((options) => {
// biome-ignore lint/complexity/useArrowFunction: Vitest 4 requires function keyword for constructors
RedisStore: vi.fn().mockImplementation(function (options) {
return { redisStoreOptions: options };
})
}));

vi.mock("express-session", () => ({
default: vi.fn().mockImplementation((options) => {
// biome-ignore lint/complexity/useArrowFunction: Vitest 4 requires function keyword for constructors
default: vi.fn().mockImplementation(function (options) {
return { sessionOptions: options };
})
}));
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
"@biomejs/biome": "2.3.0",
"@types/node": "24.9.1",
"@types/supertest": "6.0.3",
"@vitest/coverage-v8": "3.2.4",
"@vitest/ui": "3.2.4",
"@vitest/coverage-v8": "4.0.3",
"@vitest/ui": "4.0.3",
"concurrently": "9.2.1",
"supertest": "7.1.4",
"tsx": "4.20.6",
"turbo": "2.5.8",
"typescript": "5.9.3",
"vitest": "3.2.4"
"vitest": "4.0.3"
},
"packageManager": "[email protected]",
"resolutions": {
Expand Down
Loading
Loading