/
index.spec.js
133 lines (112 loc) Β· 3.77 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import cosmiconfig from 'cosmiconfig'
import makeConsoleMock from 'consolemock'
import path from 'path'
jest.unmock('execa')
// silence console from Jest output
console.log = jest.fn(() => {})
console.error = jest.fn(() => {})
// eslint-disable-next-line import/first
import getStagedFiles from '../src/getStagedFiles'
// eslint-disable-next-line import/first
import lintStaged from '../src/index'
jest.mock('../src/getStagedFiles')
const replaceSerializer = (from, to) => ({
test: val => typeof val === 'string' && from.test(val),
print: val => val.replace(from, to)
})
const mockCosmiconfigWith = result => {
cosmiconfig.mockImplementationOnce(() => ({
search: () => Promise.resolve(result)
}))
}
jest.mock('../src/gitWorkflow')
// TODO: Never run tests in the project's WC because this might change source files git status
describe('lintStaged', () => {
const logger = makeConsoleMock()
beforeEach(() => {
logger.clearHistory()
})
it('should output config in debug mode', async () => {
expect.assertions(1)
const config = {
'*': 'mytask'
}
mockCosmiconfigWith({ config })
await lintStaged(logger, undefined, false, false, true)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should not output config in normal mode', async () => {
expect.assertions(1)
const config = {
'*': 'mytask'
}
mockCosmiconfigWith({ config })
await lintStaged(logger)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should throw when invalid config is provided', async () => {
const config = {}
mockCosmiconfigWith({ config })
await lintStaged(logger)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should load config file when specified', async () => {
expect.assertions(1)
await lintStaged(
logger,
path.join(__dirname, '__mocks__', 'my-config.json'),
false,
false,
true
)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should parse function linter from js config', async () => {
expect.assertions(1)
await lintStaged(
logger,
path.join(__dirname, '__mocks__', 'advanced-config.js'),
false,
false,
true
)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should load an npm config package when specified', async () => {
expect.assertions(1)
jest.mock('my-lint-staged-config')
await lintStaged(logger, 'my-lint-staged-config', false, false, true)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should print helpful error message when config file is not found', async () => {
expect.assertions(2)
mockCosmiconfigWith(null)
await lintStaged(logger)
expect(logger.printHistory()).toMatchSnapshot()
expect(process.exitCode).toEqual(1)
})
it('should print helpful error message when explicit config file is not found', async () => {
expect.assertions(2)
const nonExistentConfig = 'fake-config-file.yml'
// Serialize Windows, Linux and MacOS paths consistently
expect.addSnapshotSerializer(
replaceSerializer(
/Error: ENOENT: no such file or directory, open '([^']+)'/,
`Error: ENOENT: no such file or directory, open '${nonExistentConfig}'`
)
)
await lintStaged(logger, nonExistentConfig)
expect(logger.printHistory()).toMatchSnapshot()
expect(process.exitCode).toEqual(1)
})
it('should exit with code 1 on linter errors', async () => {
const config = {
'*': 'node -e "process.exit(1)"'
}
mockCosmiconfigWith({ config })
getStagedFiles.mockImplementationOnce(async () => ['sample.java'])
await lintStaged(logger, undefined)
expect(console.error).toHaveBeenCalledWith(expect.stringContaining('node -e "process.exit(1)"'))
expect(process.exitCode).toEqual(1)
})
})