/
index.spec.js
327 lines (253 loc) Β· 7.86 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import path from 'path'
import { lilconfig } from 'lilconfig'
import makeConsoleMock from 'consolemock'
jest.unmock('execa')
import { getStagedFiles } from '../lib/getStagedFiles'
import lintStaged from '../lib/index'
import { InvalidOptionsError } from '../lib/symbols'
import { validateOptions } from '../lib/validateOptions'
import { replaceSerializer } from './utils/replaceSerializer'
const mockLilConfig = (result) => {
lilconfig.mockImplementationOnce(() => ({
search: () => Promise.resolve(result),
}))
}
/**
* This converts paths into `file://` urls, but this doesn't
* work with `import()` when using babel + jest.
*/
jest.mock('url', () => ({
pathToFileURL: (path) => path,
}))
jest.mock('../lib/getStagedFiles')
jest.mock('../lib/gitWorkflow')
jest.mock('../lib/validateOptions', () => ({
validateOptions: jest.fn().mockImplementation(async () => {}),
}))
// 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 use lilconfig if no params are passed', async () => {
expect.assertions(1)
const config = { '*': 'mytask' }
mockLilConfig({ config })
await lintStaged(undefined, logger)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
ERROR β Failed to get staged files!"
`)
})
it('should return true when passed', async () => {
expect.assertions(1)
getStagedFiles.mockImplementationOnce(async () => ['sample.java'])
const config = { '*': 'node -e "process.exit(0)"' }
await expect(lintStaged({ config, quiet: true }, logger)).resolves.toEqual(true)
})
it('should use use the console if no logger is passed', async () => {
expect.assertions(2)
mockLilConfig({ config: {} })
const previousConsole = console
const mockedConsole = makeConsoleMock()
console = mockedConsole
await expect(lintStaged()).rejects.toMatchInlineSnapshot(
`[Error: Configuration should not be empty]`
)
expect(mockedConsole.printHistory()).toMatchInlineSnapshot(`""`)
console = previousConsole
})
it('should output config in debug mode', async () => {
expect.assertions(1)
const config = { '*': 'mytask' }
mockLilConfig({ config })
await lintStaged({ debug: true, quiet: true }, logger)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
LOG Running lint-staged with the following config:
LOG {
'*': 'mytask'
}"
`)
})
it('should not output config in normal mode', async () => {
expect.assertions(1)
const config = { '*': 'mytask' }
mockLilConfig({ config })
await lintStaged({ quiet: true }, logger)
expect(logger.printHistory()).toMatchInlineSnapshot(`""`)
})
it('should throw when invalid options are provided', async () => {
expect.assertions(2)
validateOptions.mockImplementationOnce(async () => {
throw InvalidOptionsError
})
await expect(lintStaged({ '*': 'mytask' }, logger)).rejects.toMatchInlineSnapshot(
`[Error: Invalid Options]`
)
expect(logger.printHistory()).toMatchInlineSnapshot(`""`)
})
it('should throw when invalid config is provided', async () => {
const config = {}
mockLilConfig({ config })
await expect(lintStaged({ quiet: true }, logger)).rejects.toMatchInlineSnapshot(
`[Error: Configuration should not be empty]`
)
expect(logger.printHistory()).toMatchInlineSnapshot(`""`)
})
it('should load JSON config file', async () => {
expect.assertions(1)
await lintStaged(
{
configPath: path.join(__dirname, '__mocks__', 'my-config.json'),
debug: true,
quiet: true,
},
logger
)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
LOG Running lint-staged with the following config:
LOG {
'*': 'mytask'
}"
`)
})
it('should load CommonJS config file from absolute path', async () => {
expect.assertions(1)
await lintStaged(
{
configPath: path.join(__dirname, '__mocks__', 'advanced-config.js'),
debug: true,
quiet: true,
},
logger
)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
LOG Running lint-staged with the following config:
LOG {
'*.css': [Function: *.css],
'*.js': [Function: *.js]
}"
`)
})
it('should load CommonJS config file from relative path', async () => {
expect.assertions(1)
await lintStaged(
{
configPath: path.join('test', '__mocks__', 'advanced-config.js'),
debug: true,
quiet: true,
},
logger
)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
LOG Running lint-staged with the following config:
LOG {
'*.css': [Function: *.css],
'*.js': [Function: *.js]
}"
`)
})
it('should load CommonJS config file from .cjs file', async () => {
expect.assertions(1)
await lintStaged(
{
configPath: path.join('test', '__mocks__', 'my-config.cjs'),
debug: true,
quiet: true,
},
logger
)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
LOG Running lint-staged with the following config:
LOG {
'*': 'mytask'
}"
`)
})
it('should load EMS config file from .mjs file', async () => {
expect.assertions(1)
await lintStaged(
{
configPath: path.join('test', '__mocks__', 'esm-config.mjs'),
debug: true,
quiet: true,
},
logger
)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
LOG Running lint-staged with the following config:
LOG {
'*': 'mytask'
}"
`)
})
it('should load EMS config file from .js file', async () => {
expect.assertions(1)
await lintStaged(
{
configPath: path.join('test', '__mocks__', 'esm-config-in-js.js'),
debug: true,
quiet: true,
},
logger
)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
LOG Running lint-staged with the following config:
LOG {
'*': 'mytask'
}"
`)
})
it('should use config object', async () => {
expect.assertions(1)
const config = { '*': 'node -e "process.exit(1)"' }
await lintStaged({ config, quiet: true }, logger)
expect(logger.printHistory()).toMatchInlineSnapshot(`""`)
})
it('should load a CJS module when specified', async () => {
expect.assertions(1)
jest.mock('my-lint-staged-config')
await lintStaged({ configPath: 'my-lint-staged-config', quiet: true, debug: true }, logger)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
LOG Running lint-staged with the following config:
LOG {
'*': 'mytask'
}"
`)
})
it('should print helpful error message when config file is not found', async () => {
expect.assertions(2)
mockLilConfig(null)
await expect(lintStaged({ quiet: true }, logger)).rejects.toMatchInlineSnapshot(
`[Error: Configuration could not be found]`
)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
ERROR Configuration could not be found."
`)
})
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(
/ENOENT: no such file or directory, open '([^']+)'/,
`ENOENT: no such file or directory, open '${nonExistentConfig}'`
)
)
await expect(
lintStaged({ configPath: nonExistentConfig, quiet: true }, logger)
).rejects.toThrowError()
expect(logger.printHistory()).toMatchInlineSnapshot(`""`)
})
})