/
makeCmdTasks.spec.js
136 lines (117 loc) Β· 4.23 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
import execa from 'execa'
import makeCmdTasks from '../lib/makeCmdTasks'
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'], {
preferLocal: true,
reject: false,
shell: false,
})
taskPromise = linter2.task()
expect(taskPromise).toBeInstanceOf(Promise)
await taskPromise
expect(execa).toHaveBeenCalledTimes(2)
expect(execa).lastCalledWith('test2', ['test.js'], {
preferLocal: true,
reject: false,
shell: false,
})
})
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]'.
Function task should return a string or an array of strings.
Configured value is: null
Please refer to https://github.com/okonet/lint-staged#configuration for more information..."
`)
})
it('should truncate task title', async () => {
const longString = new Array(1000)
.fill()
.map((_, index) => index)
.join('')
const res = await makeCmdTasks({ commands: () => longString, gitDir, files: ['test.js'] })
expect(res.length).toBe(1)
expect(res[0].title).toMatchInlineSnapshot(
`"0123456789101112131415161718192021222324252627282930313233343536373839404142434β¦"`
)
})
})