-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
runScript.spec.js
150 lines (131 loc) Β· 4.67 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
150
/* eslint no-underscore-dangle: 0 */
import expect from 'expect'
import isPromise from 'is-promise'
import mockFn from 'execa'
import runScript from '../src/runScript'
jest.mock('execa')
expect.extend({
toBeAPromise() {
expect.assert(
isPromise(this.actual),
'expected %s to be a Promise',
this.actual
)
return this
}
})
const packageJSON = {
scripts: {
test: 'noop',
test2: 'noop'
},
'lint-staged': {}
}
describe('runScript', () => {
beforeEach(() => {
mockFn.mockReset()
mockFn.mockImplementation(() => new Promise(() => {}))
})
it('should return an array', () => {
expect(runScript('test', 'test.js', packageJSON)).toBeA('array')
})
it('should throw for non-existend script', () => {
expect(() => {
runScript('missing-module', 'test.js', packageJSON)[0].task()
}).toThrow()
})
it('should work with a single command', () => {
const res = runScript('test', 'test.js', packageJSON)
expect(res.length).toBe(1)
expect(res[0].title).toBe('test')
expect(res[0].task).toBeA('function')
expect(res[0].task()).toBeAPromise()
})
it('should work with multiple commands', () => {
const res = runScript(['test', 'test2'], 'test.js', packageJSON)
expect(res.length).toBe(2)
expect(res[0].title).toBe('test')
expect(res[1].title).toBe('test2')
expect(res[0].task()).toBeAPromise()
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(
['npm', ['run', '--silent', 'test', '--', 'test.js'], {}]
)
expect(res[1].task()).toBeAPromise()
expect(mockFn.mock.calls.length).toEqual(2)
expect(mockFn.mock.calls[1]).toEqual(
['npm', ['run', '--silent', 'test2', '--', 'test.js'], {}]
)
})
it('should support non npm scripts', () => {
const res = runScript(['node --arg=true ./myscript.js', 'git add'], 'test.js', packageJSON)
expect(res.length).toBe(2)
expect(res[0].title).toBe('node --arg=true ./myscript.js')
expect(res[1].title).toBe('git add')
expect(res[0].task()).toBeAPromise()
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'])
expect(res[1].task()).toBeAPromise()
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', () => {
const res = runScript(
['test', 'git add'],
'test.js',
packageJSON,
{ gitDir: '../' }
)
expect(res[0].task()).toBeAPromise()
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(
['npm', ['run', '--silent', 'test', '--', 'test.js'], {}]
)
expect(res[1].task()).toBeAPromise()
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', () => {
const res = runScript(
['jest'],
'test.js',
packageJSON,
{ gitDir: '../' }
)
expect(res[0].task()).toBeAPromise()
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(
['jest', ['--', 'test.js'], {}]
)
})
it('should use --silent in non-verbose mode', () => {
const res = runScript(
'test',
'test.js',
packageJSON,
{ verbose: false }
)
expect(res[0].task()).toBeAPromise()
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', () => {
const res = runScript(
'test',
'test.js',
packageJSON,
{ verbose: true }
)
expect(res[0].task()).toBeAPromise()
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(
['npm', ['run', 'test', '--', 'test.js'], {}]
)
})
})