Skip to content

Commit

Permalink
fix: use jest.spyOn for module mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarber committed Dec 20, 2024
1 parent f381efe commit 9cf0470
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
17 changes: 13 additions & 4 deletions cli/__tests__/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,28 @@ jest.mock('../../build');
describe('cmdBuild', () => {
// Store original console.log
const originalConsoleLog = console.log;
let mockExistsSync: jest.SpiedFunction<typeof fs.existsSync>;
let mockResolve: jest.SpiedFunction<typeof path.resolve>;

beforeEach(() => {
// Reset all mocks before each test
jest.clearAllMocks();
(fs.existsSync as jest.Mock).mockReturnValue(true);
(path.resolve as jest.Mock).mockImplementation((...args) => args.join('/'));

// Setup fs.existsSync mock
mockExistsSync = jest.spyOn(fs, 'existsSync').mockReturnValue(true);

// Setup path.resolve mock
mockResolve = jest.spyOn(path, 'resolve').mockImplementation((...args) => args.join('/'));

// Mock console.log
console.log = jest.fn();
});

afterEach(() => {
// Restore console.log after each test
console.log = originalConsoleLog;
mockExistsSync.mockRestore();
mockResolve.mockRestore();
});

afterAll(() => {
Expand Down Expand Up @@ -100,7 +109,7 @@ describe('cmdBuild', () => {
*/
test('validates project directory exists', () => {
const mockPrintAndExit = utils.printAndExit as jest.MockedFunction<typeof utils.printAndExit>;
(fs.existsSync as jest.Mock).mockReturnValue(false);
mockExistsSync.mockReturnValue(false);

cmdBuild(['--env', 'prod', '/non/existent/dir']);

Expand Down Expand Up @@ -198,7 +207,7 @@ describe('cmdBuild', () => {
* Any unexpected errors should be thrown rather than handled silently
*/
test('throws error for non-ARG_UNKNOWN_OPTION errors', () => {
(fs.existsSync as jest.Mock).mockImplementation(() => {
mockExistsSync.mockImplementation(() => {
throw new Error('Some other error');
});

Expand Down
15 changes: 12 additions & 3 deletions cli/__tests__/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,28 @@ jest.mock('../../list');
describe('cmdList', () => {
// Store original console.log
const originalConsoleLog = console.log;
let mockExistsSync: jest.SpiedFunction<typeof fs.existsSync>;
let mockResolve: jest.SpiedFunction<typeof path.resolve>;

beforeEach(() => {
// Reset all mocks before each test
jest.clearAllMocks();
(fs.existsSync as jest.Mock).mockReturnValue(true);
(path.resolve as jest.Mock).mockImplementation((...args) => args.join('/'));

// Setup fs.existsSync mock
mockExistsSync = jest.spyOn(fs, 'existsSync').mockReturnValue(true);

// Setup path.resolve mock
mockResolve = jest.spyOn(path, 'resolve').mockImplementation((...args) => args.join('/'));

// Mock console.log
console.log = jest.fn();
});

afterEach(() => {
// Restore console.log after each test
console.log = originalConsoleLog;
mockExistsSync.mockRestore();
mockResolve.mockRestore();
});

afterAll(() => {
Expand Down Expand Up @@ -71,7 +80,7 @@ describe('cmdList', () => {
*/
test('validates out directory exists when specified', () => {
const mockPrintAndExit = utils.printAndExit as jest.MockedFunction<typeof utils.printAndExit>;
(fs.existsSync as jest.Mock).mockReturnValue(false);
mockExistsSync.mockReturnValue(false);

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

Expand Down

0 comments on commit 9cf0470

Please sign in to comment.