-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
runScript.spec.js
149 lines (130 loc) Β· 5.4 KB
/
runScript.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
import mockFn from 'execa'
import logSymbols from 'log-symbols'
import runScript from '../src/runScript'
jest.mock('execa')
const scripts = {
test: 'noop',
test2: 'noop'
}
describe('runScript', () => {
afterEach(() => {
mockFn.mockClear()
})
it('should return an array', () => {
expect(runScript('test', ['test.js'], scripts)).toBeInstanceOf(Array)
})
it('should throw for non-existend script', () => {
expect(() => {
runScript('missing-module', ['test.js'], scripts)[0].task()
}).toThrow()
})
it('should work with a single command', async () => {
const res = runScript('test', ['test.js'], scripts)
expect(res.length).toBe(1)
expect(res[0].title).toBe('test')
expect(res[0].task).toBeInstanceOf(Function)
const taskPromise = res[0].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
})
it('should work with multiple commands', async () => {
const res = runScript(['test', 'test2'], ['test.js'], scripts)
expect(res.length).toBe(2)
expect(res[0].title).toBe('test')
expect(res[1].title).toBe('test2')
let taskPromise = res[0].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(['npm', ['run', '--silent', 'test', '--', 'test.js'], {}])
taskPromise = res[1].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(2)
expect(mockFn.mock.calls[1]).toEqual(['npm', ['run', '--silent', 'test2', '--', 'test.js'], {}])
})
it('should respect chunk size', async () => {
const res = runScript(['test'], ['test1.js', 'test2.js'], scripts, {
chunkSize: 1
})
const taskPromise = res[0].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(2)
expect(mockFn.mock.calls[0]).toEqual(['npm', ['run', '--silent', 'test', '--', 'test1.js'], {}])
expect(mockFn.mock.calls[1]).toEqual(['npm', ['run', '--silent', 'test', '--', 'test2.js'], {}])
})
it('should support non npm scripts', async () => {
const res = runScript(['node --arg=true ./myscript.js', 'git add'], ['test.js'], scripts)
expect(res.length).toBe(2)
expect(res[0].title).toBe('node --arg=true ./myscript.js')
expect(res[1].title).toBe('git add')
let taskPromise = res[0].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0][0]).toContain('node')
expect(mockFn.mock.calls[0][1]).toEqual(['--arg=true', './myscript.js', 'test.js'])
taskPromise = res[1].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(2)
expect(mockFn.mock.calls[1][0]).toContain('git')
expect(mockFn.mock.calls[1][1]).toEqual(['add', 'test.js'])
})
it('should pass cwd to execa if gitDir option is set for non-npm tasks', async () => {
const res = runScript(['test', 'git add'], ['test.js'], scripts, { gitDir: '../' })
let taskPromise = res[0].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(['npm', ['run', '--silent', 'test', '--', 'test.js'], {}])
taskPromise = res[1].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(2)
expect(mockFn.mock.calls[1][0]).toMatch(/git$/)
expect(mockFn.mock.calls[1][1]).toEqual(['add', 'test.js'])
expect(mockFn.mock.calls[1][2]).toEqual({ cwd: '../' })
})
it('should not pass `gitDir` as `cwd` to `execa()` if a non-git binary is called', async () => {
const res = runScript(['jest'], ['test.js'], scripts, { gitDir: '../' })
const taskPromise = res[0].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(['jest', ['test.js'], {}])
})
it('should use --silent in non-verbose mode', async () => {
const res = runScript('test', ['test.js'], scripts, { verbose: false })
const taskPromise = res[0].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(['npm', ['run', '--silent', 'test', '--', 'test.js'], {}])
})
it('should not use --silent in verbose mode', async () => {
const res = runScript('test', ['test.js'], scripts, { verbose: true })
const taskPromise = res[0].task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(['npm', ['run', 'test', '--', 'test.js'], {}])
})
it('should throw error for failed linters', async () => {
const linterErr = new Error()
linterErr.stdout = 'Mock error'
linterErr.stderr = ''
mockFn.mockImplementationOnce(() => Promise.reject(linterErr))
const res = runScript('mock-fail-linter', ['test.js'], scripts)
const taskPromise = res[0].task()
try {
await taskPromise
} catch (err) {
expect(err.message)
.toMatch(`${logSymbols.error} mock-fail-linter found some errors. Please fix them and try committing again.
${linterErr.stdout}
${linterErr.stderr}`)
}
})
})