Added a warning when module version and beau.yml version differ.

This commit is contained in:
David Diaz 2018-06-14 22:22:53 -06:00
parent 7aed5f0085
commit cf56f4b2e4
11 changed files with 82 additions and 48 deletions

View File

@ -1,23 +1,23 @@
const ListCommand = require('../commands/list');
jest.mock('../../../src/shared');
jest.mock('../utils');
describe('List Command', () => {
let result;
let result;
beforeEach(() => {
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});
beforeEach(() => {
result = [];
jest.spyOn(process.stdout, 'write').mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});
afterEach(() => jest.restoreAllMocks());
afterEach(() => jest.restoreAllMocks());
test.each([[], ['--no-format']])('with flags:', async (...args) => {
await ListCommand.run(args);
expect(result).toMatchSnapshot();
});
test.each([[], ['--no-format']])('with flags:', async (...args) => {
await ListCommand.run(args);
expect(result).toMatchSnapshot();
});
});

View File

@ -1,6 +1,8 @@
const RequestCommand = require('../commands/request');
const requestPromiseNativeMock = require('request-promise-native');
jest.mock('../../../src/shared');
jest.mock('../utils');
describe('Request Command', () => {
@ -9,11 +11,9 @@ describe('Request Command', () => {
beforeEach(() => {
requestPromiseNativeMock.fail = false;
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
jest.spyOn(process.stdout, 'write').mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});
afterEach(() => jest.restoreAllMocks());

View File

@ -1,5 +1,7 @@
const utils = require('../utils.js');
jest.mock('../../../src/shared');
jest.mock('fs');
describe('utils', () => {

View File

@ -3,27 +3,25 @@ const ValidateCommand = require('../commands/validate');
jest.mock('../utils');
describe('Validate Command', () => {
let result;
let result;
beforeEach(() => {
result = [];
jest
.spyOn(process.stdout, 'write')
.mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});
beforeEach(() => {
result = [];
jest.spyOn(process.stdout, 'write').mockImplementation(val =>
result.push(require('strip-ansi')(val.toString('utf8')))
);
});
afterEach(() => jest.restoreAllMocks());
afterEach(() => jest.restoreAllMocks());
it('should validate the configuration file', async () => {
await ValidateCommand.run([]);
expect(result).toMatchSnapshot();
});
it('should validate the configuration file', async () => {
await ValidateCommand.run([]);
expect(result).toMatchSnapshot();
});
it('should show schema errors', async () => {
await expect(
ValidateCommand.run(['invalid-conf.yml'])
).rejects.toThrow();
});
it('should show schema errors', async () => {
await expect(
ValidateCommand.run(['invalid-conf.yml'])
).rejects.toThrow();
});
});

4
src/__mocks__/shared.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
...require.requireActual('../shared'),
moduleVersion: jest.fn().mockReturnValue(1)
};

View File

@ -7,7 +7,7 @@ Object {
"body": Object {
"username": "seich",
},
"endpoint": "http://martianwabbit.com/user",
"endpoint": "http://example.com/user",
"headers": Object {
"authentication": "BEARER abc123",
},
@ -25,7 +25,7 @@ Object {
"body": "{\\"hello\\": \\"world\\"}",
"request": Object {
"body": undefined,
"endpoint": "http://martianwabbit.com/user",
"endpoint": "http://example.com/user",
"headers": undefined,
},
"response": Object {

View File

@ -1,11 +1,16 @@
const yaml = require('js-yaml');
const Beau = require('../beau');
const { moduleVersion } = require('../shared');
jest.mock('../shared');
const requireg = require('requireg');
requireg.resolving = false;
describe(`Beau's config Loader.`, () => {
it('should create a request list', () => {
moduleVersion.mockReturnValue(1);
const doc = yaml.safeLoad(`
version: 1
endpoint: 'http://jsonplaceholder.typicode.com'
@ -23,7 +28,21 @@ describe(`Beau's config Loader.`, () => {
const beau = new Beau(doc);
expect(beau.requests).toBeDefined();
expect(beau).toMatchSnapshot();
});
it('should display a warning if the module version and the beau file version are different', () => {
let stdout;
let spy = jest
.spyOn(console, 'warn')
.mockImplementation(val => (stdout = val));
moduleVersion.mockReturnValue(2);
const beau = new Beau({ version: 1 });
expect(stdout).toEqual('This Beau file expects v1. You are using v2.');
spy.mockReset();
spy.mockRestore();
});
});

View File

@ -12,7 +12,7 @@ describe('Request', () => {
beforeEach(() => {
validRequestConfig = {
request: 'POST /user',
endpoint: 'http://martianwabbit.com',
endpoint: 'http://example.com',
alias: 'update',
params: {
userId: '$profile.UserId'
@ -27,7 +27,7 @@ describe('Request', () => {
invalidRequestConfig = {
request: `POST /session`,
endpoint: 'http://martianwabbit.com'
endpoint: 'http://example.com'
};
cache = new RequestCache();
@ -36,7 +36,7 @@ describe('Request', () => {
request = new Request(validRequestConfig);
requestWithoutDependencies = new Request({
endpoint: 'http://martianwabbit.com',
endpoint: 'http://example.com',
request: 'GET /user',
alias: 'show'
});

View File

@ -1,10 +1,19 @@
const RequestList = require('./requestList');
const Config = require('./config');
const { moduleVersion } = require('./shared');
class Beau {
constructor(doc, env = {}) {
this.config = new Config(doc, env);
this.requests = new RequestList(this.config);
if (this.config.VERSION !== moduleVersion()) {
console.warn(
`This Beau file expects v${
this.config.VERSION
}. You are using v${moduleVersion()}.`
);
}
}
}

View File

@ -1,12 +1,11 @@
const deepMerge = require('deepmerge');
const { requestRegex, UpperCaseKeys } = require('./shared');
const { requestRegex, UpperCaseKeys, moduleVersion } = require('./shared');
const Plugins = require('./plugins');
const version = parseInt(require('../package.json').version, 10);
class Config {
constructor(doc, env = {}) {
const defaultConfigValues = {
VERSION: version,
VERSION: moduleVersion(),
ENDPOINT: '',
PLUGINS: [],
DEFAULTS: {},

View File

@ -67,6 +67,8 @@ const replaceInObject = function(obj, fn) {
return obj;
};
const moduleVersion = () => parseInt(require('../package.json').version, 10);
module.exports = {
requestRegex,
replacementRegex,
@ -74,5 +76,6 @@ module.exports = {
UpperCaseKeys,
removeOptionalKeys,
toKebabCase,
replaceInObject
replaceInObject,
moduleVersion
};