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