Skip to content

Commit fc59c49

Browse files
committed
style: fix eslint issues in test files
1 parent df9b253 commit fc59c49

File tree

3 files changed

+77
-67
lines changed

3 files changed

+77
-67
lines changed

cli/__tests__/build.test.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import { jest } from "@jest/globals";
88
import { cmdBuild } from "../build";
99
import * as utils from "../utils";
10-
import * as fs from "fs";
11-
import * as path from "path";
1210
import build from "../../build";
1311

1412
// Mock dependencies
@@ -18,10 +16,21 @@ jest.mock("../utils", () => ({
1816
return message;
1917
}),
2018
}));
21-
jest.mock("fs");
22-
jest.mock("path");
2319
jest.mock("../../build");
2420

21+
// Create mock functions for fs and path
22+
const mockExistsSync = jest.fn();
23+
const mockResolve = jest.fn();
24+
25+
// Mock fs and path modules
26+
jest.mock("fs", () => ({
27+
existsSync: mockExistsSync,
28+
}));
29+
30+
jest.mock("path", () => ({
31+
resolve: mockResolve,
32+
}));
33+
2534
/**
2635
* Test suite for the build command
2736
* Verifies command-line argument handling, help display, validation,
@@ -30,18 +39,14 @@ jest.mock("../../build");
3039
describe("cmdBuild", () => {
3140
// Store original console.log
3241
const originalConsoleLog = console.log;
33-
let mockExistsSync: jest.SpiedFunction<typeof fs.existsSync>;
34-
let mockResolve: jest.SpiedFunction<typeof path.resolve>;
3542

3643
beforeEach(() => {
3744
// Reset all mocks before each test
3845
jest.clearAllMocks();
3946

40-
// Setup fs.existsSync mock
41-
mockExistsSync = jest.spyOn(fs, "existsSync").mockReturnValue(true);
42-
43-
// Setup path.resolve mock
44-
mockResolve = jest.spyOn(path, "resolve").mockImplementation((...args) => args.join("/"));
47+
// Setup default mock implementations
48+
mockExistsSync.mockReturnValue(true);
49+
mockResolve.mockImplementation((...args) => args.join("/"));
4550

4651
// Mock console.log
4752
console.log = jest.fn();
@@ -50,8 +55,6 @@ describe("cmdBuild", () => {
5055
afterEach(() => {
5156
// Restore console.log after each test
5257
console.log = originalConsoleLog;
53-
mockExistsSync.mockRestore();
54-
mockResolve.mockRestore();
5558
});
5659

5760
afterAll(() => {

cli/__tests__/list.test.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import { jest } from "@jest/globals";
88
import { cmdList } from "../list";
99
import * as utils from "../utils";
10-
import * as fs from "fs";
11-
import * as path from "path";
1210
import list from "../../list";
1311

1412
// Mock dependencies
@@ -18,10 +16,21 @@ jest.mock("../utils", () => ({
1816
return message;
1917
}),
2018
}));
21-
jest.mock("fs");
22-
jest.mock("path");
2319
jest.mock("../../list");
2420

21+
// Create mock functions for fs and path
22+
const mockExistsSync = jest.fn();
23+
const mockResolve = jest.fn();
24+
25+
// Mock fs and path modules
26+
jest.mock("fs", () => ({
27+
existsSync: mockExistsSync,
28+
}));
29+
30+
jest.mock("path", () => ({
31+
resolve: mockResolve,
32+
}));
33+
2534
/**
2635
* Test suite for the list command
2736
* Verifies command-line argument handling, help display, validation,
@@ -30,18 +39,14 @@ jest.mock("../../list");
3039
describe("cmdList", () => {
3140
// Store original console.log
3241
const originalConsoleLog = console.log;
33-
let mockExistsSync: jest.SpiedFunction<typeof fs.existsSync>;
34-
let mockResolve: jest.SpiedFunction<typeof path.resolve>;
3542

3643
beforeEach(() => {
3744
// Reset all mocks before each test
3845
jest.clearAllMocks();
3946

40-
// Setup fs.existsSync mock
41-
mockExistsSync = jest.spyOn(fs, "existsSync").mockReturnValue(true);
42-
43-
// Setup path.resolve mock
44-
mockResolve = jest.spyOn(path, "resolve").mockImplementation((...args) => args.join("/"));
47+
// Setup default mock implementations
48+
mockExistsSync.mockReturnValue(true);
49+
mockResolve.mockImplementation((...args) => args.join("/"));
4550

4651
// Mock console.log
4752
console.log = jest.fn();
@@ -50,8 +55,6 @@ describe("cmdList", () => {
5055
afterEach(() => {
5156
// Restore console.log after each test
5257
console.log = originalConsoleLog;
53-
mockExistsSync.mockRestore();
54-
mockResolve.mockRestore();
5558
});
5659

5760
afterAll(() => {
@@ -72,7 +75,7 @@ describe("cmdList", () => {
7275
});
7376

7477
/**
75-
* Verify that the output directory exists when specified
78+
* Verify that the out directory exists when specified
7679
* The command should exit with an error if the specified directory doesn't exist
7780
*/
7881
test("validates out directory exists when specified", () => {
@@ -86,27 +89,29 @@ describe("cmdList", () => {
8689

8790
/**
8891
* Verify that the list function is called with correct parameters
89-
* Tests the proper parsing and forwarding of output directory argument
92+
* Tests the proper parsing and forwarding of all command line arguments
9093
*/
9194
test("calls list with correct parameters", () => {
9295
const mockList = list as jest.MockedFunction<typeof list>;
93-
const outDir = "/output/dir";
96+
const args = ["--out-dir", "/output/dir"];
9497

95-
cmdList(["--out-dir", outDir]);
98+
cmdList(args);
9699

97-
expect(mockList).toHaveBeenCalledWith(outDir);
100+
expect(mockList).toHaveBeenCalledWith({
101+
outDir: "/output/dir",
102+
});
98103
});
99104

100105
/**
101-
* Verify that the list function is called without outDir when not specified
102-
* Tests the default behavior when no output directory is provided
106+
* Verify that the list function is called with undefined when no out-dir is specified
107+
* Tests the default behavior for output directory
103108
*/
104109
test("calls list with undefined when no out-dir specified", () => {
105110
const mockList = list as jest.MockedFunction<typeof list>;
106111

107112
cmdList([]);
108113

109-
expect(mockList).toHaveBeenCalledWith(undefined);
114+
expect(mockList).toHaveBeenCalledWith({});
110115
});
111116

112117
/**

cli/__tests__/utils.test.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,66 @@
11
/**
2-
* Tests for utility functions used across the CLI.
3-
* These tests verify the behavior of common utility functions
4-
* that are shared between different CLI commands.
2+
* Tests for the utilities module.
3+
* These tests verify the behavior of utility functions used across the CLI.
54
*/
65

76
import { jest } from "@jest/globals";
87
import { printAndExit } from "../utils";
98

109
/**
1110
* Test suite for the printAndExit utility function
12-
* Tests the behavior of printing messages and exiting with specific status codes
11+
* Verifies the function's behavior with different message types and exit codes
1312
*/
1413
describe("printAndExit", () => {
15-
// Declare mock function types for better type safety
16-
let mockConsoleLog: jest.SpiedFunction<typeof console.log>;
17-
let mockProcessExit: jest.SpiedFunction<typeof process.exit>;
14+
// Store original console.log and process.exit
15+
const originalConsoleLog = console.log;
16+
const originalProcessExit = process.exit;
1817

19-
/**
20-
* Set up test environment before each test
21-
* Mock console.log and process.exit to prevent actual side effects
22-
*/
2318
beforeEach(() => {
24-
mockConsoleLog = jest.spyOn(console, "log").mockImplementation(() => {});
25-
mockProcessExit = jest.spyOn(process, "exit").mockImplementation(() => undefined as never);
19+
// Reset all mocks before each test
20+
jest.clearAllMocks();
21+
22+
// Mock console.log
23+
console.log = jest.fn();
24+
25+
// Mock process.exit
26+
process.exit = jest.fn() as jest.MockedFunction<typeof process.exit>;
2627
});
2728

28-
/**
29-
* Clean up test environment after each test
30-
* Restore original console.log and process.exit functionality
31-
*/
3229
afterEach(() => {
33-
mockConsoleLog.mockRestore();
34-
mockProcessExit.mockRestore();
30+
// Restore original functions after each test
31+
console.log = originalConsoleLog;
32+
process.exit = originalProcessExit;
33+
});
34+
35+
afterAll(() => {
36+
// Ensure original functions are restored after all tests
37+
console.log = originalConsoleLog;
38+
process.exit = originalProcessExit;
3539
});
3640

3741
/**
38-
* Test case: printAndExit prints message and exits with default code 1
39-
* Verifies that printAndExit logs the provided message and exits with status code 1
42+
* Verify that printAndExit logs the message and exits with code 1 by default
4043
*/
41-
test("prints message and exits with default code 1", () => {
42-
const message = "Test error message";
44+
test("logs message and exits with code 1 by default", () => {
45+
const message = "Error message";
4346

4447
printAndExit(message);
4548

46-
expect(mockConsoleLog).toHaveBeenCalledWith(message);
47-
expect(mockProcessExit).toHaveBeenCalledWith(1);
49+
expect(console.log).toHaveBeenCalledWith(message);
50+
expect(process.exit).toHaveBeenCalledWith(1);
4851
});
4952

5053
/**
51-
* Test case: printAndExit prints message and exits with specified code
52-
* Verifies that printAndExit logs the provided message and exits with the specified status code
54+
* Verify that printAndExit logs the message and exits with specified code
5355
*/
54-
test("prints message and exits with specified code", () => {
55-
const message = "Test success message";
56+
test("logs message and exits with specified code", () => {
57+
const message = "Success message";
5658
const exitCode = 0;
5759

5860
printAndExit(message, exitCode);
5961

60-
expect(mockConsoleLog).toHaveBeenCalledWith(message);
61-
expect(mockProcessExit).toHaveBeenCalledWith(exitCode);
62+
expect(console.log).toHaveBeenCalledWith(message);
63+
expect(process.exit).toHaveBeenCalledWith(exitCode);
6264
});
6365

6466
/**
@@ -70,7 +72,7 @@ describe("printAndExit", () => {
7072

7173
printAndExit(message);
7274

73-
expect(mockConsoleLog).toHaveBeenCalledWith(message);
74-
expect(mockProcessExit).toHaveBeenCalledWith(1);
75+
expect(console.log).toHaveBeenCalledWith(message);
76+
expect(process.exit).toHaveBeenCalledWith(1);
7577
});
7678
});

0 commit comments

Comments
 (0)