Add tests for cli
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ethan Lane 2023-10-23 14:04:30 +01:00
parent 9273df6555
commit 565c290e7c
2 changed files with 55 additions and 1 deletions

54
tests/cli.test.ts Normal file
View file

@ -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<cliResult> {
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,
}

View file

@ -1,7 +1,7 @@
import { ErrorCode } from "../src/constants/ErrorCode"; import { ErrorCode } from "../src/constants/ErrorCode";
import ErrorMessages from "../src/constants/ErrorMessages"; import ErrorMessages from "../src/constants/ErrorMessages";
import randomBunny from "../src/index"; import randomBunny from "../src/index";
import fetch, { CancelableRequest } from "got-cjs"; import fetch from "got-cjs";
jest.mock('got-cjs'); jest.mock('got-cjs');
const fetchMock = jest.mocked(fetch); const fetchMock = jest.mocked(fetch);