From 565c290e7cf2f891dec0517b9da9196ed071c71e Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 23 Oct 2023 14:04:30 +0100 Subject: [PATCH] Add tests for cli --- tests/cli.test.ts | 54 +++++++++++++++++++++++++++++++++++++++++++++ tests/index.test.ts | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/cli.test.ts diff --git a/tests/cli.test.ts b/tests/cli.test.ts new file mode 100644 index 0000000..efa1b8d --- /dev/null +++ b/tests/cli.test.ts @@ -0,0 +1,54 @@ +import { exec } from "child_process"; +import path from "path"; + +describe('version', () => { + test('GIVEN -V flag is supplied, EXPECT version returned', async () => { + const result = await cli(['-V'], '.'); + + expect(result.code).toBe(0); + expect(result.stdout).toBe('2.2\n'); + }); + + test('GIVEN --version is supplied, EXPECT version returned', async () => { + const result = await cli(['--version'], '.'); + + expect(result.code).toBe(0); + expect(result.stdout).toBe('2.2\n'); + }); +}); + +describe('help', () => { + test('GIVEN -h is supplied, EXPECT help returned', async () => { + const result = await cli(['-h'], '.'); + + expect(result.code).toBe(0); + expect(result.stdout.split('\n')[0]).toBe('Usage: random-bunny [options]'); + }); + + test('GIVEN --help is supplied, EXPECT help returned', async () => { + const result = await cli(['--help'], '.'); + + expect(result.code).toBe(0); + expect(result.stdout.split('\n')[0]).toBe('Usage: random-bunny [options]'); + }); +}) + +function cli(args: string[], cwd: string): Promise { + return new Promise(resolve => { + exec(`node ${path.resolve('./dist/cli.js')} ${args.join(' ')}`, + { cwd }, + (error, stdout, stderr) => { resolve({ + code: error && error.code ? error.code : 0, + error, + stdout, + stderr }); + }); + }); +} + +interface cliResult { + code: number, + error: any, + stdout: string, + stderr: string, +} \ No newline at end of file diff --git a/tests/index.test.ts b/tests/index.test.ts index 2fb1a14..302fd1d 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,7 +1,7 @@ import { ErrorCode } from "../src/constants/ErrorCode"; import ErrorMessages from "../src/constants/ErrorMessages"; import randomBunny from "../src/index"; -import fetch, { CancelableRequest } from "got-cjs"; +import fetch from "got-cjs"; jest.mock('got-cjs'); const fetchMock = jest.mocked(fetch);