diff --git a/package.json b/package.json index d79b166c..934be6ba 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "contributors": [ "Jack Cross ", "Nathan Force ", - "Jason Schapiro" + "Jason Schapiro", + "Jacob Franklin " ], "scripts": { "postinstall": "lerna bootstrap", diff --git a/packages/parrot-core/__tests__/utils/matchMock.spec.js b/packages/parrot-core/__tests__/utils/matchMock.spec.js index 89d5a7f3..3889390a 100644 --- a/packages/parrot-core/__tests__/utils/matchMock.spec.js +++ b/packages/parrot-core/__tests__/utils/matchMock.spec.js @@ -48,4 +48,18 @@ describe('matchMock', () => { const req = { path: '/squawk', headers: 'ahoy', 'Keep-Alive': 'timeout=5' }; expect(matchMock(req, {}, mocks)).toEqual(mocks[0]); }); + + it('throws when mocks is not an array', () => { + const mocks = {}; + expect(() => matchMock({}, {}, mocks)).toThrow( + 'mocks is not an array as expected. What was passed: [object Object]' + ); + }); + + it('throws when mocks is an empty array', () => { + const mocks = []; + expect(() => matchMock({}, {}, mocks)).toThrow( + 'mocks is empty, and likely none are defined for the current scenario.' + ); + }); }); diff --git a/packages/parrot-core/src/utils/matchMock.js b/packages/parrot-core/src/utils/matchMock.js index 0bf73108..2f426478 100644 --- a/packages/parrot-core/src/utils/matchMock.js +++ b/packages/parrot-core/src/utils/matchMock.js @@ -35,6 +35,15 @@ function match(normalizedRequest) { export default function matchMock(normalizedRequest, platformRequest, mocks) { let matchedMock; + + if (!Array.isArray(mocks)) { + throw new TypeError(`mocks is not an array as expected. What was passed: ${mocks}`); + } + + if (mocks.length === 0) { + throw new TypeError('mocks is empty, and likely none are defined for the current scenario.'); + } + for (let index = 0; index < mocks.length; index += 1) { const mock = mocks[index]; if (typeof mock === 'function') {