Skip to content

Commit

Permalink
fix: update mock declarations and test expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarber committed Dec 20, 2024
1 parent fc59c49 commit 4230028
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
15 changes: 7 additions & 8 deletions cli/__tests__/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ jest.mock("../utils", () => ({
}));
jest.mock("../../build");

// Mock fs and path modules
jest.mock("fs");
jest.mock("path");

// Create mock functions for fs and path
const mockExistsSync = jest.fn();
const mockResolve = jest.fn();

// Mock fs and path modules
jest.mock("fs", () => ({
existsSync: mockExistsSync,
}));

jest.mock("path", () => ({
resolve: mockResolve,
}));
// Override module exports with mocks
jest.mocked(require("fs")).existsSync = mockExistsSync;

Check failure on line 30 in cli/__tests__/build.test.ts

View workflow job for this annotation

GitHub Actions / build

A `require()` style import is forbidden
jest.mocked(require("path")).resolve = mockResolve;

Check failure on line 31 in cli/__tests__/build.test.ts

View workflow job for this annotation

GitHub Actions / build

A `require()` style import is forbidden

/**
* Test suite for the build command
Expand Down
60 changes: 32 additions & 28 deletions cli/__tests__/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ jest.mock("../utils", () => ({
}));
jest.mock("../../list");

// Mock fs and path modules
jest.mock("fs");
jest.mock("path");

// Create mock functions for fs and path
const mockExistsSync = jest.fn();
const mockResolve = jest.fn();

// Mock fs and path modules
jest.mock("fs", () => ({
existsSync: mockExistsSync,
}));

jest.mock("path", () => ({
resolve: mockResolve,
}));
// Override module exports with mocks
jest.mocked(require("fs")).existsSync = mockExistsSync;

Check failure on line 30 in cli/__tests__/list.test.ts

View workflow job for this annotation

GitHub Actions / build

A `require()` style import is forbidden
jest.mocked(require("path")).resolve = mockResolve;

Check failure on line 31 in cli/__tests__/list.test.ts

View workflow job for this annotation

GitHub Actions / build

A `require()` style import is forbidden

/**
* Test suite for the list command
Expand All @@ -39,15 +38,15 @@ jest.mock("path", () => ({
describe("cmdList", () => {
// Store original console.log
const originalConsoleLog = console.log;

Check warning on line 41 in cli/__tests__/list.test.ts

View workflow job for this annotation

GitHub Actions / build

Delete `··`
beforeEach(() => {
// Reset all mocks before each test
jest.clearAllMocks();

Check warning on line 45 in cli/__tests__/list.test.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
// Setup default mock implementations
mockExistsSync.mockReturnValue(true);
mockResolve.mockImplementation((...args) => args.join("/"));

Check warning on line 49 in cli/__tests__/list.test.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
// Mock console.log
console.log = jest.fn();
});
Expand All @@ -68,10 +67,13 @@ describe("cmdList", () => {
*/
test("displays help message when --help flag is used", () => {
const mockPrintAndExit = utils.printAndExit as jest.MockedFunction<typeof utils.printAndExit>;

Check warning on line 70 in cli/__tests__/list.test.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
cmdList(["--help"]);

expect(mockPrintAndExit).toHaveBeenCalledWith(expect.stringContaining("Usage"), 0);

Check warning on line 72 in cli/__tests__/list.test.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
expect(mockPrintAndExit).toHaveBeenCalledWith(

Check warning on line 73 in cli/__tests__/list.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `⏎······expect.stringContaining("Usage"),⏎······0⏎····` with `expect.stringContaining("Usage"),·0`
expect.stringContaining("Usage"),
0
);
});

/**
Expand All @@ -81,10 +83,12 @@ describe("cmdList", () => {
test("validates out directory exists when specified", () => {
const mockPrintAndExit = utils.printAndExit as jest.MockedFunction<typeof utils.printAndExit>;
mockExistsSync.mockReturnValue(false);

cmdList(["--out-dir", "/non/existent/dir"]);

expect(mockPrintAndExit).toHaveBeenCalledWith(expect.stringContaining("/non/existent/dir"));

expect(mockPrintAndExit).toHaveBeenCalledWith(
expect.stringContaining("/non/existent/dir")
);
});

/**
Expand All @@ -94,12 +98,10 @@ describe("cmdList", () => {
test("calls list with correct parameters", () => {
const mockList = list as jest.MockedFunction<typeof list>;
const args = ["--out-dir", "/output/dir"];

cmdList(args);

expect(mockList).toHaveBeenCalledWith({
outDir: "/output/dir",
});

expect(mockList).toHaveBeenCalledWith("/output/dir");
});

/**
Expand All @@ -108,10 +110,10 @@ describe("cmdList", () => {
*/
test("calls list with undefined when no out-dir specified", () => {
const mockList = list as jest.MockedFunction<typeof list>;

cmdList([]);

expect(mockList).toHaveBeenCalledWith({});
expect(mockList).toHaveBeenCalledWith(undefined);
});

/**
Expand All @@ -120,9 +122,11 @@ describe("cmdList", () => {
*/
test("handles unknown options gracefully", () => {
const mockPrintAndExit = utils.printAndExit as jest.MockedFunction<typeof utils.printAndExit>;

cmdList(["--unknown-flag"]);

expect(mockPrintAndExit).toHaveBeenCalledWith(expect.stringContaining("--unknown-flag"));

expect(mockPrintAndExit).toHaveBeenCalledWith(
expect.stringContaining("--unknown-flag")
);
});
});

0 comments on commit 4230028

Please sign in to comment.