Jest: Tests using datasets

Created on 15 Nov 2017  路  6Comments  路  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?
Possible feature

What is the current behavior?
I have an array containing arguments and the expected result that I use to check if a function is working properly. When one of the data rows fails, the expect shows the diff for the value being compared.

What is the expected behavior?
I need a way to print the entire data row when the assertion fails. I know I can extend the expect function, but is there a more generic approach? For example, phpunit has data providers..

Most helpful comment

@borela this sounds like parameterised testing where by you supply data to the same test with different input/expected. Check out jest-each.

Example:

each([
  [1, 1, 2],
  [1, 2, 3],
  [2, 1, 3],
]).test('returns the result of adding %s to %s', (a, b, expected) => {
  expect(add(a, b)).toBe(expected);
});

All 6 comments

Hello,

Thank you for posting here. I don't think this issue has sufficient information to allow us to consider how to integrate this feature and whether it is a good fit for Jest. Could you go into more detail of what API you'd like to see and how you'd like errors to be printed, ideally with a real example? Thank you

@borela this sounds like parameterised testing where by you supply data to the same test with different input/expected. Check out jest-each.

Example:

each([
  [1, 1, 2],
  [1, 2, 3],
  [2, 1, 3],
]).test('returns the result of adding %s to %s', (a, b, expected) => {
  expect(add(a, b)).toBe(expected);
});

@mattphillips Thank you!

I really like Ava's macros.

https://github.com/avajs/ava/blob/4124d77b4391401db0b92a73c84cdea8326c209a/readme.md#test-macros

jest-each solves basically the same thing, though 馃檪

I still can't find how to make parameterised tests when each test case is not so succinct to add it to a static table in the code (e.g. it is a JSON object).

jest-each is part of jest core
This works in a similar way to phpunit data providers and should do what you need
https://jestjs.io/docs/en/api.html#testeachtablename-fn-timeout

Was this page helpful?
0 / 5 - 0 ratings

Related issues

calebmer picture calebmer  路  72Comments

eldh picture eldh  路  84Comments

vitalibozhko picture vitalibozhko  路  138Comments

snapwich picture snapwich  路  77Comments

TYRONEMICHAEL picture TYRONEMICHAEL  路  80Comments