Skip to content

Commit 5f2514e

Browse files
test: add tests for parallel error handling
1 parent 6c6cff0 commit 5f2514e

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

plugins/parallel/test/tasks/parallel.test.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { valid } from '@dotcom-tool-kit/validated'
99
import winston, { Logger } from 'winston'
1010

1111
import Parallel from '../../src/tasks/parallel'
12+
import { ToolKitError } from '@dotcom-tool-kit/error'
1213

1314
const importEntryPoint = _importEntryPoint as jest.Mock
1415

@@ -131,21 +132,70 @@ describe('parallel task', () => {
131132
'parallel',
132133
{},
133134
{
134-
tasks: [{ TestOneOffTask: { id: 1 } }, { TestOneOffTask: { id: 2 } }],
135+
tasks: [
136+
{ TestOneOffTask: { id: 1 } },
137+
{ TestOneOffTask: { id: 2 } },
138+
{ TestOneOffTask: { id: 3 } }
139+
],
135140
onError: 'wait-for-others'
136141
},
137142
{ id: '@dotcom-tool-kit/parallel', root: path.resolve(__dirname, '../../') }
138143
)
139144

140145
runMock.mockImplementation(async function (this: TestOneOffTask) {
141-
if (this.options.id === 1) {
142-
throw new Error('error from task 1')
146+
if (this.options.id < 3) {
147+
throw new ToolKitError(`error from task ${this.options.id}`)
143148
}
144149
})
145150

146-
await expect(
147-
task.run({ command: 'test:command', cwd: __dirname, config })
148-
).rejects.toThrowErrorMatchingInlineSnapshot(`"1 error running tasks for test:command"`)
151+
await expect(task.run({ command: 'test:command', cwd: __dirname, config })).rejects
152+
.toMatchInlineSnapshot(`
153+
AggregateError "2 errors running tasks for test:command" {
154+
"errors": [
155+
"TestOneOffTask → ToolKitError" "error from task 1" {
156+
"details": undefined,
157+
},
158+
"TestOneOffTask → ToolKitError" "error from task 2" {
159+
"details": undefined,
160+
},
161+
],
162+
}
163+
`)
164+
})
165+
166+
it('should reject with first error and stop all tasks if one task fails and onError: stop-all', async () => {
167+
const task = new Parallel(
168+
logger,
169+
'parallel',
170+
{},
171+
{
172+
tasks: [
173+
{ TestOneOffTask: { id: 1 } },
174+
{ TestOneOffTask: { id: 2 } },
175+
{ TestOneOffTask: { id: 3 } }
176+
],
177+
onError: 'stop-all'
178+
},
179+
{ id: '@dotcom-tool-kit/parallel', root: path.resolve(__dirname, '../../') }
180+
)
181+
182+
runMock.mockImplementation(async function (this: TestOneOffTask) {
183+
if (this.options.id < 3) {
184+
throw new ToolKitError(`error from task ${this.options.id}`)
185+
}
186+
})
187+
188+
const stopMock = jest.spyOn(TestOneOffTask.prototype, 'stop')
189+
190+
await expect(task.run({ command: 'test:command', cwd: __dirname, config })).rejects
191+
.toMatchInlineSnapshot(`
192+
"ToolKitError" "error from task 1" {
193+
"details": undefined,
194+
}
195+
`)
196+
197+
expect(stopMock).toBeCalledTimes(3)
198+
expect(stopMock.mock.contexts).toEqual(task.taskInstances)
149199
})
150200
})
151201
})

0 commit comments

Comments
 (0)