Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

length check in match mock #130

Merged
merged 6 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"contributors": [
"Jack Cross <[email protected]>",
"Nathan Force <[email protected]>",
"Jason Schapiro"
"Jason Schapiro",
"Jacob Franklin <[email protected]>"
],
"scripts": {
"postinstall": "lerna bootstrap",
Expand Down
14 changes: 14 additions & 0 deletions packages/parrot-core/__tests__/utils/matchMock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
});
});
9 changes: 9 additions & 0 deletions packages/parrot-core/src/utils/matchMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down