-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 running a test function multiple times with different data #2980
Comments
The "forkable test interface" I was looking to experiment with in #2435 would work for this. I don't think we can easily add a trailing |
@novemberborn does AVA more easily support this now? |
Not in the spirit of this issue, no. There's other ways of setting up essentially the same test function for multiple inputs, though. Looking at this again, the problem I see is that AVA requires titles to be unique (it's used as a key within the snapshot file, for instance) and it's not clear how we would execute the same test with the same title for different data. |
Perhaps the title could be suffixed with a hash of the data? |
@tommy-mitchell yes that's not a bad idea, but then the question becomes how we'd reliably hash the data across platforms and Node.js versions, including for functions and whatnot. Unless we restrict the acceptable data types to something we can hash. |
Maybe a v1 of this could require data to be titled? test('moves horizontal', t => {
t.true(move(t.data));
}).data([
['left', -1],
['right', 1],
]); |
Riffing on that, and going back on my earlier comment, perhaps it doesn't usually matter? We could generate titles like We could further extend with a |
#2979 would be good to keep in mind with this too |
I do this kind of stuff all the time: test('detect when the player has won', t => [
{ player: 'scissors', computer: 'paper' },
{ player: 'paper', computer: 'rock' },
{ player: 'rock', computer: 'scissors' }
].forEach(({ player, computer }) =>
t.true(shifumi(player, computer), `with ${player} > ${computer}`)
)) I find this much more straightforward to write and to read than macros, and I put up with the disadvantages. I "wouldn't mind" if the following was possible: test('detect when the player has won', t => {
const { player, computer } = t.data;
t.true(shifumi(player, computer));
}).data(
{ player: 'scissors', computer: 'paper' },
{ player: 'paper', computer: 'rock' },
{ player: 'rock', computer: 'scissors' }
).title(
({ player, computer })=> `with ${player} > ${computer}`
); The main advantages for me:
I am not a fan of the idea of having to maintain a separate array for the data and for the titles, especially if they get long. A title function is convenient and would work around the snapshot problem nicely, because the user can come up with a way to reliably convert its cases to a unique string. |
I'm looking at I understand that the calls to Another way would be something like e.g., FWIW Mocha also has this problem, which I solved with a terrible hack (pass a |
@boneskull once you declare a test or hook, all others need to be declared synchronously. However you're allowed to wait before you do that. Either in a timeout, a promise-chain, or with top-level await. |
You can already do this with macros, but it's a bit verbose and complicated. It would be nice to simplify the common case of just running the same test over an array of fixtures.
Possible API:
The text was updated successfully, but these errors were encountered: