diff --git a/bin/cli/__tests__/list.spec.js b/bin/cli/__tests__/list.spec.js index 3227839..367aa3a 100644 --- a/bin/cli/__tests__/list.spec.js +++ b/bin/cli/__tests__/list.spec.js @@ -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(); + }); }); diff --git a/bin/cli/__tests__/request.spec.js b/bin/cli/__tests__/request.spec.js index ab3132a..b09e9a2 100644 --- a/bin/cli/__tests__/request.spec.js +++ b/bin/cli/__tests__/request.spec.js @@ -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()); diff --git a/bin/cli/__tests__/utils.spec.js b/bin/cli/__tests__/utils.spec.js index d4830a0..69877c0 100644 --- a/bin/cli/__tests__/utils.spec.js +++ b/bin/cli/__tests__/utils.spec.js @@ -1,5 +1,7 @@ const utils = require('../utils.js'); +jest.mock('../../../src/shared'); + jest.mock('fs'); describe('utils', () => { diff --git a/bin/cli/__tests__/validate.spec.js b/bin/cli/__tests__/validate.spec.js index c563130..8f99821 100644 --- a/bin/cli/__tests__/validate.spec.js +++ b/bin/cli/__tests__/validate.spec.js @@ -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(); + }); }); diff --git a/src/__mocks__/shared.js b/src/__mocks__/shared.js new file mode 100644 index 0000000..26f2ff5 --- /dev/null +++ b/src/__mocks__/shared.js @@ -0,0 +1,4 @@ +module.exports = { + ...require.requireActual('../shared'), + moduleVersion: jest.fn().mockReturnValue(1) +}; diff --git a/src/__tests__/__snapshots__/request.spec.js.snap b/src/__tests__/__snapshots__/request.spec.js.snap index 4f7c00b..ab94f3a 100644 --- a/src/__tests__/__snapshots__/request.spec.js.snap +++ b/src/__tests__/__snapshots__/request.spec.js.snap @@ -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 { diff --git a/src/__tests__/beau.spec.js b/src/__tests__/beau.spec.js index 9f7bbcf..eb94e8a 100644 --- a/src/__tests__/beau.spec.js +++ b/src/__tests__/beau.spec.js @@ -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(); + }); }); diff --git a/src/__tests__/request.spec.js b/src/__tests__/request.spec.js index 4c111aa..edb0098 100644 --- a/src/__tests__/request.spec.js +++ b/src/__tests__/request.spec.js @@ -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' }); diff --git a/src/beau.js b/src/beau.js index f734940..9b3f2fb 100644 --- a/src/beau.js +++ b/src/beau.js @@ -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()}.` + ); + } } } diff --git a/src/config.js b/src/config.js index 5f93cd0..3af7b5a 100644 --- a/src/config.js +++ b/src/config.js @@ -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: {}, diff --git a/src/shared.js b/src/shared.js index 4b0404d..2964c40 100644 --- a/src/shared.js +++ b/src/shared.js @@ -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 };