jasmine / jasmine.github.io

Source for Jasmine's documentation
https://jasmine.github.io
MIT License
567 stars 418 forks source link

Specifically address parameterized/data-driven testing with Jasmine #170

Open jneuhaus20 opened 5 days ago

jneuhaus20 commented 5 days ago

I don't know the best place to add this, but since it's a touted feature in many frameworks, I feel like explicit guidance on parameterized/data-driven testing would be beneficial. Or if it exists, it's not discoverable.

There are several closed issues indicating it's available in (or one would need to create a) plugin, but this is misleading and gives the impression that it's not natively supported. (See https://github.com/jasmine/jasmine/issues/900, https://github.com/jasmine/jasmine/issues/741 (and PR https://github.com/jasmine/jasmine/pull/527), and https://github.com/jasmine/jasmine/issues/480.

However, by virtue of language capabilities and how jasmine works at runtime, this is actually 100% natively supported. Credit to https://github.com/jasmine/jasmine/issues/900#issuecomment-2267646850:

const cases = [
  {first: 3, second: 3, sum: 6},
  {first: 10, second: 4, sum: 14},
  {first: 7, second: 1, sum: 8}
];

for (const {first, second, sum} of cases) {
  it(`${first} plus ${second} is ${sum}`, function() {
    expect(first+second).toEqual(sum);
  });
}

Or even:

const integerCases = {
  positive: [
    {first: 3, second: 3, sum: 6},
    {first: 10, second: 4, sum: 14},
    {first: 7, second: 1, sum: 8}
  ],
  negative: [
    {first: -3, second: -3, sum: -6},
    {first: -10, second: -4, sum: -14},
    {first: -7, second: -1, sum: -8}
  ]
};

Object.entries(integerCases ).forEach(([type, cases]) => {
  describe(`when adding ${type} integers`, () => {
      cases.forEach(({first, second, sum}) => {
        it(`${first} plus ${second} is ${sum}`, function() {
          expect(first + second).toEqual(sum);
        });
      });
  });
});

The appropriate area of the docs should be amended to include this technique, in whatever style/manner best aligns with the existing examples. That way people don't think this common best practice is prohibitively difficult or unsupported, and look to a different framework instead.