@@ -9,6 +9,15 @@ const mockParseIndex = mock.fn(async index => [index]);
99const mockImportFromURL = mock . fn ( async ( ) => ( { } ) ) ;
1010const mockExistsSync = mock . fn ( ( ) => false ) ;
1111
12+ const CONFIG_FILE_NAMES = [
13+ 'doc-kit.config.js' ,
14+ 'doc-kit.config.cjs' ,
15+ 'doc-kit.config.mjs' ,
16+ 'doc-kit.config.ts' ,
17+ 'doc-kit.config.cts' ,
18+ 'doc-kit.config.mts' ,
19+ ] ;
20+
1221const createMockConfig = ( overrides = { } ) => ( {
1322 global : { } ,
1423 ...overrides ,
@@ -55,6 +64,8 @@ const resetAllMocks = () => {
5564 mockImportFromURL ,
5665 mockExistsSync ,
5766 ] . forEach ( m => m . mock . resetCalls ( ) ) ;
67+ mockImportFromURL . mock . mockImplementation ( async ( ) => ( { } ) ) ;
68+ mockExistsSync . mock . mockImplementation ( ( ) => false ) ;
5869} ;
5970
6071// Helper to count specific function calls
@@ -158,26 +169,50 @@ describe('config.mjs', () => {
158169 } ) ;
159170
160171 describe ( 'createRunConfiguration' , ( ) => {
161- it ( 'should auto-detect a config file in the current directory' , async ( ) => {
162- const defaultConfigFile = join ( process . cwd ( ) , 'doc-kit.config.mjs' ) ;
163- const mockConfig = createMockConfig ( {
164- global : { input : 'auto-detected-src/' } ,
165- } ) ;
166- mockExistsSync . mock . mockImplementationOnce ( ( ) => true ) ;
167- mockImportFromURL . mock . mockImplementationOnce ( async ( ) => mockConfig ) ;
172+ for ( const [ index , fileName ] of CONFIG_FILE_NAMES . entries ( ) ) {
173+ it ( `should auto-detect ${ fileName } in the current directory` , async ( ) => {
174+ const detectedConfigFile = join ( process . cwd ( ) , fileName ) ;
175+ const mockConfig = createMockConfig ( {
176+ global : { input : 'auto-detected-src/' } ,
177+ } ) ;
178+ mockExistsSync . mock . mockImplementation (
179+ filePath => filePath === detectedConfigFile
180+ ) ;
181+ mockImportFromURL . mock . mockImplementationOnce ( async ( ) => mockConfig ) ;
168182
169- const config = await createRunConfiguration ( { } ) ;
183+ const config = await createRunConfiguration ( { } ) ;
170184
171- assert . strictEqual ( config . global . input , 'auto-detected-src/' ) ;
172- assert . strictEqual ( mockExistsSync . mock . calls . length , 1 ) ;
173- assert . strictEqual (
174- mockExistsSync . mock . calls [ 0 ] . arguments [ 0 ] ,
175- defaultConfigFile
185+ assert . strictEqual ( config . global . input , 'auto-detected-src/' ) ;
186+ assert . deepStrictEqual (
187+ mockExistsSync . mock . calls . map ( call => call . arguments [ 0 ] ) ,
188+ CONFIG_FILE_NAMES . slice ( 0 , index + 1 ) . map ( candidate =>
189+ join ( process . cwd ( ) , candidate )
190+ )
191+ ) ;
192+ assert . strictEqual ( mockImportFromURL . mock . calls . length , 1 ) ;
193+ assert . strictEqual (
194+ mockImportFromURL . mock . calls [ 0 ] . arguments [ 0 ] ,
195+ detectedConfigFile
196+ ) ;
197+ } ) ;
198+ }
199+
200+ it ( 'should use the first matching config file by priority' , async ( ) => {
201+ const existingConfigFiles = new Set ( [
202+ join ( process . cwd ( ) , 'doc-kit.config.cjs' ) ,
203+ join ( process . cwd ( ) , 'doc-kit.config.mjs' ) ,
204+ join ( process . cwd ( ) , 'doc-kit.config.cts' ) ,
205+ ] ) ;
206+ mockExistsSync . mock . mockImplementation ( filePath =>
207+ existingConfigFiles . has ( filePath )
176208 ) ;
209+
210+ await createRunConfiguration ( { } ) ;
211+
177212 assert . strictEqual ( mockImportFromURL . mock . calls . length , 1 ) ;
178213 assert . strictEqual (
179214 mockImportFromURL . mock . calls [ 0 ] . arguments [ 0 ] ,
180- defaultConfigFile
215+ join ( process . cwd ( ) , 'doc-kit.config.cjs' )
181216 ) ;
182217 } ) ;
183218
@@ -262,10 +297,9 @@ describe('config.mjs', () => {
262297
263298 assert . ok ( config ) ;
264299 assert . strictEqual ( config . threads , 4 ) ;
265- assert . strictEqual ( mockExistsSync . mock . calls . length , 1 ) ;
266- assert . strictEqual (
267- mockExistsSync . mock . calls [ 0 ] . arguments [ 0 ] ,
268- join ( process . cwd ( ) , 'doc-kit.config.mjs' )
300+ assert . deepStrictEqual (
301+ mockExistsSync . mock . calls . map ( call => call . arguments [ 0 ] ) ,
302+ CONFIG_FILE_NAMES . map ( fileName => join ( process . cwd ( ) , fileName ) )
269303 ) ;
270304 assert . strictEqual ( mockImportFromURL . mock . calls . length , 0 ) ;
271305 } ) ;
0 commit comments