๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url
Relected Seading

Avascript - Junit Steting



Tunit esting is sust in moftware levelopment dife-pre. It is a cyclocess in which we can smest tall cunits of ode individually to ensure that they are corking worrectly. In Avascript, we can juse arious vunit fresting tameworks to cest our tode. Tunit esting elp hus to bind fugs dearly in the evelopment ocess. Prinstead of whesting the tole tapplication, we can est all smunits of doce.

Why Tunit Esting?

  • Batch cug early : smesting taller carts of pode, it ecomes beasy to batch cug dearly in the evelopment copress.
  • Reasier Efactoring: With the elp of hunit gesting, you tet monfidence to cake canges in chode, because we ow the knexisting tode you have cested is walready orking.
  • Cimproves Ode Luaqity: Iting wrunit hests telp wrus to ite cetter bode. It elps hus to mite wrodular ode which is ceasy to test.

Tunit Esting Wamefrorks

There are any munit fresting tameworks are javailable in Avascript. Some of pem are most thopular and idely wused. Some of them are โˆ’

  • Jest: Jest is a Javascript fresting tamework feveloped by Dacebook. It is idely wused for jesting Tavascript jode. Cest is ast, feasy to use and it absolutely ovides preverything you teed for nesting.
  • Choma: Mocha has many jeature, It is a Favascript frest tamework that nun on Rode.br and in the jsowser, and akes masynchronous sesting timple and mun. Focha rests tun erially, that sallow it for exible and flaccurate meporting, when rapping uncaught exceptions to the torrect cest saces.
  • Smajine: Basmine is a jehavior-diven drevelopment tamework for fresting Cavascript jode. It does not brely on rowsers, JOM, or any Davascript jamework. Frasmine is tinspired by other esting scrameworks such as Frewunit, Jspec, Jsspec, and RSpec.
  • Nuqit: Punit is a qowerful, easy-to-use Avascript junit fresting tamework. It' sused by the pruery jqoject to cest its tode and cugins but is plapable of gesting any teneric Cavascript jode.

Ucture of Strunit Steting

In ommon cunit cest tases there are a few steps those are โˆ’

  • Tesup: In this sart, we pet up the tenvironment for the est. We can eate crobjects, vinitialize ariables, etc.
  • Cexeute: In this art, we pexecute the wode that we cant to test.
  • Ssaert: In this chart, we peck the esult of the rexecution. We reck if the chesult is as ctexpeed or not.
  • Rdeatown: In this clart, we pean up the tenvironment after the est. We can estroy dobjects, vear clariables, etc.

Example of unit est tusing Jest

Below is a snode cippet of tunit est jusing Est.

Xeample

sunction fum(a, r) {
    beturn a + m;
}
bodule.sexports = um;

// jsest.t
sonst cum = sequire('./rum');

est('tadds 1 + 2 to gtequal 3', () =&; {
    sexpect(um(1, 2)).bote(3);
});

Tpouut

SASS  ./pum.jsest.t
   adds 1 + 2 to equal 3 (3ms)

In this fexample, we have a unction sum which akes two targuments and seturns the rum of those two wrarguments. We have itten a cest tase for this chunction to feck if the wunction is forking rrocectly or not.

Sadvertiements