/
makeCmdTasks.spec.js
112 lines (100 loc) Β· 3.47 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
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 linter 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 linter 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 linter 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 [file]')
expect(res[1].title).toEqual('test [file]')
})
it('should work with array of mixed string and function linters', 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 [file]')
expect(res[3].title).toEqual('test [file]')
expect(res[4].title).toEqual('test [file]')
})
it('should generate short names for function tasks with long file list', async () => {
const res = await makeCmdTasks({
commands: filenames => `test ${filenames.map(file => `--file ${file}`).join(' ')}`,
gitDir,
files: Array(100).fill('file.js') // 100 times `file.js`
})
expect(res.length).toBe(1)
expect(res[0].title).toEqual('test --file [file]')
})
})