|
1 | 1 | import execa from 'execa'
|
| 2 | +import pidTree from 'pidtree' |
2 | 3 |
|
3 | 4 | import { resolveTaskFn } from '../lib/resolveTaskFn'
|
4 | 5 | import { getInitialState } from '../lib/state'
|
5 | 6 | import { TaskError } from '../lib/symbols'
|
6 | 7 |
|
7 | 8 | import { createExecaReturnValue } from './utils/createExecaReturnValue'
|
8 | 9 |
|
| 10 | +jest.useFakeTimers() |
| 11 | + |
| 12 | +jest.mock('pidtree', () => jest.fn(async () => [])) |
| 13 | + |
9 | 14 | const defaultOpts = { files: ['test.js'] }
|
10 | 15 |
|
11 |
| -function mockExecaImplementationOnce(value) { |
| 16 | +const mockExecaImplementationOnce = (value) => { |
12 | 17 | execa.mockImplementationOnce(() => createExecaReturnValue(value))
|
13 | 18 | }
|
14 | 19 |
|
@@ -350,6 +355,52 @@ describe('resolveTaskFn', () => {
|
350 | 355 |
|
351 | 356 | context.errors.add({})
|
352 | 357 |
|
| 358 | + jest.runAllTimers() |
| 359 | + |
353 | 360 | await expect(taskPromise).rejects.toThrowErrorMatchingInlineSnapshot(`"node [KILLED]"`)
|
354 | 361 | })
|
| 362 | + |
| 363 | + it('should also kill child processes of killed execa processes', async () => { |
| 364 | + expect.assertions(3) |
| 365 | + |
| 366 | + execa.mockImplementationOnce(() => |
| 367 | + createExecaReturnValue( |
| 368 | + { |
| 369 | + stdout: 'a-ok', |
| 370 | + stderr: '', |
| 371 | + code: 0, |
| 372 | + cmd: 'mock cmd', |
| 373 | + failed: false, |
| 374 | + killed: false, |
| 375 | + signal: null, |
| 376 | + }, |
| 377 | + 1000 |
| 378 | + ) |
| 379 | + ) |
| 380 | + |
| 381 | + const realKill = process.kill |
| 382 | + const mockKill = jest.fn() |
| 383 | + Object.defineProperty(process, 'kill', { |
| 384 | + value: mockKill, |
| 385 | + }) |
| 386 | + |
| 387 | + pidTree.mockImplementationOnce(() => ['1234']) |
| 388 | + |
| 389 | + const taskFn = resolveTaskFn({ command: 'node' }) |
| 390 | + |
| 391 | + const context = getInitialState() |
| 392 | + const taskPromise = taskFn(context) |
| 393 | + |
| 394 | + context.errors.add({}) |
| 395 | + jest.runAllTimers() |
| 396 | + |
| 397 | + await expect(taskPromise).rejects.toThrowErrorMatchingInlineSnapshot(`"node [KILLED]"`) |
| 398 | + |
| 399 | + expect(mockKill).toHaveBeenCalledTimes(1) |
| 400 | + expect(mockKill).toHaveBeenCalledWith('1234') |
| 401 | + |
| 402 | + Object.defineProperty(process, 'kill', { |
| 403 | + value: realKill, |
| 404 | + }) |
| 405 | + }) |
355 | 406 | })
|
0 commit comments