@@ -9,6 +9,7 @@ import { GitWorkflow } from '../lib/gitWorkflow'
99import { resolveGitRepo } from '../lib/resolveGitRepo'
1010import { runAll } from '../lib/runAll'
1111import { GitError } from '../lib/symbols'
12+ import * as getConfigGroupsNS from '../lib/getConfigGroups'
1213
1314jest . mock ( '../lib/file' )
1415jest . mock ( '../lib/getStagedFiles' )
@@ -26,6 +27,8 @@ jest.mock('../lib/resolveConfig', () => ({
2627 } ,
2728} ) )
2829
30+ const getConfigGroups = jest . spyOn ( getConfigGroupsNS , 'getConfigGroups' )
31+
2932getStagedFiles . mockImplementation ( async ( ) => [ ] )
3033
3134resolveGitRepo . mockImplementation ( async ( ) => {
@@ -293,4 +296,76 @@ describe('runAll', () => {
293296 expect ( mockConstructor ) . toHaveBeenCalledTimes ( 1 )
294297 expect ( expected ) . toEqual ( [ [ normalize ( path . join ( cwd , 'test/foo.js' ) ) ] ] )
295298 } )
299+
300+ it ( 'should resolve matched files to config locations with multiple configs' , async ( ) => {
301+ getStagedFiles . mockImplementationOnce ( async ( ) => [ 'foo.js' , 'test/foo.js' ] )
302+
303+ const mockTask = jest . fn ( ( ) => [ 'echo "sample"' ] )
304+
305+ getConfigGroups . mockResolvedValueOnce ( {
306+ '.lintstagedrc.json' : {
307+ config : { '*.js' : mockTask } ,
308+ files : [ 'foo.js' ] ,
309+ } ,
310+ 'test/.lintstagedrc.json' : {
311+ config : { '*.js' : mockTask } ,
312+ files : [ 'test/foo.js' ] ,
313+ } ,
314+ } )
315+
316+ // We are only interested in the `matchedFileChunks` generation
317+ let expected
318+ const mockConstructor = jest . fn ( ( { matchedFileChunks } ) => ( expected = matchedFileChunks ) )
319+ GitWorkflow . mockImplementationOnce ( mockConstructor )
320+
321+ try {
322+ await runAll ( {
323+ stash : false ,
324+ relative : true ,
325+ } )
326+ } catch { } // eslint-disable-line no-empty
327+
328+ // task received relative `foo.js` from both directories
329+ expect ( mockTask ) . toHaveBeenCalledTimes ( 2 )
330+ expect ( mockTask ) . toHaveBeenNthCalledWith ( 1 , [ 'foo.js' ] )
331+ expect ( mockTask ) . toHaveBeenNthCalledWith ( 2 , [ 'foo.js' ] )
332+ // GitWorkflow received absolute paths `foo.js` and `test/foo.js`
333+ expect ( mockConstructor ) . toHaveBeenCalledTimes ( 1 )
334+ expect ( expected ) . toEqual ( [
335+ [
336+ normalize ( path . join ( process . cwd ( ) , 'foo.js' ) ) ,
337+ normalize ( path . join ( process . cwd ( ) , 'test/foo.js' ) ) ,
338+ ] ,
339+ ] )
340+ } )
341+
342+ it ( 'should resolve matched files to explicit cwd with multiple configs' , async ( ) => {
343+ getStagedFiles . mockImplementationOnce ( async ( ) => [ 'foo.js' , 'test/foo.js' ] )
344+
345+ const mockTask = jest . fn ( ( ) => [ 'echo "sample"' ] )
346+
347+ getConfigGroups . mockResolvedValueOnce ( {
348+ '.lintstagedrc.json' : {
349+ config : { '*.js' : mockTask } ,
350+ files : [ 'foo.js' ] ,
351+ } ,
352+ 'test/.lintstagedrc.json' : {
353+ config : { '*.js' : mockTask } ,
354+ files : [ 'test/foo.js' ] ,
355+ } ,
356+ } )
357+
358+ try {
359+ await runAll ( {
360+ cwd : '.' ,
361+ stash : false ,
362+ relative : true ,
363+ } )
364+ } catch { } // eslint-disable-line no-empty
365+
366+ expect ( mockTask ) . toHaveBeenCalledTimes ( 2 )
367+ expect ( mockTask ) . toHaveBeenNthCalledWith ( 1 , [ 'foo.js' ] )
368+ // This is now relative to "." instead of "test/"
369+ expect ( mockTask ) . toHaveBeenNthCalledWith ( 2 , [ 'test/foo.js' ] )
370+ } )
296371} )
0 commit comments