-
-
Notifications
You must be signed in to change notification settings - Fork 411
/
makeCmdTasks.spec.js
146 lines (126 loc) Β· 4.54 KB
/
makeCmdTasks.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
import { getMockExeca } from './__utils__/getMockExeca.js'
const { execa } = await getMockExeca()
const { makeCmdTasks } = await import('../../lib/makeCmdTasks.js')
describe('makeCmdTasks', () => {
const gitDir = process.cwd()
beforeEach(() => {
execa.mockClear()
})
it('should return an array', async () => {
const array = await makeCmdTasks({ commands: 'test', gitDir, files: ['test.js'] })
expect(array).toBeInstanceOf(Array)
})
it('should work with a single command', async () => {
expect.assertions(4)
const res = await makeCmdTasks({ commands: 'test', gitDir, files: ['test.js'] })
expect(res.length).toBe(1)
const [linter] = res
expect(linter.title).toBe('test')
expect(linter.task).toBeInstanceOf(Function)
const taskPromise = linter.task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
})
it('should work with multiple commands', async () => {
expect.assertions(9)
const res = await makeCmdTasks({
commands: ['test', 'test2'],
gitDir,
files: ['test.js'],
})
expect(res.length).toBe(2)
const [linter1, linter2] = res
expect(linter1.title).toBe('test')
expect(linter2.title).toBe('test2')
let taskPromise = linter1.task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(execa).toHaveBeenCalledTimes(1)
expect(execa).lastCalledWith('test', ['test.js'], {
cwd: process.cwd(),
preferLocal: true,
reject: false,
shell: false,
stdin: 'ignore',
})
taskPromise = linter2.task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(execa).toHaveBeenCalledTimes(2)
expect(execa).lastCalledWith('test2', ['test.js'], {
cwd: process.cwd(),
preferLocal: true,
reject: false,
shell: false,
stdin: 'ignore',
})
})
it('should work with function task returning a string', async () => {
const res = await makeCmdTasks({ commands: () => 'test', gitDir, files: ['test.js'] })
expect(res.length).toBe(1)
expect(res[0].title).toEqual('test')
})
it('should work with function task returning array of string', async () => {
const res = await makeCmdTasks({
commands: () => ['test', 'test2'],
gitDir,
files: ['test.js'],
})
expect(res.length).toBe(2)
expect(res[0].title).toEqual('test')
expect(res[1].title).toEqual('test2')
})
it('should work with function task accepting arguments', async () => {
const res = await makeCmdTasks({
commands: (filenames) => filenames.map((file) => `test ${file}`),
gitDir,
files: ['test.js', 'test2.js'],
})
expect(res.length).toBe(2)
expect(res[0].title).toEqual('test test.js')
expect(res[1].title).toEqual('test test2.js')
})
it('should work with array of mixed string and function tasks', async () => {
const res = await makeCmdTasks({
commands: [() => 'test', 'test2', (files) => files.map((file) => `test ${file}`)],
gitDir,
files: ['test.js', 'test2.js', 'test3.js'],
})
expect(res.length).toBe(5)
expect(res[0].title).toEqual('test')
expect(res[1].title).toEqual('test2')
expect(res[2].title).toEqual('test test.js')
expect(res[3].title).toEqual('test test2.js')
expect(res[4].title).toEqual('test test3.js')
})
it('should work with async function tasks', async () => {
const res = await makeCmdTasks({ commands: async () => 'test', gitDir, files: ['test.js'] })
expect(res.length).toBe(1)
expect(res[0].title).toEqual('test')
})
it("should throw when function task doesn't return string | string[]", async () => {
await expect(makeCmdTasks({ commands: () => null, gitDir, files: ['test.js'] })).rejects
.toThrowErrorMatchingInlineSnapshot(`
"β Validation Error:
Invalid value for '[Function]': null
Function task should return a string or an array of strings"
`)
})
it('should prevent function from mutating original file list', async () => {
const files = ['test.js']
const res = await makeCmdTasks({
commands: (stagedFiles) => {
/** Array.splice() mutates the array */
stagedFiles.splice(0, 1)
expect(stagedFiles).toEqual([])
return stagedFiles.map((file) => `test ${file}`)
},
gitDir,
files,
})
/** Because function mutated file list, it was empty and no tasks were created... */
expect(res.length).toBe(0)
/** ...but the original file list was not mutated */
expect(files).toEqual(['test.js'])
})
})