Skip to content

Commit 3c39501

Browse files
author
Devnil434
committed
fix test errors
1 parent 4012666 commit 3c39501

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

scripts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const tagsPath = resolve(currentDirPath, '../config', 'all-tags.json');
2323
* This asynchronous function orchestrates the creation of various content lists by processing designated directories and files.
2424
* It builds the posts list, generates the blog RSS feed, creates the case studies list, compiles the adopters list,
2525
* and combines tools data.
26-
* For finance information, it reads the finance directory, filters and sorts numeric filenames representing years, and utilizes the latest year.
26+
* For finance information, it reads the finance directory, filters and sorts numeric directory names representing years, and utilizes the latest year.
2727
* The function throws an error if no valid finance data is found.
2828
*
2929
* @throws {Error} If no numeric finance data is found in the finance directory.

tests/index.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ describe('start function', () => {
4747

4848
readdirSyncSpy.mockRestore();
4949
});
50-
});
50+
51+
// Note: Testing the direct execution branch (lines 79-81 in scripts/index.ts)
52+
// is challenging in Jest as it requires mocking module loading behavior.
53+
// This coverage gap is acceptable as the branch is straightforward.
54+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
// This test file specifically tests the direct execution path of scripts/index.ts
6+
// by manipulating the module environment to simulate direct execution.
7+
8+
import fs from 'fs';
9+
10+
// Mock all the imported functions to avoid actual file operations
11+
jest.mock('../../scripts/build-post-list');
12+
jest.mock('../../scripts/build-rss');
13+
jest.mock('../../scripts/casestudies/index');
14+
jest.mock('../../scripts/build-tools');
15+
jest.mock('../../scripts/usecases/index');
16+
jest.mock('../../scripts/finance/index');
17+
18+
describe('scripts/index.ts direct execution', () => {
19+
beforeEach(() => {
20+
jest.clearAllMocks();
21+
});
22+
23+
test('should execute start function when run directly', async () => {
24+
// Mock fs.readdirSync to return a valid year directory to avoid the error path
25+
const readdirSyncSpy = jest.spyOn(fs, 'readdirSync').mockReturnValue(['2023'] as any);
26+
const statSyncSpy = jest.spyOn(fs, 'statSync').mockReturnValue({ isDirectory: () => true } as any);
27+
28+
// We need to mock console.error to avoid polluting the test output
29+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
30+
31+
// Set up process.argv to simulate direct execution
32+
const originalArgv = process.argv;
33+
const testFilePath = require.resolve('../../scripts/index.ts');
34+
process.argv = ['', testFilePath];
35+
36+
// Dynamically import the module to trigger the direct execution
37+
// Using require to ensure the module is evaluated
38+
await import('../../scripts/index.ts');
39+
40+
// Clean up
41+
process.argv = originalArgv;
42+
consoleErrorSpy.mockRestore();
43+
readdirSyncSpy.mockRestore();
44+
statSyncSpy.mockRestore();
45+
});
46+
});

0 commit comments

Comments
 (0)