Cleaning up tests. (#34)

This commit is contained in:
Sergio Díaz 2018-06-02 23:47:03 -06:00 committed by GitHub
parent 8e961211a5
commit cc7245b501
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1442 additions and 1401 deletions

View File

@ -4,36 +4,36 @@ const original = require.requireActual('../utils');
const utils = {};
const config = {
environment: {
params: {
name: 'David'
}
},
endpoint: 'https://example.org',
version: 1,
'GET /anything': {
alias: 'anything',
payload: {
name: '$env.params.name'
}
},
'GET /status/418': {
alias: 'teapot'
}
environment: {
params: {
name: 'David'
}
},
endpoint: 'https://example.org',
version: 1,
'GET /anything': {
alias: 'alias',
payload: {
name: '$env.params.name'
}
},
'GET /status/418': {
alias: 'teapot'
}
};
utils.loadConfig = function() {
return new Beau(config, {});
return new Beau(config, {});
};
utils.openConfigFile = function(filename) {
if (filename === 'beau.yml') {
return config;
}
if (filename === 'beau.yml') {
return config;
}
if (filename === 'invalid-conf.yml') {
return { plugins: [{ hello: 1, world: 2 }] };
}
if (filename === 'invalid-conf.yml') {
return { plugins: [{ hello: 1, world: 2 }] };
}
};
utils.baseFlags = original.baseFlags;

View File

@ -1,19 +1,10 @@
// 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`] = `
Array [
" HTTP Verb Alias Endpoint
",
" GET anything https://example.org/anything
" GET alias https://example.org/anything
",
" 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
",
]
`;

View File

@ -1,20 +1,34 @@
// 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 [
"{\\"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 [
"{\\"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 [
"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 [
"",
" Status Endpoint
@ -55,17 +69,3 @@ Array [
",
]
`;
exports[`Request Command with flags: anything 1`] = `
Array [
"",
" Status Endpoint
",
" 200 https://example.org/anything
",
"
",
"\\"{\\"hello\\": \\"world\\"}\\"
",
]
`;

View File

@ -4,34 +4,34 @@ const requestPromiseNativeMock = require('request-promise-native');
jest.mock('../utils');
describe('Request Command', () => {
let result;
let result;
beforeEach(() => {
requestPromiseNativeMock.fail = false;
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});
beforeEach(() => {
requestPromiseNativeMock.fail = false;
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});
afterEach(() => jest.restoreAllMocks());
afterEach(() => jest.restoreAllMocks());
test.each([
['anything'],
['anything', '--verbose'],
['anything', '--as-json'],
['anything', '--as-json', '--verbose'],
['anything', '--no-format'],
['anything', '--quiet']
])('with flags:', async (...args) => {
await RequestCommand.run(args);
expect(result).toMatchSnapshot();
});
test.each([
['alias'],
['alias', '--verbose'],
['alias', '--as-json'],
['alias', '--as-json', '--verbose'],
['alias', '--no-format'],
['alias', '--quiet']
])('with flags: %s %s %s', async (...args) => {
await RequestCommand.run(args);
expect(result).toMatchSnapshot();
});
it('should thrown an error when the request fails', async () => {
requestPromiseNativeMock.fail = true;
await expect(RequestCommand.run(['anything'])).rejects.toThrow(Error);
});
it('should throw an error when the request fails', async () => {
requestPromiseNativeMock.fail = true;
await expect(RequestCommand.run(['anything'])).rejects.toThrow(Error);
});
});

View File

@ -3,28 +3,28 @@ const utils = require('../utils.js');
jest.mock('fs');
describe('utils', () => {
describe('openConfigFile', () => {
it('should read and parse the given configuration file.', () => {
expect(utils.openConfigFile('beau.yml')).toMatchSnapshot();
});
describe('openConfigFile', () => {
it('should read and parse the given configuration file.', () => {
expect(utils.openConfigFile('beau.yml')).toMatchSnapshot();
});
it('should thrown if given not given a file', () => {
expect(() => utils.openConfigFile('not-a-file.yml')).toThrow();
});
});
it('should throw if given not given a file', () => {
expect(() => utils.openConfigFile('not-a-file.yml')).toThrow();
});
});
describe('loadConfig', () => {
it('should load load the config onto Beau', () => {
let beau = utils.loadConfig('beau.yml');
expect(beau.config).toMatchSnapshot();
});
describe('loadConfig', () => {
it('should load load the config onto Beau', () => {
let beau = utils.loadConfig('beau.yml');
expect(beau.config).toMatchSnapshot();
});
it('should load params onto the environment', () => {
let beau = utils.loadConfig('beau.yml', [
'HELLO=WORLD',
'BYE=MARS'
]);
expect(beau.config.ENVIRONMENT).toMatchSnapshot();
});
});
it('should load params onto the environment', () => {
let beau = utils.loadConfig('beau.yml', [
'HELLO=WORLD',
'BYE=MARS'
]);
expect(beau.config.ENVIRONMENT).toMatchSnapshot();
});
});
});

2574
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,26 +13,27 @@
"./src/*"
],
"dependencies": {
"@oclif/command": "^1.4.30",
"@oclif/config": "^1.6.21",
"@oclif/plugin-help": "^2.0.0",
"@oclif/command": "^1.4.32",
"@oclif/config": "^1.6.27",
"@oclif/plugin-help": "^2.0.4",
"@oclif/plugin-warn-if-update-available": "^1.3.9",
"cli-color": "^1.1.0",
"clui": "^0.3.1",
"deepmerge": "^2.1.1",
"dotenv": "^5.0.1",
"dotenv": "^6.0.0",
"globby": "^8.0.1",
"is-plain-object": "^2.0.4",
"joi": "^13.3.0",
"js-yaml": "^3.11.0",
"js-yaml": "^3.12.0",
"jsome": "^2.5.0",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
"requireg": "^0.1.6"
"requireg": "^0.1.8"
},
"repository": "git@github.com:Seich/Beau.git",
"devDependencies": {
"jest": "^23.0.0",
"jest": "^23.1.0",
"jest-watch-typeahead": "^0.1.0",
"strip-ansi": "^4.0.0"
},
"oclif": {
@ -45,12 +46,16 @@
},
"jest": {
"testEnvironment": "node",
"notify": true
"notify": true,
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
]
},
"bin": {
"beau": "./bin/beau"
},
"engines": {
"node": ">=8.9.3"
"node": ">=8.10.0"
}
}

View File

@ -96,35 +96,31 @@ class Request {
this.originalRequest
);
try {
const response = await request(settings);
const response = await request(settings);
let results = {
request: {
headers: response.request.headers,
body: response.request.body,
endpoint: response.request.uri.href
},
response: {
status: response.statusCode,
headers: response.headers,
body: response.body
},
let results = {
request: {
headers: response.request.headers,
body: response.request.body,
endpoint: response.request.uri.href
},
response: {
status: response.statusCode,
headers: response.headers,
body: response.body
};
},
body: response.body
};
results = this.plugins.executeModifier(
'postRequestModifiers',
results,
this.originalRequest
);
results = this.plugins.executeModifier(
'postRequestModifiers',
results,
this.originalRequest
);
cache.add(this.ALIAS, results);
cache.add(this.ALIAS, results);
return results;
} catch ({ error }) {
throw new Error(`Request Error: ` + error);
}
return results;
}
}

View File

@ -28,7 +28,7 @@ class RequestList {
return await request.exec(this.cache);
} catch (reason) {
throw new Error(
`Request: ${request.VERB} ${
`Request ${request.VERB} ${
request.ENDPOINT
} FAILED. \n${reason}`
);