/
getConfigGroups.spec.js
75 lines (61 loc) · 2.29 KB
/
getConfigGroups.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import makeConsoleMock from 'consolemock'
import { getConfigGroups } from '../lib/getConfigGroups'
import { loadConfig } from '../lib/loadConfig'
jest.mock('../lib/loadConfig', () => ({
// config not found
loadConfig: jest.fn(async () => ({})),
}))
const globalConsoleTemp = console
const config = {
'*.js': 'my-task',
}
describe('getConfigGroups', () => {
beforeEach(() => {
console = makeConsoleMock()
})
afterEach(() => {
console.printHistory()
console = globalConsoleTemp
})
it('should throw when config path not found', async () => {
await expect(getConfigGroups({ configPath: '/' })).rejects.toThrowErrorMatchingInlineSnapshot(
`"Configuration could not be found"`
)
})
it('should throw when config not found', async () => {
await expect(
getConfigGroups({ files: ['/foo.js'] })
).rejects.toThrowErrorMatchingInlineSnapshot(`"Configuration could not be found"`)
})
it('should find config files for all staged files', async () => {
// Base cwd
loadConfig.mockResolvedValueOnce({ config, filepath: '/.lintstagedrc.json' })
// '/foo.js' and '/bar.js'
loadConfig.mockResolvedValueOnce({ config, filepath: '/.lintstagedrc.json' })
// '/deeper/foo.js'
loadConfig.mockResolvedValueOnce({ config, filepath: '/deeper/.lintstagedrc.json' })
// '/even/deeper/foo.js'
loadConfig.mockResolvedValueOnce({ config, filepath: '/deeper/.lintstagedrc.json' })
const configGroups = await getConfigGroups({
files: ['/foo.js', '/bar.js', '/deeper/foo.js', '/even/deeper/foo.js'],
})
expect(configGroups).toEqual({
'/.lintstagedrc.json': { config, files: ['/foo.js', '/bar.js'] },
'/deeper/.lintstagedrc.json': { config, files: ['/deeper/foo.js', '/even/deeper/foo.js'] },
})
})
it('should find config for one file, and not care about other', async () => {
// Base cwd
loadConfig.mockResolvedValueOnce({})
// '/foo.js'
loadConfig.mockResolvedValueOnce({})
// '/deeper/foo.js'
loadConfig.mockResolvedValueOnce({ config, filepath: '/deeper/.lintstagedrc.json' })
const configGroups = await getConfigGroups({
files: ['/foo.js', '/deeper/foo.js'],
})
expect(configGroups).toEqual({
'/deeper/.lintstagedrc.json': { config, files: ['/deeper/foo.js'] },
})
})
})