/
runAll.spec.js
102 lines (87 loc) Β· 2.78 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
import makeConsoleMock from 'consolemock'
import execa from 'execa'
import normalize from 'normalize-path'
import path from 'path'
import resolveGitRepo from '../lib/resolveGitRepo'
import getStagedFiles from '../lib/getStagedFiles'
import runAll from '../lib/runAll'
jest.mock('../lib/resolveGitRepo')
jest.mock('../lib/getStagedFiles')
jest.mock('../lib/gitWorkflow')
resolveGitRepo.mockImplementation(async () => {
const cwd = process.cwd()
return { gitConfigDir: normalize(path.resolve(cwd, '.git')), gitDir: normalize(cwd) }
})
getStagedFiles.mockImplementation(async () => [])
const globalConsoleTemp = console
describe('runAll', () => {
beforeAll(() => {
console = makeConsoleMock()
})
afterEach(() => {
console.clearHistory()
})
afterAll(() => {
console = globalConsoleTemp
})
it('should resolve the promise with no tasks', async () => {
const res = await runAll({ config: {} })
expect(res).toEqual('No tasks to run.')
})
it('should resolve the promise with no files', async () => {
await runAll({ config: { '*.js': ['echo "sample"'] } })
expect(console.printHistory()).toMatchSnapshot()
})
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()).toMatchSnapshot()
})
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()).toMatchSnapshot()
})
it('should skip applying unstaged modifications if there are errors during linting', async () => {
expect.assertions(1)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
execa.mockImplementation(() =>
Promise.resolve({
stdout: '',
stderr: 'Linter finished with error',
code: 1,
failed: true,
cmd: 'mock cmd'
})
)
try {
await runAll({ config: { '*.js': ['echo "sample"'] } })
} catch (err) {
console.log(err)
}
expect(console.printHistory()).toMatchSnapshot()
})
it('should skip tasks and restore state if terminated', async () => {
expect.assertions(1)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
execa.mockImplementation(() =>
Promise.resolve({
stdout: '',
stderr: '',
code: 0,
failed: false,
killed: true,
signal: 'SIGINT',
cmd: 'mock cmd'
})
)
try {
await runAll({ config: { '*.js': ['echo "sample"'] } })
} catch (err) {
console.log(err)
}
expect(console.printHistory()).toMatchSnapshot()
})
})