mirror of https://github.com/Seich/Beau.git
Cleaning up tests. (#34)
This commit is contained in:
parent
8e961211a5
commit
cc7245b501
|
|
@ -4,36 +4,36 @@ const original = require.requireActual('../utils');
|
||||||
const utils = {};
|
const utils = {};
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
environment: {
|
environment: {
|
||||||
params: {
|
params: {
|
||||||
name: 'David'
|
name: 'David'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
endpoint: 'https://example.org',
|
endpoint: 'https://example.org',
|
||||||
version: 1,
|
version: 1,
|
||||||
'GET /anything': {
|
'GET /anything': {
|
||||||
alias: 'anything',
|
alias: 'alias',
|
||||||
payload: {
|
payload: {
|
||||||
name: '$env.params.name'
|
name: '$env.params.name'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'GET /status/418': {
|
'GET /status/418': {
|
||||||
alias: 'teapot'
|
alias: 'teapot'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
utils.loadConfig = function() {
|
utils.loadConfig = function() {
|
||||||
return new Beau(config, {});
|
return new Beau(config, {});
|
||||||
};
|
};
|
||||||
|
|
||||||
utils.openConfigFile = function(filename) {
|
utils.openConfigFile = function(filename) {
|
||||||
if (filename === 'beau.yml') {
|
if (filename === 'beau.yml') {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filename === 'invalid-conf.yml') {
|
if (filename === 'invalid-conf.yml') {
|
||||||
return { plugins: [{ hello: 1, world: 2 }] };
|
return { plugins: [{ hello: 1, world: 2 }] };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
utils.baseFlags = original.baseFlags;
|
utils.baseFlags = original.baseFlags;
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,10 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
exports[`List Command with flags: --no-format 1`] = `
|
|
||||||
Array [
|
|
||||||
"GET anything https://example.org/anything
|
|
||||||
",
|
|
||||||
"GET teapot https://example.org/status/418
|
|
||||||
",
|
|
||||||
]
|
|
||||||
`;
|
|
||||||
|
|
||||||
exports[`List Command with flags: 1`] = `
|
exports[`List Command with flags: 1`] = `
|
||||||
Array [
|
Array [
|
||||||
" HTTP Verb Alias Endpoint
|
" HTTP Verb Alias Endpoint
|
||||||
",
|
",
|
||||||
" GET anything https://example.org/anything
|
" GET alias https://example.org/anything
|
||||||
",
|
",
|
||||||
" GET teapot https://example.org/status/418
|
" GET teapot https://example.org/status/418
|
||||||
",
|
",
|
||||||
|
|
@ -21,3 +12,12 @@ Array [
|
||||||
",
|
",
|
||||||
]
|
]
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`List Command with flags: 2`] = `
|
||||||
|
Array [
|
||||||
|
"GET alias https://example.org/anything
|
||||||
|
",
|
||||||
|
"GET teapot https://example.org/status/418
|
||||||
|
",
|
||||||
|
]
|
||||||
|
`;
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,34 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
exports[`Request Command with flags: anything --as-json --verbose 1`] = `
|
exports[`Request Command with flags: alias %s %s 1`] = `
|
||||||
Array [
|
Array [
|
||||||
"{\\"request\\":{\\"body\\":{\\"name\\":\\"David\\"},\\"endpoint\\":\\"https://example.org/anything\\"},\\"response\\":{\\"status\\":200,\\"headers\\":[],\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"},\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"}
|
"",
|
||||||
|
" Status Endpoint
|
||||||
|
",
|
||||||
|
" 200 https://example.org/anything
|
||||||
|
",
|
||||||
|
"
|
||||||
|
",
|
||||||
|
"\\"{\\"hello\\": \\"world\\"}\\"
|
||||||
",
|
",
|
||||||
]
|
]
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`Request Command with flags: anything --as-json 1`] = `
|
exports[`Request Command with flags: alias --as-json %s 1`] = `
|
||||||
Array [
|
Array [
|
||||||
"{\\"status\\":200,\\"headers\\":[],\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"}
|
"{\\"status\\":200,\\"headers\\":[],\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"}
|
||||||
",
|
",
|
||||||
]
|
]
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`Request Command with flags: anything --no-format 1`] = `
|
exports[`Request Command with flags: alias --as-json --verbose 1`] = `
|
||||||
|
Array [
|
||||||
|
"{\\"request\\":{\\"body\\":{\\"name\\":\\"David\\"},\\"endpoint\\":\\"https://example.org/anything\\"},\\"response\\":{\\"status\\":200,\\"headers\\":[],\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"},\\"body\\":\\"{\\\\\\"hello\\\\\\": \\\\\\"world\\\\\\"}\\"}
|
||||||
|
",
|
||||||
|
]
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`Request Command with flags: alias --no-format %s 1`] = `
|
||||||
Array [
|
Array [
|
||||||
"200
|
"200
|
||||||
",
|
",
|
||||||
|
|
@ -27,9 +41,9 @@ Array [
|
||||||
]
|
]
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`Request Command with flags: anything --quiet 1`] = `Array []`;
|
exports[`Request Command with flags: alias --quiet %s 1`] = `Array []`;
|
||||||
|
|
||||||
exports[`Request Command with flags: anything --verbose 1`] = `
|
exports[`Request Command with flags: alias --verbose %s 1`] = `
|
||||||
Array [
|
Array [
|
||||||
"",
|
"",
|
||||||
" Status Endpoint
|
" Status Endpoint
|
||||||
|
|
@ -55,17 +69,3 @@ Array [
|
||||||
",
|
",
|
||||||
]
|
]
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`Request Command with flags: anything 1`] = `
|
|
||||||
Array [
|
|
||||||
"",
|
|
||||||
" Status Endpoint
|
|
||||||
",
|
|
||||||
" 200 https://example.org/anything
|
|
||||||
",
|
|
||||||
"
|
|
||||||
",
|
|
||||||
"\\"{\\"hello\\": \\"world\\"}\\"
|
|
||||||
",
|
|
||||||
]
|
|
||||||
`;
|
|
||||||
|
|
|
||||||
|
|
@ -4,34 +4,34 @@ const requestPromiseNativeMock = require('request-promise-native');
|
||||||
jest.mock('../utils');
|
jest.mock('../utils');
|
||||||
|
|
||||||
describe('Request Command', () => {
|
describe('Request Command', () => {
|
||||||
let result;
|
let result;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
requestPromiseNativeMock.fail = false;
|
requestPromiseNativeMock.fail = false;
|
||||||
result = [];
|
result = [];
|
||||||
jest
|
jest
|
||||||
.spyOn(process.stdout, 'write')
|
.spyOn(process.stdout, 'write')
|
||||||
.mockImplementation(val =>
|
.mockImplementation(val =>
|
||||||
result.push(require('strip-ansi')(val.toString('utf8')))
|
result.push(require('strip-ansi')(val.toString('utf8')))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => jest.restoreAllMocks());
|
afterEach(() => jest.restoreAllMocks());
|
||||||
|
|
||||||
test.each([
|
test.each([
|
||||||
['anything'],
|
['alias'],
|
||||||
['anything', '--verbose'],
|
['alias', '--verbose'],
|
||||||
['anything', '--as-json'],
|
['alias', '--as-json'],
|
||||||
['anything', '--as-json', '--verbose'],
|
['alias', '--as-json', '--verbose'],
|
||||||
['anything', '--no-format'],
|
['alias', '--no-format'],
|
||||||
['anything', '--quiet']
|
['alias', '--quiet']
|
||||||
])('with flags:', async (...args) => {
|
])('with flags: %s %s %s', async (...args) => {
|
||||||
await RequestCommand.run(args);
|
await RequestCommand.run(args);
|
||||||
expect(result).toMatchSnapshot();
|
expect(result).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should thrown an error when the request fails', async () => {
|
it('should throw an error when the request fails', async () => {
|
||||||
requestPromiseNativeMock.fail = true;
|
requestPromiseNativeMock.fail = true;
|
||||||
await expect(RequestCommand.run(['anything'])).rejects.toThrow(Error);
|
await expect(RequestCommand.run(['anything'])).rejects.toThrow(Error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -3,28 +3,28 @@ const utils = require('../utils.js');
|
||||||
jest.mock('fs');
|
jest.mock('fs');
|
||||||
|
|
||||||
describe('utils', () => {
|
describe('utils', () => {
|
||||||
describe('openConfigFile', () => {
|
describe('openConfigFile', () => {
|
||||||
it('should read and parse the given configuration file.', () => {
|
it('should read and parse the given configuration file.', () => {
|
||||||
expect(utils.openConfigFile('beau.yml')).toMatchSnapshot();
|
expect(utils.openConfigFile('beau.yml')).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should thrown if given not given a file', () => {
|
it('should throw if given not given a file', () => {
|
||||||
expect(() => utils.openConfigFile('not-a-file.yml')).toThrow();
|
expect(() => utils.openConfigFile('not-a-file.yml')).toThrow();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('loadConfig', () => {
|
describe('loadConfig', () => {
|
||||||
it('should load load the config onto Beau', () => {
|
it('should load load the config onto Beau', () => {
|
||||||
let beau = utils.loadConfig('beau.yml');
|
let beau = utils.loadConfig('beau.yml');
|
||||||
expect(beau.config).toMatchSnapshot();
|
expect(beau.config).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load params onto the environment', () => {
|
it('should load params onto the environment', () => {
|
||||||
let beau = utils.loadConfig('beau.yml', [
|
let beau = utils.loadConfig('beau.yml', [
|
||||||
'HELLO=WORLD',
|
'HELLO=WORLD',
|
||||||
'BYE=MARS'
|
'BYE=MARS'
|
||||||
]);
|
]);
|
||||||
expect(beau.config.ENVIRONMENT).toMatchSnapshot();
|
expect(beau.config.ENVIRONMENT).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
23
package.json
23
package.json
|
|
@ -13,26 +13,27 @@
|
||||||
"./src/*"
|
"./src/*"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@oclif/command": "^1.4.30",
|
"@oclif/command": "^1.4.32",
|
||||||
"@oclif/config": "^1.6.21",
|
"@oclif/config": "^1.6.27",
|
||||||
"@oclif/plugin-help": "^2.0.0",
|
"@oclif/plugin-help": "^2.0.4",
|
||||||
"@oclif/plugin-warn-if-update-available": "^1.3.9",
|
"@oclif/plugin-warn-if-update-available": "^1.3.9",
|
||||||
"cli-color": "^1.1.0",
|
"cli-color": "^1.1.0",
|
||||||
"clui": "^0.3.1",
|
"clui": "^0.3.1",
|
||||||
"deepmerge": "^2.1.1",
|
"deepmerge": "^2.1.1",
|
||||||
"dotenv": "^5.0.1",
|
"dotenv": "^6.0.0",
|
||||||
"globby": "^8.0.1",
|
"globby": "^8.0.1",
|
||||||
"is-plain-object": "^2.0.4",
|
"is-plain-object": "^2.0.4",
|
||||||
"joi": "^13.3.0",
|
"joi": "^13.3.0",
|
||||||
"js-yaml": "^3.11.0",
|
"js-yaml": "^3.12.0",
|
||||||
"jsome": "^2.5.0",
|
"jsome": "^2.5.0",
|
||||||
"request": "^2.87.0",
|
"request": "^2.87.0",
|
||||||
"request-promise-native": "^1.0.5",
|
"request-promise-native": "^1.0.5",
|
||||||
"requireg": "^0.1.6"
|
"requireg": "^0.1.8"
|
||||||
},
|
},
|
||||||
"repository": "git@github.com:Seich/Beau.git",
|
"repository": "git@github.com:Seich/Beau.git",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"jest": "^23.0.0",
|
"jest": "^23.1.0",
|
||||||
|
"jest-watch-typeahead": "^0.1.0",
|
||||||
"strip-ansi": "^4.0.0"
|
"strip-ansi": "^4.0.0"
|
||||||
},
|
},
|
||||||
"oclif": {
|
"oclif": {
|
||||||
|
|
@ -45,12 +46,16 @@
|
||||||
},
|
},
|
||||||
"jest": {
|
"jest": {
|
||||||
"testEnvironment": "node",
|
"testEnvironment": "node",
|
||||||
"notify": true
|
"notify": true,
|
||||||
|
"watchPlugins": [
|
||||||
|
"jest-watch-typeahead/filename",
|
||||||
|
"jest-watch-typeahead/testname"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"beau": "./bin/beau"
|
"beau": "./bin/beau"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=8.9.3"
|
"node": ">=8.10.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -96,35 +96,31 @@ class Request {
|
||||||
this.originalRequest
|
this.originalRequest
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
const response = await request(settings);
|
||||||
const response = await request(settings);
|
|
||||||
|
|
||||||
let results = {
|
let results = {
|
||||||
request: {
|
request: {
|
||||||
headers: response.request.headers,
|
headers: response.request.headers,
|
||||||
body: response.request.body,
|
body: response.request.body,
|
||||||
endpoint: response.request.uri.href
|
endpoint: response.request.uri.href
|
||||||
},
|
},
|
||||||
response: {
|
response: {
|
||||||
status: response.statusCode,
|
status: response.statusCode,
|
||||||
headers: response.headers,
|
headers: response.headers,
|
||||||
body: response.body
|
|
||||||
},
|
|
||||||
body: response.body
|
body: response.body
|
||||||
};
|
},
|
||||||
|
body: response.body
|
||||||
|
};
|
||||||
|
|
||||||
results = this.plugins.executeModifier(
|
results = this.plugins.executeModifier(
|
||||||
'postRequestModifiers',
|
'postRequestModifiers',
|
||||||
results,
|
results,
|
||||||
this.originalRequest
|
this.originalRequest
|
||||||
);
|
);
|
||||||
|
|
||||||
cache.add(this.ALIAS, results);
|
cache.add(this.ALIAS, results);
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
} catch ({ error }) {
|
|
||||||
throw new Error(`Request Error: ` + error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ class RequestList {
|
||||||
return await request.exec(this.cache);
|
return await request.exec(this.cache);
|
||||||
} catch (reason) {
|
} catch (reason) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Request: ${request.VERB} ${
|
`Request ${request.VERB} ${
|
||||||
request.ENDPOINT
|
request.ENDPOINT
|
||||||
} FAILED. \n${reason}`
|
} FAILED. \n${reason}`
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue