Jest: Multiple test cases per test

Created on 30 Dec 2016  路  7Comments  路  Source: facebook/jest

Do you want to request a feature or report a bug?
Feature

What is the expected behavior?
Add a way to set up multiple test cases for a single test (Similiar to the TestCaseAttribute in NUnit).

given the function:

var plus = function sum(a, b) {
  return a + b;
};

Example 1:

it('Should add the numbers together', [
  TestCase(1, 2, 3),
  TestCase(-1, -1, -2),
  TestCase(-1, 1, 0)
], function (a, b, expectedResult) {
  // act
  var result = sum(a, b);

  // assert
  expect(result).toEqual(expectedResult);
});

Example 2:

given([
  TestCase(1, 2, 3),
  TestCase(-1, -1, -2),
  TestCase(-1, 1, 0)
]).it('Should add the numbers together', function (a, b, expectedResult) {
  // act
  var result = sum(a, b);

  // assert
  expect(result).toEqual(expectedResult);
});

The examples above are just two different examples of how I imagine the feature could be.

New API proposal

Most helpful comment

That's great @jlandure!

As an update: Jest 23 will provide this feature out of the box! Check out jest@beta and the docs here

We provide:

  • test.each
  • it.each
  • describe.each

For all of the fit, xdescribe, .only, .skip, etc variants

All 7 comments

[TestCase1, TestCase2].forEach(case => test(case.name, case.fn))

I don't think it is necessary to add an API for this specifically. You can build it yourself based on the above construct.

A good alternative @SteinarArnason 馃憠 https://github.com/atlassian/jest-in-case

That's great @jlandure!

As an update: Jest 23 will provide this feature out of the box! Check out jest@beta and the docs here

We provide:

  • test.each
  • it.each
  • describe.each

For all of the fit, xdescribe, .only, .skip, etc variants

@rickhanlonii - Hey, I was trying to test it in the example repl, but am told that .each is not a function. In further investigation, I logged the test and it objects/functions and it really isn't there. Is the repl running v23?

Nope, repl is on Jest v22.1.2 and node v7.4.0

Hi, I testing on Jest 23.2 and .each is not a function. In docs: http://jestjs.io/docs/en/api#testeachtable-name-fn each method exist for 23 API.

@kklimczak interesting, can you file a new issue with the bug template and a reproduction repo?

Was this page helpful?
0 / 5 - 0 ratings