-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
runScript-mock-findBin.spec.js
66 lines (57 loc) · 1.7 KB
/
runScript-mock-findBin.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
// This is in a separate test file because I was unable to get `jest.mock` working in a test block
// `jest.mock` gets hoisted, but even with `jest.doMock` it wasn't working
/* 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')
// Mock findBin to return an absolute path
jest.mock('../src/findBin', () => (commands) => {
const [
bin,
...otherArgs
] = commands.split(' ')
return ({
bin: `/usr/local/bin/${ bin }`,
args: otherArgs
})
}, { virtual: true })
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.only('runScript with absolute paths', () => {
beforeEach(() => {
mockFn.mockReset()
mockFn.mockImplementation(() => Promise.resolve(true))
})
it('can pass `gitDir` as `cwd` to `execa()` when git is called via absolute path', async () => {
const res = runScript(
['git add'],
['test.js'],
packageJSON,
{ gitDir: '../' }
)
const taskPromise = res[0].task()
expect(taskPromise).toBeAPromise()
await taskPromise
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0][0]).toMatch('/usr/local/bin/git')
expect(mockFn.mock.calls[0][1]).toEqual(['add', 'test.js'])
expect(mockFn.mock.calls[0][2]).toEqual({ cwd: '../' })
})
})