-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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 each array validation check #7033
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
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 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 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 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 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 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 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 not called with an array 1`] = ` | ||
"\`.each\` must be called with an Array or Tagged Template String. | ||
|
||
Instead was called with: undefined | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,12 +23,35 @@ const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g; | |
const PRETTY_PLACEHOLDER = '%p'; | ||
const INDEX_PLACEHOLDER = '%#'; | ||
|
||
const errorWithStack = (message, callsite) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could possibly stick this in jest-util or something, we do it all over the place There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I'll make that refactor in a separate PR |
||
const error = new Error(message); | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(error, callsite); | ||
} | ||
return error; | ||
}; | ||
|
||
export default (cb: Function, supportsDone: boolean = true) => (...args: any) => | ||
function eachBind(title: string, test: Function, timeout: number): void { | ||
if (args.length === 1) { | ||
const table: Table = args[0].every(Array.isArray) | ||
? args[0] | ||
: args[0].map(entry => [entry]); | ||
const [tableArg] = args; | ||
|
||
if (!Array.isArray(tableArg)) { | ||
const error = errorWithStack( | ||
'`.each` must be called with an Array or Tagged Template String.\n\n' + | ||
`Instead was called with: ${pretty(tableArg, { | ||
maxDepth: 1, | ||
min: true, | ||
})}\n`, | ||
eachBind, | ||
); | ||
return cb(title, () => { | ||
throw error; | ||
}); | ||
} | ||
const table: Table = tableArg.every(Array.isArray) | ||
? tableArg | ||
: tableArg.map(entry => [entry]); | ||
return table.forEach((row, i) => | ||
cb( | ||
arrayFormat(title, i, ...row), | ||
|
@@ -47,7 +70,7 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => | |
const missingData = data.length % keys.length; | ||
|
||
if (missingData > 0) { | ||
const error = new Error( | ||
const error = errorWithStack( | ||
'Not enough arguments supplied for given headings:\n' + | ||
EXPECTED_COLOR(keys.join(' | ')) + | ||
'\n\n' + | ||
|
@@ -58,12 +81,9 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => | |
'argument', | ||
missingData, | ||
)}`, | ||
eachBind, | ||
); | ||
|
||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(error, eachBind); | ||
} | ||
|
||
return cb(title, () => { | ||
throw error; | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we have a "was called with" thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just added it :)