forked from lint-staged/lint-staged
/
runAll.spec.js
191 lines (169 loc) Β· 6.06 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
import makeConsoleMock from 'consolemock'
import sgfMock from 'staged-git-files'
import execa from 'execa'
import { getConfig } from '../src/getConfig'
import runAll from '../src/runAll'
import { hasPartiallyStagedFiles, gitStashSave, gitStashPop, updateStash } from '../src/gitWorkflow'
jest.mock('staged-git-files')
jest.mock('../src/gitWorkflow')
sgfMock.mockImplementation((params, callback) => {
callback(null, [])
})
const globalConsoleTemp = global.console
describe('runAll', () => {
beforeAll(() => {
global.console = makeConsoleMock()
})
afterEach(() => {
global.console.clearHistory()
gitStashSave.mockClear()
gitStashPop.mockClear()
updateStash.mockClear()
})
afterAll(() => {
global.console = globalConsoleTemp
})
it('should throw when invalid config is provided', () => {
expect(() => runAll({})).toThrowErrorMatchingSnapshot()
expect(() => runAll()).toThrowErrorMatchingSnapshot()
})
it('should not throw when a valid config is provided', () => {
const config = getConfig({
concurrent: false
})
expect(() => runAll(config)).not.toThrow()
})
it('should return a promise', () => {
expect(runAll(getConfig({}))).toBeInstanceOf(Promise)
})
it('should resolve the promise with no tasks', async () => {
expect.assertions(1)
const res = await runAll(getConfig({}))
expect(res).toEqual('No tasks to run.')
})
it('should resolve the promise with no files', async () => {
expect.assertions(1)
await runAll(getConfig({ linters: { '*.js': ['echo "sample"'] } }))
expect(console.printHistory()).toMatchSnapshot()
})
it('should not skip tasks if there are files', async () => {
expect.assertions(1)
sgfMock.mockImplementationOnce((params, callback) => {
callback(null, [{ filename: 'sample.js', status: 'sample' }])
})
await runAll(getConfig({ linters: { '*.js': ['echo "sample"'] } }))
expect(console.printHistory()).toMatchSnapshot()
})
it('should not skip stashing and restoring if there are partially staged files', async () => {
expect.assertions(4)
hasPartiallyStagedFiles.mockImplementationOnce(() => Promise.resolve(true))
sgfMock.mockImplementationOnce((params, callback) => {
callback(null, [{ filename: 'sample.js', status: 'Modified' }])
})
await runAll(getConfig({ linters: { '*.js': ['echo "sample"'] } }))
expect(gitStashSave).toHaveBeenCalledTimes(1)
expect(updateStash).toHaveBeenCalledTimes(1)
expect(gitStashPop).toHaveBeenCalledTimes(1)
expect(console.printHistory()).toMatchSnapshot()
})
it('should skip stashing and restoring if there are no partially staged files', async () => {
expect.assertions(4)
hasPartiallyStagedFiles.mockImplementationOnce(() => Promise.resolve(false))
sgfMock.mockImplementationOnce((params, callback) => {
callback(null, [{ filename: 'sample.js', status: 'Modified' }])
})
await runAll(getConfig({ linters: { '*.js': ['echo "sample"'] } }))
expect(gitStashSave).toHaveBeenCalledTimes(0)
expect(updateStash).toHaveBeenCalledTimes(0)
expect(gitStashPop).toHaveBeenCalledTimes(0)
expect(console.printHistory()).toMatchSnapshot()
})
it('should skip updating stash if there are errors during linting', async () => {
expect.assertions(4)
hasPartiallyStagedFiles.mockImplementationOnce(() => Promise.resolve(true))
sgfMock.mockImplementationOnce((params, callback) => {
callback(null, [{ filename: 'sample.js', status: 'Modified' }])
})
execa.mockImplementationOnce(() =>
Promise.resolve({
stdout: '',
stderr: 'Linter finished with error',
code: 1,
failed: true,
cmd: 'mock cmd'
})
)
try {
await runAll(getConfig({ linters: { '*.js': ['echo "sample"'] } }))
} catch (err) {
console.log(err)
}
expect(console.printHistory()).toMatchSnapshot()
expect(gitStashSave).toHaveBeenCalledTimes(1)
expect(updateStash).toHaveBeenCalledTimes(0)
expect(gitStashPop).toHaveBeenCalledTimes(1)
})
it('should skip linters and stash update but perform working copy restore if terminated', async () => {
expect.assertions(4)
hasPartiallyStagedFiles.mockImplementationOnce(() => Promise.resolve(true))
sgfMock.mockImplementationOnce((params, callback) => {
callback(null, [{ filename: 'sample.js', status: 'Modified' }])
})
execa.mockImplementationOnce(() =>
Promise.resolve({
stdout: '',
stderr: '',
code: 0,
failed: false,
killed: true,
signal: 'SIGINT',
cmd: 'mock cmd'
})
)
try {
await runAll(getConfig({ linters: { '*.js': ['echo "sample"'] } }))
} catch (err) {
console.log(err)
}
expect(console.printHistory()).toMatchSnapshot()
expect(gitStashSave).toHaveBeenCalledTimes(1)
expect(updateStash).toHaveBeenCalledTimes(0)
expect(gitStashPop).toHaveBeenCalledTimes(1)
})
it('should reject promise when staged-git-files errors', async () => {
expect.assertions(1)
sgfMock.mockImplementationOnce((params, callback) => {
callback('test', undefined)
})
try {
await runAll(getConfig({}))
} catch (err) {
expect(err).toEqual('test')
}
})
it('should skip stashing changes if no lint-staged files are changed', async () => {
expect.assertions(4)
hasPartiallyStagedFiles.mockImplementationOnce(() => Promise.resolve(true))
sgfMock.mockImplementationOnce((params, callback) => {
callback(null, [{ filename: 'sample.java', status: 'Modified' }])
})
execa.mockImplementationOnce(() =>
Promise.resolve({
stdout: '',
stderr: 'Linter finished with error',
code: 1,
failed: true,
cmd: 'mock cmd'
})
)
try {
await runAll(getConfig({ linters: { '*.js': ['echo "sample"'] } }))
} catch (err) {
console.log(err)
}
expect(console.printHistory()).toMatchSnapshot()
expect(gitStashSave).toHaveBeenCalledTimes(0)
expect(updateStash).toHaveBeenCalledTimes(0)
expect(gitStashPop).toHaveBeenCalledTimes(0)
})
})