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

[jest-each]: Add empty string/array validation check #7249

Merged
merged 5 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

### Fixes

- `[jest-each]` Add empty array validation check ([#7249](https://github.com/facebook/jest/pull/7249))
- `[jest-cli]` Interrupt tests if interactive watch plugin key is pressed ([#7222](https://github.com/facebook/jest/pull/7222))
- `[jest-cli]` Fix coverage summary reporting ([#7058](https://github.com/facebook/jest/pull/7058))
- `[jest-each]` Add each array validation check ([#7033](https://github.com/facebook/jest/pull/7033))
Expand Down
40 changes: 40 additions & 0 deletions packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,54 +1,94 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jest-each .describe throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty array of table data.
"
`;

exports[`jest-each .describe throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template String.

Instead was called with: undefined
"
`;

exports[`jest-each .describe.only throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty array of table data.
"
`;

exports[`jest-each .describe.only throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template String.

Instead was called with: undefined
"
`;

exports[`jest-each .fdescribe throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty array of table data.
"
`;

exports[`jest-each .fdescribe throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template String.

Instead was called with: undefined
"
`;

exports[`jest-each .fit throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty array of table data.
"
`;

exports[`jest-each .fit throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template String.

Instead was called with: undefined
"
`;

exports[`jest-each .it throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty array of table data.
"
`;

exports[`jest-each .it throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template String.

Instead was called with: undefined
"
`;

exports[`jest-each .it.only throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty array of table data.
"
`;

exports[`jest-each .it.only throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template String.

Instead was called with: undefined
"
`;

exports[`jest-each .test throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty array of table data.
"
`;

exports[`jest-each .test throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template String.

Instead was called with: undefined
"
`;

exports[`jest-each .test.only throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty array of table data.
"
`;

exports[`jest-each .test.only throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template String.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jest-each .describe throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty tagged template string of table data.
"
`;

exports[`jest-each .describe throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Expand Down Expand Up @@ -29,6 +34,11 @@ Received:
Missing <red>2</> arguments"
`;

exports[`jest-each .describe.only throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty tagged template string of table data.
"
`;

exports[`jest-each .describe.only throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Expand Down Expand Up @@ -58,6 +68,11 @@ Received:
Missing <red>2</> arguments"
`;

exports[`jest-each .fdescribe throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty tagged template string of table data.
"
`;

exports[`jest-each .fdescribe throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Expand Down Expand Up @@ -87,6 +102,11 @@ Received:
Missing <red>2</> arguments"
`;

exports[`jest-each .fit throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty tagged template string of table data.
"
`;

exports[`jest-each .fit throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Expand Down Expand Up @@ -116,6 +136,11 @@ Received:
Missing <red>2</> arguments"
`;

exports[`jest-each .it throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty tagged template string of table data.
"
`;

exports[`jest-each .it throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Expand Down Expand Up @@ -145,6 +170,11 @@ Received:
Missing <red>2</> arguments"
`;

exports[`jest-each .it.only throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty tagged template string of table data.
"
`;

exports[`jest-each .it.only throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Expand Down Expand Up @@ -174,6 +204,11 @@ Received:
Missing <red>2</> arguments"
`;

exports[`jest-each .test throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty tagged template string of table data.
"
`;

exports[`jest-each .test throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Expand Down Expand Up @@ -203,6 +238,11 @@ Received:
Missing <red>2</> arguments"
`;

exports[`jest-each .test.only throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty tagged template string of table data.
"
`;

exports[`jest-each .test.only throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-each/src/__tests__/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ describe('jest-each', () => {
).toThrowErrorMatchingSnapshot();
});

test('throws an error when called with an empty array', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)([]);
const testFunction = get(eachObject, keyPath);

testFunction('expected string', noop);
const globalMock = get(globalTestMocks, keyPath);

expect(() =>
globalMock.mock.calls[0][1](),
).toThrowErrorMatchingSnapshot();
});

test('calls global with given title', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)([[]]);
Expand Down
15 changes: 15 additions & 0 deletions packages/jest-each/src/__tests__/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ describe('jest-each', () => {
expect(testCallBack).not.toHaveBeenCalled();
});

test('throws an error when called with an empty string', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)` `;
const testFunction = get(eachObject, keyPath);
const testCallBack = jest.fn();
testFunction('this will blow up :(', testCallBack);

const globalMock = get(globalTestMocks, keyPath);

expect(() =>
globalMock.mock.calls[0][1](),
).toThrowErrorMatchingSnapshot();
expect(testCallBack).not.toHaveBeenCalled();
});

test('calls global with given title', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)`
Expand Down
24 changes: 24 additions & 0 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) =>
throw error;
});
}

if (
tableArg.length === 1 &&
typeof tableArg[0] === 'string' &&
tableArg[0].trim() === ''
) {
const error = new ErrorWithStack(
'Error: `.each` called with an empty tagged template string of table data.\n',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's called "tagged template literal"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good spot!

eachBind,
);
return cb(title, () => {
throw error;
});
}

if (tableArg.length === 0) {
const error = new ErrorWithStack(
'Error: `.each` called with an empty array of table data.\n',
eachBind,
);
return cb(title, () => {
throw error;
});
}
const table: Table = tableArg.every(Array.isArray)
? tableArg
: tableArg.map(entry => [entry]);
Expand Down