From 0e9661d4a3ba3b656ff4b2f2204a821a73cacee4 Mon Sep 17 00:00:00 2001 From: Naoki Oketani Date: Sat, 14 Dec 2019 12:56:45 +0900 Subject: [PATCH] Add unit test for error handling (#30) --- __tests__/audit.test.ts | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/__tests__/audit.test.ts b/__tests__/audit.test.ts index 320a476..9af3020 100644 --- a/__tests__/audit.test.ts +++ b/__tests__/audit.test.ts @@ -54,4 +54,61 @@ describe('run', () => { audit.run() expect(audit.foundVulnerability()).toBeFalsy() }) + + test('throws an error if error is not null', async () => { + mocked(child_process).spawnSync.mockImplementation((): any => { + return { + pid: 100, + output: '', + stdout: '', + stderr: '', + status: 0, + signal: null, + error: new Error('Something is wrong') + } + }) + + expect.assertions(1) + const r = audit.run() + const e = new Error('Something is wrong') + await expect(r).rejects.toEqual(e) + }) + + test('throws an error if status is null', async () => { + mocked(child_process).spawnSync.mockImplementation((): any => { + return { + pid: 100, + output: '', + stdout: '', + stderr: '', + status: null, + signal: 'SIGTERM', + error: null + } + }) + + expect.assertions(1) + const r = audit.run() + const e = new Error('the subprocess terminated due to a signal.') + await expect(r).rejects.toEqual(e) + }) + + test('throws an error if stderr is null', async () => { + mocked(child_process).spawnSync.mockImplementation((): any => { + return { + pid: 100, + output: '', + stdout: '', + stderr: 'Something is wrong', + status: 1, + signal: null, + error: null + } + }) + + expect.assertions(1) + const r = audit.run() + const e = new Error('Something is wrong') + await expect(r).rejects.toEqual(e) + }) })