diff --git a/lib/resolveTaskFn.js b/lib/resolveTaskFn.js index 9a60e8350..03cdea39e 100644 --- a/lib/resolveTaskFn.js +++ b/lib/resolveTaskFn.js @@ -71,23 +71,25 @@ const killExecaProcess = async (execaProcess) => { * * @param {Object} ctx * @param {execa.ExecaChildProcess} execaChildProcess - * @returns {() => void} Function that clears the interval that + * @returns {() => Promise} Function that clears the interval that * checks the context. */ const interruptExecutionOnError = (ctx, execaChildProcess) => { - let loopIntervalId + let intervalId, killPromise const loop = async () => { if (ctx.errors.size > 0) { - clearInterval(loopIntervalId) - await killExecaProcess(execaChildProcess) + clearInterval(intervalId) + killPromise = killExecaProcess(execaChildProcess) + await killPromise } } - loopIntervalId = setInterval(loop, ERROR_CHECK_INTERVAL) + intervalId = setInterval(loop, ERROR_CHECK_INTERVAL) - return () => { - clearInterval(loopIntervalId) + return async () => { + clearInterval(intervalId) + await killPromise } } @@ -155,7 +157,7 @@ export const resolveTaskFn = ({ const quitInterruptCheck = interruptExecutionOnError(ctx, execaChildProcess) const result = await execaChildProcess - quitInterruptCheck() + await quitInterruptCheck() if (result.failed || result.killed || result.signal != null) { throw makeErr(command, result, ctx) diff --git a/test/resolveTaskFn.spec.js b/test/resolveTaskFn.spec.js index c5bbb3368..d5d1d6906 100644 --- a/test/resolveTaskFn.spec.js +++ b/test/resolveTaskFn.spec.js @@ -333,6 +333,31 @@ describe('resolveTaskFn', () => { `) }) + it('should not kill long running tasks without errors in context', async () => { + execa.mockImplementationOnce(() => + createExecaReturnValue( + { + stdout: 'a-ok', + stderr: '', + code: 0, + cmd: 'mock cmd', + failed: false, + killed: false, + signal: null, + }, + 1000 + ) + ) + + const context = getInitialState() + const taskFn = resolveTaskFn({ command: 'node' }) + const taskPromise = taskFn(context) + + jest.runOnlyPendingTimers() + + await expect(taskPromise).resolves.toEqual() + }) + it('should kill a long running task when an error is added to the context', async () => { execa.mockImplementationOnce(() => createExecaReturnValue(