/
index.spec.js
172 lines (147 loc) Β· 4.85 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
import { cosmiconfig } from 'cosmiconfig'
import makeConsoleMock from 'consolemock'
import path from 'path'
jest.unmock('execa')
// eslint-disable-next-line import/first
import getStagedFiles from '../lib/getStagedFiles'
// eslint-disable-next-line import/first
import lintStaged from '../lib/index'
import { replaceSerializer } from './utils/replaceSerializer'
jest.mock('../lib/getStagedFiles')
const mockCosmiconfigWith = (result) => {
cosmiconfig.mockImplementationOnce(() => ({
search: () => Promise.resolve(result),
}))
}
jest.mock('../lib/gitWorkflow')
async function withMockedConsole(mockConsole, fn) {
const previousConsole = console
try {
console = mockConsole
await fn()
} finally {
console = previousConsole
}
}
// 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 cosmiconfig if no params are passed', async () => {
expect.assertions(1)
const config = {
'*': 'mytask',
}
mockCosmiconfigWith({ config })
await lintStaged(undefined, logger)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should return true when passed', async () => {
expect.assertions(1)
const config = {
'*': 'node -e "process.exit(0)"',
}
getStagedFiles.mockImplementationOnce(async () => ['sample.java'])
const passed = await lintStaged({ config, quiet: true }, logger)
expect(passed).toEqual(true)
})
it('should use use the console if no logger is passed', async () => {
expect.assertions(1)
mockCosmiconfigWith({ config: {} })
const mockedConsole = makeConsoleMock()
try {
await withMockedConsole(mockedConsole, () => lintStaged())
} catch (ignore) {
expect(mockedConsole.printHistory()).toMatchSnapshot()
}
})
it('should output config in debug mode', async () => {
expect.assertions(1)
const config = {
'*': 'mytask',
}
mockCosmiconfigWith({ config })
await lintStaged({ debug: true, quiet: true }, logger)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should not output config in normal mode', async () => {
expect.assertions(1)
const config = {
'*': 'mytask',
}
mockCosmiconfigWith({ config })
await lintStaged({ quiet: true }, logger)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should throw when invalid config is provided', async () => {
const config = {}
mockCosmiconfigWith({ config })
await expect(lintStaged({ quiet: true }, logger)).rejects.toMatchInlineSnapshot(
`[Error: Configuration should not be empty!]`
)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should load config file when specified', async () => {
expect.assertions(1)
await lintStaged(
{
configPath: path.join(__dirname, '__mocks__', 'my-config.json'),
debug: true,
quiet: true,
},
logger
)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should parse function linter from js config', async () => {
expect.assertions(1)
await lintStaged(
{
configPath: path.join(__dirname, '__mocks__', 'advanced-config.js'),
debug: true,
quiet: true,
},
logger
)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should use config object', async () => {
const config = {
'*': 'node -e "process.exit(1)"',
}
expect.assertions(1)
await lintStaged({ config, quiet: true }, logger)
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({ configPath: 'my-lint-staged-config', quiet: true, debug: true }, logger)
expect(logger.printHistory()).toMatchSnapshot()
})
it('should print helpful error message when config file is not found', async () => {
expect.assertions(2)
mockCosmiconfigWith(null)
await expect(lintStaged({ quiet: true }, logger)).rejects.toMatchInlineSnapshot(
`[Error: Config could not be found]`
)
expect(logger.printHistory()).toMatchSnapshot()
})
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 expect(
lintStaged({ configPath: nonExistentConfig, quiet: true }, logger)
).rejects.toThrowError()
expect(logger.printHistory()).toMatchSnapshot()
})
})