/
runAll.spec.js
233 lines (205 loc) Β· 7.6 KB
/
runAll.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import makeConsoleMock from 'consolemock'
import execa from 'execa'
import normalize from 'normalize-path'
import path from 'path'
import getStagedFiles from '../lib/getStagedFiles'
import GitWorkflow from '../lib/gitWorkflow'
import resolveGitRepo from '../lib/resolveGitRepo'
import runAll from '../lib/runAll'
import { GitError } from '../lib/symbols'
jest.mock('../lib/file')
jest.mock('../lib/getStagedFiles')
jest.mock('../lib/gitWorkflow')
jest.mock('../lib/resolveGitRepo')
getStagedFiles.mockImplementation(async () => [])
resolveGitRepo.mockImplementation(async () => {
const cwd = process.cwd()
return { gitConfigDir: normalize(path.resolve(cwd, '.git')), gitDir: normalize(cwd) }
})
describe('runAll', () => {
const globalConsoleTemp = console
beforeAll(() => {
console = makeConsoleMock()
})
afterEach(() => {
console.clearHistory()
})
afterAll(() => {
console = globalConsoleTemp
})
it('should resolve the promise with no tasks', async () => {
expect.assertions(1)
await expect(runAll({ config: {} })).resolves.toMatchInlineSnapshot(`
Object {
"errors": Set {},
"hasPartiallyStagedFiles": null,
"output": Array [
"i No staged files found.",
],
"quiet": false,
"shouldBackup": true,
}
`)
})
it('should not print output when no staged files and quiet', async () => {
expect.assertions(1)
await expect(runAll({ config: {}, quiet: true })).resolves.toMatchInlineSnapshot(`
Object {
"errors": Set {},
"hasPartiallyStagedFiles": null,
"output": Array [],
"quiet": true,
"shouldBackup": true,
}
`)
})
it('should resolve the promise with no files', async () => {
expect.assertions(1)
await runAll({ config: { '*.js': ['echo "sample"'] } })
expect(console.printHistory()).toMatchInlineSnapshot(`""`)
})
it('should use an injected logger', async () => {
expect.assertions(1)
const logger = makeConsoleMock()
await runAll({ config: { '*.js': ['echo "sample"'] }, debug: true }, logger)
expect(logger.printHistory()).toMatchInlineSnapshot(`""`)
})
it('should exit without output when no staged files match configured tasks and quiet', async () => {
expect.assertions(1)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
await runAll({ config: { '*.css': ['echo "sample"'] }, quiet: true })
expect(console.printHistory()).toMatchInlineSnapshot(`""`)
})
it('should not skip tasks if there are files', async () => {
expect.assertions(1)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
await runAll({ config: { '*.js': ['echo "sample"'] } })
expect(console.printHistory()).toMatchInlineSnapshot(`
"
LOG [STARTED] Preparing...
LOG [SUCCESS] Preparing...
LOG [STARTED] Running tasks...
LOG [STARTED] Running tasks for *.js
LOG [STARTED] echo \\"sample\\"
LOG [SUCCESS] echo \\"sample\\"
LOG [SUCCESS] Running tasks for *.js
LOG [SUCCESS] Running tasks...
LOG [STARTED] Applying modifications...
LOG [SUCCESS] Applying modifications...
LOG [STARTED] Cleaning up...
LOG [SUCCESS] Cleaning up..."
`)
})
it('should skip tasks if previous git error', async () => {
expect.assertions(2)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
GitWorkflow.mockImplementationOnce(() => ({
...jest.requireActual('../lib/gitWorkflow'),
prepare: (ctx) => {
ctx.errors.add(GitError)
throw new Error('test')
},
}))
await expect(
runAll({ config: { '*.js': ['echo "sample"'] } })
).rejects.toThrowErrorMatchingInlineSnapshot(`"lint-staged failed"`)
expect(console.printHistory()).toMatchInlineSnapshot(`
"
LOG [STARTED] Preparing...
ERROR [FAILED] test
LOG [STARTED] Running tasks...
LOG [STARTED] Applying modifications...
LOG [STARTED] Cleaning up..."
`)
})
it('should skip applying unstaged modifications if there are errors during linting', async () => {
expect.assertions(2)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
execa.mockImplementation(() =>
Promise.resolve({
stdout: '',
stderr: 'Linter finished with error',
code: 1,
failed: true,
cmd: 'mock cmd',
})
)
await expect(
runAll({ config: { '*.js': ['echo "sample"'] } })
).rejects.toThrowErrorMatchingInlineSnapshot(`"lint-staged failed"`)
expect(console.printHistory()).toMatchInlineSnapshot(`
"
LOG [STARTED] Preparing...
LOG [SUCCESS] Preparing...
LOG [STARTED] Running tasks...
LOG [STARTED] Running tasks for *.js
LOG [STARTED] echo \\"sample\\"
ERROR [FAILED] echo \\"sample\\" [1]
ERROR [FAILED] echo \\"sample\\" [1]
LOG [SUCCESS] Running tasks...
LOG [STARTED] Applying modifications...
LOG [STARTED] Reverting to original state because of errors...
LOG [SUCCESS] Reverting to original state because of errors...
LOG [STARTED] Cleaning up...
LOG [SUCCESS] Cleaning up..."
`)
})
it('should skip tasks and restore state if terminated', async () => {
expect.assertions(2)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
execa.mockImplementation(() =>
Promise.resolve({
stdout: '',
stderr: '',
code: 0,
failed: false,
killed: true,
signal: 'SIGINT',
cmd: 'mock cmd',
})
)
await expect(
runAll({ config: { '*.js': ['echo "sample"'] } })
).rejects.toThrowErrorMatchingInlineSnapshot(`"lint-staged failed"`)
expect(console.printHistory()).toMatchInlineSnapshot(`
"
LOG [STARTED] Preparing...
LOG [SUCCESS] Preparing...
LOG [STARTED] Running tasks...
LOG [STARTED] Running tasks for *.js
LOG [STARTED] echo \\"sample\\"
ERROR [FAILED] echo \\"sample\\" [SIGINT]
ERROR [FAILED] echo \\"sample\\" [SIGINT]
LOG [SUCCESS] Running tasks...
LOG [STARTED] Applying modifications...
LOG [STARTED] Reverting to original state because of errors...
LOG [SUCCESS] Reverting to original state because of errors...
LOG [STARTED] Cleaning up...
LOG [SUCCESS] Cleaning up..."
`)
})
it('should resolve matched files to cwd when using relative option', async () => {
// A staged file inside test/, which will be our cwd
getStagedFiles.mockImplementationOnce(async () => ['test/foo.js'])
// We are only interested in the `matchedFileChunks` generation
let expected
const mockConstructor = jest.fn(({ matchedFileChunks }) => (expected = matchedFileChunks))
GitWorkflow.mockImplementationOnce(mockConstructor)
const mockTask = jest.fn(() => ['echo "sample"'])
// actual cwd
const cwd = process.cwd()
// For the test, set cwd in test/
const innerCwd = path.join(cwd, 'test/')
try {
// Run lint-staged in `innerCwd` with relative option
// This means the sample task will receive `foo.js`
await runAll({ config: { '*.js': mockTask }, stash: false, relative: true, cwd: innerCwd })
} catch {} // eslint-disable-line no-empty
// task received relative `foo.js`
expect(mockTask).toHaveBeenCalledTimes(1)
expect(mockTask).toHaveBeenCalledWith(['foo.js'])
// GitWorkflow received absolute `test/foo.js`
expect(mockConstructor).toHaveBeenCalledTimes(1)
expect(expected).toEqual([[normalize(path.join(cwd, 'test/foo.js'))]])
})
})