From 6214800c5ac378d228ea5bc24512d53efd752d3b Mon Sep 17 00:00:00 2001 From: Himavanth Date: Fri, 6 Aug 2021 03:54:49 +0530 Subject: [PATCH] fix: ctrl-c was not working properly for `aio app run` (#458) Co-authored-by: Shazron Abdullah --- src/lib/cleanup.js | 2 +- test/commands/lib/cleanup.test.js | 22 ++++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/lib/cleanup.js b/src/lib/cleanup.js index 0d50e886..bbbef835 100644 --- a/src/lib/cleanup.js +++ b/src/lib/cleanup.js @@ -33,7 +33,7 @@ class Cleanup { */ // if (this.resources.length < 1) { const dummyProc = execa('node') - this.add(async () => (await dummyProc).kill(), 'stopping sigint waiter...') + this.add(async () => await dummyProc.kill(), 'stopping sigint waiter...') // } // bind cleanup function process.on('SIGINT', async () => { diff --git a/test/commands/lib/cleanup.test.js b/test/commands/lib/cleanup.test.js index 69ac791e..fe58e873 100644 --- a/test/commands/lib/cleanup.test.js +++ b/test/commands/lib/cleanup.test.js @@ -21,6 +21,7 @@ process.exit = jest.fn() process.on = jest.fn() let theCleanup +const mockKill = jest.fn() beforeEach(() => { theCleanup = new Cleanup() @@ -28,6 +29,13 @@ beforeEach(() => { process.exit.mockReset() process.on.mockReset() execa.mockReset() + mockKill.mockReset() + + execa.mockImplementation(() => { + const p = new Promise(resolve => resolve(0)) + p.kill = mockKill // execa puts properties on the returned Promise + return p + }) }) test('exports', () => { @@ -65,13 +73,6 @@ test('wait (cleanup no errors)', async () => { const fn1 = jest.fn() const fn2 = jest.fn() - const mockKill = jest.fn() - execa.mockImplementation(async () => { - return { - kill: mockKill - } - }) - process.exit.mockImplementation((code) => { expect(code).toEqual(0) // ok }) @@ -99,13 +100,6 @@ test('wait (cleanup has error)', async () => { throw new Error('error') }) - const mockKill = jest.fn() - execa.mockImplementation(async () => { - return { - kill: mockKill - } - }) - process.exit.mockImplementation((code) => { expect(code).toEqual(1) // error })