Skip to content

Commit d4b7cac

Browse files
committed
test: update tests
1 parent 0ee9ab8 commit d4b7cac

File tree

6 files changed

+96
-23
lines changed

6 files changed

+96
-23
lines changed
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import debug from 'debug'
22

33
import { makeErr } from './getSpawnedTask.js'
4-
import { configurationError } from './messages.js'
54

65
const debugLog = debug('lint-staged:getFunctionTasks')
76

@@ -18,25 +17,21 @@ export const isFunctionTask = (commands) => !Array.isArray(commands) && typeof c
1817
* @param {Array<string>} files
1918
* @throws {Error} If the function configuration is not valid
2019
*/
21-
export const getFunctionTasks = async (command, files) => {
20+
export const getFunctionTask = async (command, files) => {
2221
debugLog('Creating Listr tasks for function %o', command)
23-
if (typeof command.title === 'string' && typeof command.task === 'function') {
24-
const task = async (ctx) => {
25-
try {
26-
await command.task(files)
27-
} catch (e) {
28-
throw makeErr(command.title, e, ctx)
29-
}
30-
}
3122

32-
return [{ title: command.title, task }]
33-
} else {
34-
throw new Error(
35-
configurationError(
36-
'[Function]',
37-
'Function task should contain `title` and `task` where `title` should be string and `task` should be function.',
38-
command
39-
)
40-
)
23+
const task = async (ctx) => {
24+
try {
25+
await command.task(files)
26+
} catch (e) {
27+
throw makeErr(command.title, e, ctx)
28+
}
4129
}
30+
31+
return [
32+
{
33+
title: command.title,
34+
task,
35+
},
36+
]
4237
}

lib/runAll.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Listr } from 'listr2'
99
import { chunkFiles } from './chunkFiles.js'
1010
import { execGit } from './execGit.js'
1111
import { generateTasks } from './generateTasks.js'
12-
import { getFunctionTasks, isFunctionTask } from './getFunctionTasks.js'
12+
import { getFunctionTask, isFunctionTask } from './getFunctionTask.js'
1313
import { getRenderer } from './getRenderer.js'
1414
import { getSpawnedTasks } from './getSpawnedTasks.js'
1515
import { getStagedFiles } from './getStagedFiles.js'
@@ -192,7 +192,7 @@ export const runAll = async (
192192
const chunkListrTasks = await Promise.all(
193193
generateTasks({ config, cwd: groupCwd, files, relative }).map((task) =>
194194
(isFunctionTask(task.commands)
195-
? getFunctionTasks(task.commands, files)
195+
? getFunctionTask(task.commands, files)
196196
: getSpawnedTasks({
197197
commands: task.commands,
198198
cwd: groupCwd,

lib/validateConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const validateConfigLogic = (config, configPath, logger) => {
6464
errors.push(
6565
configurationError(
6666
pattern,
67-
'Custom task should contain `title` and `task` fields, where `title` should be a string and `task` should be a function.',
67+
'Function task should contain `title` and `task` fields, where `title` should be a string and `task` should be a function.',
6868
task
6969
)
7070
)

test/unit/__snapshots__/validateConfig.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports[`validateConfig should throw and should print validation errors for inva
1515
1616
Invalid value for '*.js': { title: 'Running a custom task' }
1717
18-
Custom task should contain \`title\` and \`task\` fields, where \`title\` should be a string and \`task\` should be a function."
18+
Function task should contain \`title\` and \`task\` fields, where \`title\` should be a string and \`task\` should be a function."
1919
`;
2020

2121
exports[`validateConfig should throw for empty config 1`] = `"Configuration should not be empty"`;

test/unit/getFunctionTask.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { expect, jest } from '@jest/globals'
2+
3+
import { getFunctionTask } from '../../lib/getFunctionTask.js'
4+
import { getInitialState } from '../../lib/state.js'
5+
import { TaskError } from '../../lib/symbols.js'
6+
7+
describe('getFunctionTask', () => {
8+
it('should return wrapped function task', async () => {
9+
const files = ['file.js']
10+
11+
const cmd = {
12+
title: 'My task',
13+
task: jest.fn(),
14+
}
15+
16+
const wrapped = await getFunctionTask(cmd, files)
17+
18+
expect(wrapped).toEqual([
19+
{
20+
title: 'My task',
21+
task: expect.any(Function),
22+
},
23+
])
24+
25+
wrapped[0].task()
26+
27+
expect(cmd.task).toHaveBeenCalledTimes(1)
28+
expect(cmd.task).toHaveBeenCalledWith(files)
29+
})
30+
31+
it('should wrap function task failure', async () => {
32+
const files = ['file.js']
33+
34+
const cmd = {
35+
title: 'My task',
36+
task: jest.fn().mockImplementation(async () => {
37+
throw new Error('test error')
38+
}),
39+
}
40+
41+
const wrapped = await getFunctionTask(cmd, files)
42+
43+
expect(wrapped).toEqual([
44+
{
45+
title: 'My task',
46+
task: expect.any(Function),
47+
},
48+
])
49+
50+
const context = getInitialState()
51+
52+
await expect(wrapped[0].task(context)).rejects.toThrowErrorMatchingInlineSnapshot(
53+
`"My task [FAILED]"`
54+
)
55+
56+
expect(context.errors.has(TaskError)).toEqual(true)
57+
})
58+
})

test/unit/runAll.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,24 @@ describe('runAll', () => {
365365
await runAll({ configObject: { '*.js': ['echo "sample"'] }, diff: 'branch1...branch2' })
366366
expect(console.printHistory()).toMatch('Skipping backup because `--diff` was used')
367367
})
368+
369+
it('should support function tasks', async () => {
370+
const staged = ['foo.js']
371+
getStagedFiles.mockImplementationOnce(async () => staged)
372+
373+
const task = jest.fn()
374+
const configObject = { '*.js': { title: 'My task', task } }
375+
376+
searchConfigs.mockImplementationOnce(async () => ({
377+
'': configObject,
378+
}))
379+
380+
await runAll({
381+
configObject: { '*.js': { title: 'My task', task } },
382+
relative: true, // make sure filenames are relative for easier assertion below
383+
})
384+
385+
expect(task).toHaveBeenCalledTimes(1)
386+
expect(task).toHaveBeenCalledWith(staged)
387+
})
368388
})

0 commit comments

Comments
 (0)