-
-
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
RFC: Data driven testing #6082
Comments
I second this! Data driven testing is something I've wanted in JS (especially in Jest) for a while. As you can see I've tried to make it possible with I really like the suggestion of using tagged template literals to try to mimic Spocks data tables API - without the need to do it at a compiler level like in My only concern with using tagged template literals is everything will be a string and the underlying library (possibly Jest) will have to convert them into actual types. I've just had a play with describe('.add', () => {
each`
a | b | expected
${0} | ${0} | ${0}
${0} | ${1} | ${1}
${1} | ${1} | ${2}
`.test('returns $expected when given $a and $b', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
});
↓↓↓↓↓↓↓↓↓↓ I'd be more than happy to build this as part of Jest's core, if approved and once an API is agreed 😃 |
Hm, I dont't think I'm not sold on template strings for that one, as I'd need Prettier to support formatting it :D |
Yeah, didn't think of that. I think it makes sense, if not we'd have to call
That's awesome! @cpojer asked me to write this up, so hopefully he'll chime in with his thoughts on the API.
Yeah, prettier support for the template string would be awesome. It's great to hit format in IntelliJ and have the tables in Spock magically align. Could probably reuse whatever logic Prettier has for tables in markdown |
Yeah, I'm wondering if the initial example with jest-each is enough and whether we should just ship that as part of Jest? I'm not sure what the value of naming the fields in the template literal is when you have to name the variables separately in the callback function. |
@cpojer one nicety of naming the fields in the template String is that we can use the name to interpolate the test title with the value of the field - I agree it can seem a bit verbose to have the names both in the template String and in the callback. |
Starting out with jest importing @mattphillips I think that's a good starting point for a PR 🙂 Should it be |
I think |
I think it would make more sense to be a property on If we put it on
describe('.add', () => {
each([
[0, 0, 0],
[1, 0, 1],
[2, 1, 1]
]).test('returns %s when given %s and %s', (a, b, expected) => {
expect(a + b).toBe(expected);
});
}); If we exposed describe('.add', () => {
test.each([
[0, 0, 0],
[1, 0, 1],
[2, 1, 1]
])('returns %s when given %s and %s', (expected, a, b) => {
expect(a + b).toBe(expected);
});
}); |
The one feature I miss the most from other test frameworks is a nice, clean way of testing multiple inputs and multiple outputs. I first came over this pattern when using Spock, where they call it Data driven testing. It looks basically like this (example taken from link):
This provides a very concise and readable test, perfect for testing pure functions without resorting to looping through values manually.
In Jest, I think the best way would be to implement it similar to how
jest-each
works today:Potentially by parsing a template string, so it's closer to Spock's implementation:
(notice the destructuring matches the names in the headers)
jest-each
allows us to define custom test names, which I think is a good idea. If not, we basically have this:I think the big benefit with supporting it out of the box in Jest is that we can provide static analysis of it, and maybe better messages. We might even be able to hook it into fuzzy testing or other improvements.
It might just boil down to simple sugar around
[].forEach
and a template string, but encouraging the pattern by having built-in support might improve developer's test suite.There is also
babel-plugin-gwt
, but I don't think introducing custom syntax is something we want to do in Jest core. If we do implement this feature though, that plugin can target whatever mechanism we come up with here, for a potentially better interop./cc @mattphillips
The text was updated successfully, but these errors were encountered: