Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Unit testing run multiple times with different parameters

Tags:

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     }); }); 
like image 716
Martin Dawson Avatar asked Oct 26 '16 22:10

Martin Dawson


People also ask

Which annotation makes it run a test multiple times with different parameters?

You need to add the annotation @RunWith(Parameterized.

Can unit tests depend on each other?

Tests should never depend on each other. If your tests have to be run in a specific order, then you need to change your tests. Instead, you should make proper use of the Setup and TearDown features of your unit-testing framework to ensure each test is ready to run individually.

How unit test is done in JavaScript?

JavaScript Unit Testing is a method where JavaScript test code is written for a web page or web application module. It is then combined with HTML as an inline event handler and executed in the browser to test if all functionalities are working as desired. These unit tests are then organized in the test suite.


1 Answers

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); }); 
like image 155
Asen Arizanov Avatar answered Oct 04 '22 15:10

Asen Arizanov