JS Unit testing run multiple times with different parameters

If you're using Mocha you can combine it with mocha-testdata:

import * as assert from assert;
import { given } from mocha-testdata;

describe('<Foo />', function () {
    given([
        { input: true,  expected: 'some expected value',       description: 'flag enabled' },
        { input: false, expected: 'some other expected value', description: 'flag disabled' },
    ]).
    it('option: enableRemoveControls renders remove controls', function ({ input, expected }) {
        // prepare, act, assert
    });
});

In the above sample you'll also notice a description field that's not being injected into the test. This little trick can be used to make the reported test name more meaningful.

Hope this helps!

Jan


An alternative is to use Jest. It has this functionality built-in:

test.each`
  a    | b    | expected
  ${1} | ${1} | ${2}
  ${1} | ${2} | ${3}
  ${2} | ${1} | ${3}
`('returns $expected when $a is added $b', ({a, b, expected}) => {
  expect(a + b).toBe(expected);
});

You can put the it-call inside a function and call it with different parameters:

describe("<Foo />", () => {

    function run(enableRemoveControls){
        it("option: enableRemoveControls renders remove controls", () =>  {
            mockFoo.enableRemoveControls = enableRemoveControls;

            //Assert that the option has rendered or not rendered the html
        });
    }

    run(false);
    run(true);
});