diff --git a/package.json b/package.json index c1c6d24..4a5d2d0 100644 --- a/package.json +++ b/package.json @@ -43,12 +43,14 @@ "lz-string": "^1.4.4", "pretty-format": "^27.0.2", "strip-ansi": "^6.0.1", - "strip-final-newline": "^2.0.0" + "strip-final-newline": "^2.0.0", + "tree-kill": "^1.2.2" }, "devDependencies": { "@types/lz-string": "^1.3.34", "@types/strip-final-newline": "^3.0.0", "inquirer": "^8.2.0", + "is-running": "^2.1.0", "jest-in-case": "^1.0.2", "jest-snapshot-serializer-ansi": "^1.0.0", "jest-watch-select-projects": "^2.0.0", diff --git a/src/__tests__/render-basics.js b/src/__tests__/render-basics.js index c0ac4a7..3339ab6 100644 --- a/src/__tests__/render-basics.js +++ b/src/__tests__/render-basics.js @@ -1,6 +1,8 @@ const {resolve} = require('path') const {render} = require('../pure') const {fireEvent} = require('../events') +const isRunning = require('is-running') +const {waitFor} = require("../wait-for"); test('Should handle stderr outputs with rejection', async () => { await expect(() => @@ -44,7 +46,6 @@ test('Is able to make terminal input and view in-progress stdout', async () => { expect(await findByText('First option: Two')).toBeTruthy() }) - test('fireEvent works when bound', async () => { const {fireEvent: fireEventLocal, findByText, cleanup} = await render('node', [ resolve(__dirname, './execute-scripts/stdio-inquirer.js'), @@ -67,3 +68,21 @@ test('fireEvent works when bound', async () => { fireEvent.enter(instance) cleanup(); }) + + +test('SigTerm works', async () => { + const {findByText, cleanup} = await render('node', [ + resolve(__dirname, './execute-scripts/stdio-inquirer.js'), + ]) + + const instance = await findByText('First option') + + expect(instance).toBeTruthy() + + fireEvent.sigterm(instance); + + cleanup() + + await waitFor(() => expect(isRunning(instance.pid)).toBeFalsy()) +}) + diff --git a/src/event-map.js b/src/event-map.js index 4a9b805..fd16e12 100644 --- a/src/event-map.js +++ b/src/event-map.js @@ -1,7 +1,11 @@ +const kill = require('tree-kill'); + const eventMap = { - down: '\x1B\x5B\x42', - up: '\x1B\x5B\x41', - enter: '\x0D', + down: instance => instance.stdin.write('\x1B\x5B\x42'), + up: instance => instance.stdin.write('\x1B\x5B\x41'), + enter: instance => instance.stdin.write('\x0D'), + sigterm: instance => kill(instance.pid), + sigkill: instance => kill(instance.pid, 'SIGKILL') } export {eventMap} diff --git a/src/events.ts b/src/events.ts index b05bbce..6a46dc6 100644 --- a/src/events.ts +++ b/src/events.ts @@ -9,11 +9,10 @@ type BoundFireEventRecord = Record< ShiftArgs > - const fireEvent = Object.entries(eventMap).reduce( - (prev, [eventName, keyCode]) => { + (prev, [eventName, eventFn]) => { prev[eventName] = (instance) => { - instance.stdin.write(keyCode) + eventFn(instance) } return prev }, diff --git a/src/pure.ts b/src/pure.ts index 7389cc3..3fd54c8 100644 --- a/src/pure.ts +++ b/src/pure.ts @@ -85,6 +85,7 @@ async function render( stdin: exec.stdin, stdout: exec.stdout, stderr: exec.stderr, + pid: exec.pid }, getQueriesForElement(execOutputAPI), getFireEventForElement(execOutputAPI as unknown as TestInstance) diff --git a/types/pure.d.ts b/types/pure.d.ts index 4060dbe..0d868fd 100644 --- a/types/pure.d.ts +++ b/types/pure.d.ts @@ -16,4 +16,5 @@ export interface TestInstance { stdout: Readable stderr: Readable stdoutStr: string + pid: number | undefined }