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

Support shallow arrays in jest-each #6348

Closed
captbaritone opened this issue May 29, 2018 · 6 comments · Fixed by #6351
Closed

Support shallow arrays in jest-each #6348

captbaritone opened this issue May 29, 2018 · 6 comments · Fixed by #6351

Comments

@captbaritone
Copy link
Contributor

captbaritone commented May 29, 2018

I expected I could write this:

test.each(['red', 'green', 'bean'])("The word %s contains the letter 'e'", word => {
  expect(/e/.test(word)).toBe(true);
});

But it lead to very confusing test titles and test failures:

screen shot 2018-05-29 at 2 50 19 pm

I ended up doing something like this, and it worked.

test.each(['red', 'green', 'bean'].map(word => [word]))(
  "The word %s contains the letter 'e'",
  word => {
    expect(/e/.test(word)).toBe(true);
  },
);

At the very least, it would be nice to error if the values in the table are not arrays.

@mattphillips

@mattphillips
Copy link
Contributor

Hey @captbaritone the array needs to represent a table:

test.each([
  [arg1, arg2, arg3], // row1
  [arg1, arg2, arg3], // row2
])

We could add a check here https://github.com/facebook/jest/blob/master/packages/jest-each/src/bind.js#L24 to make sure the inner row array is actually an array. What do you think?

@captbaritone
Copy link
Contributor Author

Yeah, I figured that out eventually, and it makes sense. I would say we should check if it's an array and either:

  1. Throw a helpful error.
  2. Convert it to an array of length one automatically.

@mattphillips
Copy link
Contributor

I'm not torn either way, in fact I think I'm leaning towards options 2.

@SimenB @thymikee what do you guys think?

@captbaritone
Copy link
Contributor Author

captbaritone commented May 29, 2018

I think option two is best, since I would imagine it's pretty common to want to make the same assertion about an array of values, and having to manually convert the array to a table is kinda gross.

@mattphillips
Copy link
Contributor

Cool I'll send a PR :)

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants