Martin Mazza Dawson Martin Mazza Dawson - 1 month ago 15
Javascript Question

JS Unit testing run multiple times with different parameters

Is their any way to have multiple parameters in one test instead of copying and pasting the function again?

Example in NUnit for C#:

[TestCase("0", 1)]
[TestCase("1", 1)]
[TestCase("2", 1)]
public void UnitTestName(string input, int expected)
{
//Arrange

//Act

//Assert
}


What I want in Js:

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

[TestCase("false")]
[TestCase("true")]
it("option: enableRemoveControls renders remove controls", (enableRemoveControls) => {
mockFoo.enableRemoveControls = enableRemoveControls;

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

Answer

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);
});
Comments