Added tests for all CLI commands.

This commit is contained in:
David Diaz 2018-05-22 20:14:57 -06:00
parent 6e4a77527a
commit 9444b9ca61
8 changed files with 168 additions and 8 deletions

View File

@ -9,7 +9,7 @@ const config = {
name: 'David' name: 'David'
} }
}, },
endpoint: 'https://httpbin.org/', endpoint: 'https://example.org',
version: 1, version: 1,
'GET /anything': { 'GET /anything': {
alias: 'anything', alias: 'anything',
@ -26,6 +26,10 @@ utils.loadConfig = function() {
return new Beau(config, {}); return new Beau(config, {});
}; };
utils.openConfigFile = function() {
return config;
};
utils.baseFlags = original.baseFlags; utils.baseFlags = original.baseFlags;
module.exports = utils; module.exports = utils;

View File

@ -2,9 +2,9 @@
exports[`List Command Should disable formatting when the flag is active. 1`] = ` exports[`List Command Should disable formatting when the flag is active. 1`] = `
Array [ Array [
"GET anything https://httpbin.org/anything "GET anything https://example.org/anything
", ",
"GET teapot https://httpbin.org/status/418 "GET teapot https://example.org/status/418
", ",
] ]
`; `;
@ -13,9 +13,9 @@ exports[`List Command Should list available requests for a given file. 1`] = `
Array [ Array [
" HTTP Verb Alias Endpoint " HTTP Verb Alias Endpoint
", ",
" GET anything https://httpbin.org/anything " GET anything https://example.org/anything
", ",
" GET teapot https://httpbin.org/status/418 " GET teapot https://example.org/status/418
", ",
" "
", ",

View File

@ -0,0 +1,71 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Request Command Should output an unformatted version 1`] = `
Array [
"200
",
"https://example.org/anything
",
"[]
",
"\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"
",
]
`;
exports[`Request Command Should output nothing 1`] = `Array []`;
exports[`Request Command Should output the response as json 1`] = `
Array [
"{\\"status\\":200,\\"headers\\":[],\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"}
",
]
`;
exports[`Request Command Should output the response as json verboselly 1`] = `
Array [
"{\\"request\\":{\\"body\\":{\\"name\\":\\"David\\"},\\"endpoint\\":\\"https://example.org/anything\\"},\\"response\\":{\\"status\\":200,\\"headers\\":[],\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"},\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"}
",
]
`;
exports[`Request Command Should request the given alias 1`] = `
Array [
"",
" Status Endpoint
",
" 200 https://example.org/anything
",
"
",
"\\"{\\"hello\\": \\"world\\"}\\"
",
]
`;
exports[`Request Command Should show all information available when being verbose 1`] = `
Array [
"",
" Status Endpoint
",
" 200 https://example.org/anything
",
"
",
"{
request: {
body: {
name: \\"David\\"
},
endpoint: \\"https://example.org/anything\\"
},
response: {
status: 200,
headers: [],
body: \\"{\\"hello\\": \\"world\\"}\\"
},
body: \\"{\\"hello\\": \\"world\\"}\\"
}
",
]
`;

View File

@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Validate Command Validate the configuration file 1`] = `
Array [
"beau.yml is valid.
",
]
`;

View File

@ -1,5 +1,4 @@
const ListCommand = require('../commands/list'); const ListCommand = require('../commands/list');
const stripAnsi = require('strip-ansi');
jest.mock('../utils'); jest.mock('../utils');
@ -11,7 +10,7 @@ describe('List Command', () => {
jest jest
.spyOn(process.stdout, 'write') .spyOn(process.stdout, 'write')
.mockImplementation(val => .mockImplementation(val =>
result.push(stripAnsi(val.toString('utf8'))) result.push(require('strip-ansi')(val.toString('utf8')))
); );
}); });

View File

@ -0,0 +1,55 @@
const RequestCommand = require('../commands/request');
const requestPromiseNativeMock = require('request-promise-native');
jest.mock('../utils');
describe('Request Command', () => {
let result;
beforeEach(() => {
requestPromiseNativeMock.fail = false;
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});
afterEach(() => jest.restoreAllMocks());
it('Should request the given alias', async () => {
await RequestCommand.run(['anything']);
expect(result).toMatchSnapshot();
});
it('Should show all information available when being verbose', async () => {
await RequestCommand.run(['anything', '--verbose']);
expect(result).toMatchSnapshot();
});
it('Should output the response as json', async () => {
await RequestCommand.run(['anything', '--as-json']);
expect(result).toMatchSnapshot();
});
it('Should output the response as json verboselly', async () => {
await RequestCommand.run(['anything', '--as-json', '--verbose']);
expect(result).toMatchSnapshot();
});
it('Should output an unformatted version', async () => {
await RequestCommand.run(['anything', '--no-format']);
expect(result).toMatchSnapshot();
});
it('Should output nothing', async () => {
await RequestCommand.run(['anything', '--quiet']);
expect(result).toMatchSnapshot();
});
it('should thrown an error when the request fails', async () => {
requestPromiseNativeMock.fail = true;
await expect(RequestCommand.run(['anything'])).rejects.toThrow(Error);
});
});

View File

@ -0,0 +1,23 @@
const ValidateCommand = require('../commands/validate');
jest.mock('../utils');
describe('Validate Command', () => {
let result;
beforeEach(() => {
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});
afterEach(() => jest.restoreAllMocks());
it('Validate the configuration file', async () => {
await ValidateCommand.run([]);
expect(result).toMatchSnapshot();
});
});

View File

@ -28,7 +28,7 @@ class RequestCommand extends Command {
new Line().output(); new Line().output();
jsome((verbose ? res : body) || null); this.log(jsome.getColoredString((verbose ? res : body) || null));
} }
async run() { async run() {