|
1 | 1 | import { describe, it, vi } from 'vitest' |
2 | 2 |
|
| 3 | +import * as figures from '../../lib/figures.js' |
3 | 4 | import { Signal } from '../../lib/getAbortController.js' |
4 | 5 | import { getFunctionTask } from '../../lib/getFunctionTask.js' |
5 | 6 | import { getInitialState } from '../../lib/state.js' |
@@ -31,7 +32,9 @@ describe('getFunctionTask', () => { |
31 | 32 | wrapped[0].task() |
32 | 33 |
|
33 | 34 | expect(command.task).toHaveBeenCalledTimes(1) |
34 | | - expect(command.task).toHaveBeenCalledExactlyOnceWith(['file.js']) |
| 35 | + expect(command.task).toHaveBeenCalledExactlyOnceWith(['file.js'], { |
| 36 | + log: expect.any(Function), |
| 37 | + }) |
35 | 38 | }) |
36 | 39 |
|
37 | 40 | it('should wrap function task failure', async ({ expect }) => { |
@@ -97,4 +100,76 @@ describe('getFunctionTask', () => { |
97 | 100 |
|
98 | 101 | expect(abortController.signal.aborted).toBe(false) |
99 | 102 | }) |
| 103 | + |
| 104 | + it('should collect output from output function on error', async ({ expect }) => { |
| 105 | + const testError = new Error('test error') |
| 106 | + |
| 107 | + const command = { |
| 108 | + title: 'My task', |
| 109 | + task: vi.fn().mockImplementation(async (_, { log }) => { |
| 110 | + log('test output') |
| 111 | + throw testError |
| 112 | + }), |
| 113 | + } |
| 114 | + |
| 115 | + const abortController = new AbortController() |
| 116 | + |
| 117 | + const wrapped = await getFunctionTask({ |
| 118 | + abortController, |
| 119 | + command, |
| 120 | + files: [{ filepath: 'file.js', status: 'M' }], |
| 121 | + }) |
| 122 | + |
| 123 | + const context = getInitialState() |
| 124 | + |
| 125 | + await expect(wrapped[0].task(context)).rejects.toThrow('My task [FAILED]') |
| 126 | + |
| 127 | + expect(context.output).toEqual([ |
| 128 | + expect.stringMatching(`\n${figures.error()} My task:\ntest output\n\n${testError}`), |
| 129 | + ]) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should not collect output from output function when task succeeds', async ({ expect }) => { |
| 133 | + const command = { |
| 134 | + title: 'My task', |
| 135 | + task: vi.fn().mockImplementation(async (_, { log }) => { |
| 136 | + log('test output') |
| 137 | + }), |
| 138 | + } |
| 139 | + |
| 140 | + const wrapped = await getFunctionTask({ |
| 141 | + command, |
| 142 | + files: [{ filepath: 'file.js', status: 'M' }], |
| 143 | + }) |
| 144 | + |
| 145 | + const context = getInitialState() |
| 146 | + |
| 147 | + await wrapped[0].task(context) |
| 148 | + |
| 149 | + expect(context.output).toStrictEqual([]) |
| 150 | + }) |
| 151 | + |
| 152 | + it('should collect output from output function when using --verbose', async ({ expect }) => { |
| 153 | + const command = { |
| 154 | + title: 'My task', |
| 155 | + task: vi.fn().mockImplementation(async (_, { log }) => { |
| 156 | + log('test output') |
| 157 | + }), |
| 158 | + } |
| 159 | + |
| 160 | + const abortController = new AbortController() |
| 161 | + |
| 162 | + const wrapped = await getFunctionTask({ |
| 163 | + abortController, |
| 164 | + command, |
| 165 | + files: [{ filepath: 'file.js', status: 'M' }], |
| 166 | + verbose: true, |
| 167 | + }) |
| 168 | + |
| 169 | + const context = getInitialState() |
| 170 | + |
| 171 | + await wrapped[0].task(context) |
| 172 | + |
| 173 | + expect(context.output).toStrictEqual([`\n${figures.info()} My task:\ntest output`]) |
| 174 | + }) |
100 | 175 | }) |
0 commit comments