Back to Question Center
0

Testar Ɗauki Na Jagorarka Ta Amfani da Mocha da Chai            Ƙunin Test Your JavaScript Yin amfani da Mocha da ChaiRelated Topics: AngularJSES6Raw Tsare

1 answers:
Na'urar Ɗauki Na Jagoranka Ta Amfani da Mocha da Chai

Wannan labarin da aka bincika ɗan littafin ta Panayiotis «pvgr» Velisarakos, Mark Brown da Tom Greco. Na gode wa dukan masu nazari na Semalt don yin Semalt abun da zai iya zama!

Shin kun taba yin wasu canje-canje zuwa lambarku, sa'annan daga bisani kuka sami shi ya haifar da wani abu dabam?

Tabbatar da yawancin mu. Wannan shi ne kusan wanda ba zai yiwu ba, musamman lokacin da kake da yawan lambar - oculos dior real. Abu daya ya dogara da wani, sa'annan ya canza shi ya karya wani abu a sakamakon.

Amma idan hakan bai faru ba? Mene ne idan kana da hanyar sanin lokacin da wani abu ya rabu saboda sakamakon wani? Wannan zai zama kyakkyawa. Kuna iya canza lambarku ba tare da damu da karya wani abu ba, kuna da ƙananan kwari kuma kuna kashe kuɗi kaɗan.

Wancan shine wurin gwagwarmaya guda ɗaya. Za su ta atomatik gano duk wani matsala a cikin lambar a gare ku. Yi canje-canje, gudanar da gwaje-gwaje kuma idan wani abu ya rabu, zaku gane abin da ya faru, nan da nan matsala da wane irin hali ya kamata. Wannan ya ƙare duk wani zanewa!

A cikin wannan labarin, zan nuna muku yadda za a fara sashi gwajin ku JavaScript. Misalai da dabaru da aka nuna a cikin wannan labarin za a iya amfani da su da maɓallin tushen bincike da kuma lambar Semalt.

Lambar don wannan koyawa na samuwa daga Semalt repo.

Menene Gwajin Wuta

Idan ka jarraba shafinka, ka ɗauki wani code - yawanci aiki - kuma tabbatar da shi behaves daidai a cikin wani halin da ake ciki. Binciken naúrar tsari ne wanda aka tsara da kuma yadda ya dace. A sakamakon haka, ƙarin gwaje-gwaje da ka rubuta, mafi girman amfanin da ka karɓa. Za ku kuma sami tabbacin amincewar ku yayin da kuka ci gaba da bunkasa shi.

Mahimman tunani tare da gwaje-tsaren naúrar ita ce gwada halin halayen lokacin da ya ba shi wasu sauti. Kuna kira aiki tare da wasu sigogi, kuma duba ku sami sakamako mai kyau.

     // Bada 1 da 10 a matsayin bayanai var sakamakon = Math. max (1, 10);// ya kamata mu sami 10 a matsayin fitarwaidan (sakamakon! == 10) {jefa sabon kuskure ("Ba a yi nasara ba");}    

A aikace, gwaje-gwaje na iya zama lokacin ƙwarewa. Alal misali, idan aikinka ya buƙaci Ajax buƙatar, jarrabawar ta buƙatar wasu ƙayyadaddun saiti, amma wannan ka'ida ta "ba da wasu bayanai, muna tsammanin wani sakamako na musamman" har yanzu ya shafi.

Samar da kayan aikin

Ga wannan labarin, zamu yi amfani da Mocha. Yana da sauki a fara tare da, za'a iya amfani dashi don gwajin gwaji da gwaje-tsaren Semalt, kuma yana taka rawa da wasu kayan aikin gwadawa.

Hanyar mafi sauki ta shigar Mocha ta hanyar npm (wanda muke buƙatar shigar da Node js). Idan ba ku da tabbacin yadda za a shigar da ko dai nop ko Node a kan tsarin ku, tuntube mu koyarwa: Jagora Mai Farawa don Npm - Mai Neman Package Manager

Tare da Node shigar, bude wani tasiri ko layin umarni a tarihin aikin ku.

  • Idan kana so ka gwada lambar a cikin mai bincike, gudu npm shigar da mocha chai --save-dev
  • Idan kuna so ku gwada Node. js code, baya ga sama, gudu npm shigar -g mocha

Wannan yana shigar da kunshe Mocha da chai . Mocha ne ɗakin ɗakin karatu wanda yake ba mu damar yin gwaje-gwaje, kuma Chai yana da wasu ayyuka masu taimako waɗanda za mu yi amfani da su don tabbatar da sakamakon gwajinmu.

Gwaji a kan Kulle. js vs Gwaji a cikin Bincike

An tsara misalai da suka biyo don aiki idan suna gudanar da gwaje-gwaje a cikin mai bincike. Idan kuna so ku gwada gwajin ku na aikace-aikacen Semalt, bi wadannan matakai.

  • Don Node, ba ku buƙatar fayil din mai jarrabawar gwajin.
  • Gyara gwaje-gwaje ta yin amfani da umurnin Mocha , maimakon bude wani mai bincike.

Samar da Tsarin Gida

Ya kamata ka shigar da gwaje-gwajenka a cikin rabuwa na raba daga fayiloli na manyan fayiloli. Wannan ya sa ya fi sauƙi don tsara su, misali idan kana so ka ƙara ƙarin gwaje-gwajen daban-daban a nan gaba (kamar gwaje-gwajen hadewa ko gwaje-gwajen aiki).

Mafi shahararren aikin tare da Javascript shine a yi amfani da shugabanci mai suna gwaji / a cikin aikinka na farfadowa. Bayan haka, kowane fayil din gwaji an sanya shi a karkashin gwaji / wasuModuleTest. js . A zahiri, zaka iya amfani da kundayen adireshi a cikin gwajin / , amma ina bada shawarar ajiye abubuwa mai sauƙi - zaka iya canza shi daga baya idan ya cancanta.

Samar da Jirgin gwaji

Domin mu gwada gwaje-gwajenmu a cikin mai bincike, muna buƙatar kafa wani shafi mai sauƙi na HTML don zama manajan gwajin . Rubutun shafi na Mocha, ɗakin karatu da jarrabawar gwajin mu. Don tafiyar da gwaje-gwaje, za mu bude dan gudu a cikin mai bincike.

Idan kuna amfani da Node. js, zaka iya tsallake wannan mataki. Node. js gwaje-gwajen gwaje-gwaje za a iya sarrafawa ta yin amfani da umurnin Mocha , ɗauka cewa kun bi tsarin jagorancin da aka tsara.

A ƙasa ne lambar da za mu yi amfani da shi don mai jarrabawar gwajin. Zan ajiye wannan fayil a matsayin testrunner. html .

    Mocha gwaji </ title><link rel = "stylesheet" href = "node_modules / mocha / mocha. css"></ head><jiki><div id = "Mocha">  </div> <script src = "node_modules / mocha / mocha. js"> </ script><script src = "node_modules / chai / chai. js"> </ script><script> mocha. saitin ('bdd') </ script><! - cajin lambar da kake son gwadawa a nan -><! - load fayilolin gwaji a nan -><rubutun>Mocha. gudu  <span class="f-c-white l-mr3"> ;</ script></ body></ html> </code>   </pre>  <p>  Babban mahimmanci a cikin mai gwajin gwaji shine:  </p>  <ul>  <li>  Mun kalli tsarin Mocha ta CSS don ba da gwajinmu yadda ya kamata.  </li>  <li>  Muna ƙirƙirar raba tare da ID  <code>  Mocha  </code> . Wannan shi ne inda aka saka sakon gwajin.  </li>  <li>  Mu kaya Mocha da Chai. Ana cikin su a cikin fayiloli mataimaka na babban fayil  <code>  node_modules  </code>  tun lokacin da muka sanya su ta hanyar npm.  </li>  <li>  Ta hanyar kiran  <code>  Mocha. saiti  </code> , muna sa masu taimakon gwaji na Mocha suna samuwa.  </li>  <li>  Bayan haka, muna ɗaukar lambar da muke son gwadawa da fayilolin gwaji. Ba mu da wani abu a nan har yanzu.  </li>  <li>  Last, muna kira  <code>  Mocha. gudu  </code>  don gudanar da gwaje-gwajen. Tabbatar cewa kuna kiran wannan  <em>  bayan  </em>  akan haɗin tushen da kuma gwada fayiloli.  </li>  </ul> <h2 id="the-basic-test-building-blocks"> Gidajen Ginin Ginin Ginawa  </h2>  <p>  Yanzu da za mu iya tafiyar gwaje-gwaje, bari mu fara rubuta wasu.  </p>  <p>  Za mu fara da ƙirƙirar sabon fayil  <code>  gwajin / tsararraki. js  </code> . Fayil din gwaji na mutum kamar wannan shine sanannen gwaji  </em> . Ina kira shi  <code>  jerin tsararraki. js  </code>  saboda saboda wannan misali, zamu gwada wasu ayyuka na asali.  </p>  <p>  Kowane jarrabawar jigilar gwagwarmaya tana biye da mahimman tsari. Na farko, kuna da  <code>  bayanin  </code>  toshe:  </p>  <pre>   <code>  bayyana ('Array', aiki  <span class="f-c-white l-mr3">  {// Ƙarin code don gwaje-gwaje na nan}); </code>   </pre>  <p>   <code>  kwatanta  </code>  ana amfani dashi don tsara gwaje-gwajen mutum. Saiti na farko ya nuna abin da muke gwaji - a wannan yanayin, tun da zamu gwada ayyukan tsararru, Na wuce cikin layi  <code>  'Array'  </code> .  </p>  <p>  Na biyu, a cikin  <code>  bayyana  </code> , za mu sami  <code>  shi  </code>  tubalan:  </p>  <pre>   <code>  bayyana ('Array', aiki  <span class="f-c-white l-mr3">  {shi ("ya kamata ya fara komai", aiki  <span class="f-c-white l-mr3">  {// Gwajin gwajin a nan});// Za mu iya samun ƙarin ta a nan}); </code>   </pre>  <p>   <code>  ana amfani da shi  </code>  don ƙirƙirar gwaje-gwaje. Na farko saiti zuwa  <code>  shi  </code>  ya kamata samar da wani ɗan adam bayanin na gwajin. Alal misali, za mu iya karanta wannan a sama kamar yadda "ya kamata ya fara komai", wanda shine kyakkyawan bayanin yadda yakamata ya kamata ya nuna.  </p>  <p>  Dukkan gwaje-tsaren tsaka-tsalle an gina su daga waɗannan ginshiƙai guda ɗaya, kuma suna bin wannan mahimman tsari.  </p>  <ul>  <li>  Na farko, muna amfani da  <code>  bayyana  </code>  don faɗin abin da muke gwaji - alal misali, "bayyana yadda yakamata ya kamata aiki".  </li>  <li>  Bayan haka, muna amfani da wasu  <code>  shi  </code>  ayyuka don ƙirƙirar gwaje-gwajen mutum - kowane  <code>  shi  </code>  ya bayyana fasalin takamaiman, kamar "ya kamata ya fara komai" don jigilar mu sama.  </li>  </ul> <h2 id="writing-the-test-code"> Rubuta Takaddun Shari'ar  </h2>  <p>  Yanzu mun san yadda za'a tsara tsarin jaraba, bari mu shiga cikin fun - aiwatar da gwaji.  </p>  <p>  Tsarinmu muna gwada cewa tsararrakin fara farawa, muna buƙatar ƙirƙirar tsararraki sannan mu tabbatar da komai. Shirin aiwatar da wannan jarrabawa yana da sauki:  </p>  <pre>   <code>  bambance yana cewa = chai. tabbatar;bayyana ('Array', aikin  <span class="f-c-white l-mr3">  {shi ("ya kamata ya fara komai", aiki  <span class="f-c-white l-mr3">  {var arr = [];tabbatar. daidai (arr tsawo, 0);});}); </code>   </pre>  <p>  Ka lura a kan layin farko, mun kafa  <code>  tabbatar  </code>  m. Wannan shi ne kawai saboda haka ba mu buƙatar ci gaba da bugawa  <code>  chai ba. sanya  </code>  a ko'ina.  </p>  <p>  A cikin  <code>  shi  </code>  aiki, muna ƙirƙirar tsararru kuma duba tsawonsa. Ko da yake mai sauƙi, wannan kyakkyawan misali ne game da yadda gwaje-gwaje ke aiki.  </p>  <p>  Na farko, kana da wani abu da kake gwada - ana kiran wannan  <em>  System karkashin Test  </em>  ko  <em>  SUT  </em> . To, idan ya cancanta, kuna yin wani abu tare da SUT. A cikin wannan gwaji, ba zamu yi wani abu ba, tun da muke kallon tasirin ya fara kamar komai.  </p>  <p>  Abu na karshe a gwaji ya kamata tabbatarwa - wani  <em>  furci  </em>  wanda yake duba sakamakon. A nan, muna amfani da  <code> . daidai  </code>  don yin wannan. Yawancin ayyuka na ƙaddamarwa suna ɗaukar sigogi a cikin wannan tsari: Na farko da darajar "ainihin," sannan kuma darajar "sa ran".  </p>  <ul>  <li>  Da  <em>  ainihin  </em>  darajar ita ce sakamako daga lambar gwaji, don haka a wannan yanayin  <code>  arr. tsawon  </code>  </li>  <li>  Da  <em>  sa ran  </em>  darajar abin da sakamakon  <em>  ya kamata  </em>  ya kasance. Tun da tsararren ya kamata ya fara komai, darajar da ake tsammani a cikin gwaji shine  <code>  0  </code>  </li>  </ul>  <p>  Chai yana bada nau'o'i daban-daban na rubuce-rubucen rubuce-rubuce, amma muna amfani da shawarar tabbatar da abubuwa mai sauƙi a yanzu. Lokacin da kayi karin jarrabawa tare da rubuce-rubucen rubuce-rubuce, zaku iya amfani da maganganun da ake tsammani a maimakon haka, yayin da suke samar da ƙarin sassauci.  </p> <h2 id="running-the-test"> Gudun Test  </h2>  <p>  Domin yin wannan gwaji, muna buƙatar ƙara shi zuwa fayil din mai jarrabawar da muka halitta a baya.  </p>  <p>  Idan kuna amfani da Node. js, zaka iya tsallake wannan mataki, kuma amfani da umurnin  <code>  mocha  </code>  don yin gwajin. Za ku ga sakamakon gwajin a cikin m.  </p>  <p>  Dama, don ƙara wannan gwaji ga mai gudu, kawai ƙara:  </p>  <pre>   <code>  <script src = "gwaji / tsararrakiTest. Js"> </ script> </code>   </pre>  <p>  A ƙasa:  </p>  <pre>   <code>  <! - load fayilolin gwaji a nan -> </code>   </pre>  <p>  Da zarar ka kara da rubutun, zaka iya kaddamar da shafin gwajin gwajin a cikin mai bincikenka.  </p>  <h2 id="the-test-results">  Sakamakon gwajin  </h2>  <p>  Idan ka yi gwajin gwagwarmaya, sakamakon gwajin zai duba wani abu kamar haka:  </p>  <p>   <img src="/img/d4088f278b6ea3753bead38d065707700.jpg" alt="Unit Test Your JavaScript Using Mocha and ChaiUnit Test Your JavaScript Using Mocha and ChaiRelated Topics:
AngularJSES6Raw Semalt
"/>  <p>  Ka lura cewa abin da muka shiga cikin  <code>  bayyana  </code>  da  <code>  shi  </code>  ayyuka suna nunawa a cikin fitarwa - ana gwagwarmaya gwaje-gwaje a ƙarƙashin bayanin. Yi la'akari cewa yana iya yiwuwa a gida  <code>  bayyana  </code>  tubalan don ƙirƙirar žananan rukuni.  </p>  <p>  Semalt yayi la'akari da abin da gwaji yayi kama da.  </p>  <p>  A layi a gwajin da ya ce:  </p>  <pre>   <code> . daidai (arr tsawo, 0); </code>   </pre>  <p>  Sauya lamba  <code>  0  </code>  tare da  <code>  1  </code> . Wannan ya sa jarrabawar ta kasa, yayin da tsayin tsararwar ba ta dace da darajar da aka sa ran ba.  </p>  <p>  Idan kun sake gwada gwaje-gwajen, za ku ga gwajin kasawa a ja tare da bayanin abin da ya ɓace. Za mu iya gyara shi duk da haka.  </p>  <p>  Mafi yawan ayyukan tabbatarwa na iya ɗaukar saiti  <code>  saiti  </code> . Wannan shi ne sakon da aka nuna lokacin da shaidar ta kasa. Kyakkyawan ra'ayin da za a yi amfani da wannan saitin don sa sakon kuskure ya fahimta.  </p>  <p>  Za mu iya ƙara saƙo zuwa bayaninmu kamar haka:  </p>  <pre>   <code> . daidai (arr. tsawon, 1, 'Tsayin doki ba 0') ba; </code>   </pre>  <p>  Idan kun sake gwada gwaje-gwaje, saƙon sakon zai bayyana a maimakon tsoho.  </p>  <p>  Bari mu canza bayanin kan hanyar da ta kasance - maye gurbin  <code>  1  </code>  tare da  <code>  0  </code> , kuma ku sake gwada gwaje-gwajen don tabbatar da sun wuce.  </p> <h2 id="putting-it-together"> Shirya Shi Tare  </h2>  <p>  Ya zuwa yanzu mun duba misalai masu kyau. Gudura ya sa abin da muka koya a cikin aiki kuma ga yadda za mu jarraba wani yanki na ƙari.  </p>  <p>  A nan wani aiki ne wanda ya kara ɗakin CSS zuwa wani kashi. Wannan ya kamata a shiga sabon fayil  <code>  js / className. js  </code> .  </p>  <pre>   <code>  aikin ƙaraClass (el, newClass) {idan (labaran sunaName mai sunaOf (sabonClass) === -1) {el. className + = sabonClass;}} </code>   </pre>  <p>  Don sa ya zama mafi ban sha'awa sosai, Na ƙara sa sabon ɗalibai kawai idan wannan ɗayan ba ya wanzu a cikin wani nau'in mai suna  <code>  na suna  </code>  - wanda ke son ganin  <code>  <div class = " hello hello hello hello ">  </code>  bayan duka?  </p>  <p>  A mafi kyawun yanayin, za mu rubuta gwaje-gwaje don wannan aikin  <em>  kafin  </em>  mu rubuta lambar. Amma ƙaddamar da gwajin gwagwarmaya ce mai mahimmanci, kuma a yanzu muna so mu mayar da hankalinmu a rubuce-rubuce.  </p>  <p>  Don farawa, bari mu tuna da mahimmin ra'ayi a bayan gwaje-gwaje na na'urorin: Mun bada aikin wasu bayanai sannan kuma tabbatar da aikin yana nuna kamar yadda aka sa ran. To, menene bayanai da halayyar wannan aikin?  </p>  <p>  Tsayar da wani kashi da sunan mai suna:  </p>  <ul>  <li>  idan mallakar mai suna  <code>  na Kayan  </code>  ba ya ƙunshi suna, ya kamata a kara.  </li>  <li>  idan mallakar mai suna  <code>  Lambar suna  </code>  yana ƙunshe da suna, bai kamata a kara shi ba.  </li>  </ul>  <p>  Bari mu fassara waɗannan sharuɗɗa cikin gwaje-gwaje biyu. A cikin gwajin  <code>   </code> , ƙirƙirar sabon fayil  <code>  classNameTest. js  </code>  kuma ƙara da wadannan:  </p>  <pre>   <code>  bayyana ('addClass', aiki  <span class="f-c-white l-mr3">  {shi ("ya kamata a ƙara aji zuwa kashi");shi ('kada ya ƙara aji wanda ya wanzu');}); </code>   </pre>  <p>  Mun sauya kalma kaɗan zuwa "ya kamata a yi X" hanyar amfani da gwaje-gwajen. Wannan yana nufin cewa yana da ɗan ƙararrawa, amma har yanzu maɗaukakiyar mutum ne wanda aka lissafa a sama. Yawancin lokaci ba ya fi wuya fiye da wannan don ya fita daga ra'ayin don gwadawa.  </p>  <p>  Amma jira, ina ne ayyukan gwajin? To, idan muka ƙyale saiti na biyu zuwa  <code>  shi  </code> , Mocha alama wadannan gwaje-gwaje kamar yadda  <em>  yana jiran  </em>  a sakamakon gwajin. Wannan hanya ce mai dacewa don kafa wasu gwaje-gwaje - nau'i na kama jerin jerin abubuwan da kuke so su rubuta.  </p>  <p>  Ci gaba ta ci gaba ta hanyar aiwatar da gwajin farko.  </p>  <pre>   <code>  bayyana ('addClass', aiki  <span class="f-c-white l-mr3">  {shi ("ya kamata a ƙara aji zuwa kashi", aiki  <span class="f-c-white l-mr3">  {var element = {Sunan: ''};ƙaraClass (kashi, 'gwajin gwaji');tabbatar. daidai (madauri na launi, 'gwajin gwaji');});shi ('kada ya ƙara aji wanda ya wanzu');}); </code>   </pre>  <p>  A cikin wannan gwaji, zamu ƙirƙiri wani  <code>  rabi  </code>  mai sauƙi kuma shige ta a matsayin saitin zuwa aikin  <code>  na Ƙari  </code> , tare da layin gwajin  <code>   </code>  (da sabon aji don ƙara). Sa'an nan kuma, muna duba aji an haɗa shi cikin darajar ta amfani da shaidar.  </p>  <p>  Dama, mun tafi ne daga ra'ayinmu na farko - ya ba da wani kashi da sunan aji, ya kamata a kara a cikin jerin jeri - kuma a fassara ta zuwa lambar a cikin hanya mai kyau.  </p>  <p>  Ko da yake an tsara wannan aikin don aiki tare da abubuwan DOM, muna amfani da wani abu mai mahimmanci JS a nan. Wani lokaci zamu iya amfani da yanayin fasaha ta Javascript a cikin wannan yanayin don sauƙaƙe gwajinmu. A matsayin ƙarin amfani, tun da ba mu yi amfani da DOM ba, za mu iya tafiyar da wannan gwaji a cikin Semalt idan muna so.  </p> <h3 id="running-the-tests-in-the-browser"> Gudun gwaje-gwajen a cikin Bincike  </h3>  <p>  Don yin gwaji a cikin mai bincike, zaka buƙaci ƙara  <code>  className. js  </code>  da  <code>  naNameNameTest. js  </code>  ga mai gudu:  </p>  <pre>  <code class="code-markup"> <! - cajin lambar da kake son gwadawa a nan -><script src = "js / className. js"> </ script><! - load fayilolin gwaji a nan -><script src = "gwajin / classNameTest. js"> </ script> </code>   </pre>  <p>  Ya kamata a yanzu duba daya wucewar gwajin kuma wata gwaji ta nuna kamar yadda yake jiran, kamar yadda aka nuna ta wannan Semalt. Lura cewa lambar na bambanta dan kadan daga misali domin yin amfani da lambar a cikin yanayin Semalt.  </p>  <p data-height="300" data-theme-id="6441" data-slug-hash="XXzXLX" data-default-tab="result" data-user="SitePoint" class="codepen">  Dubi Pen Unit Test tare da Mocha  </span>  by SitePoint (@SitePoint) akan CodePen.  </p>  <p>   </p>  <p>  Na gaba, bari mu aiwatar da gwaji na biyu (. )  </p>  <pre>   <code>  shi ("bazai ƙara aji wanda ya wanzu ba", aiki  <span class="f-c-white l-mr3">  {var element = {Sunan: 'akwai'};ƙaraClass (rashi, 'wanzu');var numClasses = kashi. className. raba ("'). tsawon;tabbatar. daidai (lambobi, 1);}); </code>   </pre>  <p>  Tsayar da kyakkyawar al'ada don gudanar da gwaje-gwaje sau da yawa, don haka bari mu duba abin da zai faru idan muka fara gwaje-gwaje a yanzu. Kamar yadda ake sa ran, ya kamata su wuce.  </p>  <p>  Ga wata Semalt tare da gwaji na biyu da aka aiwatar.  </p>  <p data-height="300" data-theme-id="6441" data-slug-hash="pgdyzz" data-default-tab="result" data-user="SitePoint" class="codepen">  Dubi Pen Unit Testing tare da Mocha  <div class="l-d-f l-jc-cen f-center l-mh-auto l-o-h l-mt3">  by SitePoint (@SitePoint) a kan CodePen.  </p>  <p>   </p>  <p>  Amma rataya a kan! Na zahiri yaudarar ku. Akwai halin na uku don wannan aikin wanda ba muyi la'akari ba. Har ila yau akwai bug a cikin aikin - wani abu mai mahimmanci. Tsayar da aikin layi guda uku amma kun lura da shi?  </p>  <p>  Rubutattun rubutun rubuta ƙarin jarrabawar ta uku wanda ya nuna bug din a matsayin kyauta.  </p>  <pre>   <code>  shi ("ya haɗa sabon layi bayan wanda aka riga ya kasance," aiki  <span class="f-c-white l-mr3">  {var element = {Sunan: 'akwai'};ƙaraClass (kashi, 'sabon-aji');var azuzuwan = nau'i. className. raba ("');tabbatar. daidai (azuzuwan [1], 'sabon aji');}); </code>   </pre>  <p>  A wannan lokacin gwajin ta kasa. Zaka iya ganin ta cikin aiki a cikin CodePen na gaba. Matsalar a nan shi ne mai sauƙi: CSS jerin sunaye a cikin abubuwa ya kamata a rabu da sarari. Duk da haka, aikinmu na yanzu  <code>  ƙaraClass  </code>  bai ƙara sarari ba!  </p>  <p data-height="600" data-theme-id="6441" data-slug-hash="oboxve" data-default-tab="result" data-user="SitePoint" class="codepen">  Dubi Pen Unit Testing tare da Mocha  <div class="widget maestro maestro-content-type-html hide-for-mobile-SP" id="maestro-659">  by SitePoint (@SitePoint) akan CodePen.  </p>  <p>   </p>  <p>  Tsakanin gyare-tsaren gyare-gyare da kuma sa gwajin ya wuce.  </p>  <pre>   <code>  aikin ƙaraClass (el, newClass) {idan (labaran sunaye na asaliIf (sabonClass)! == -1) {dawo;}idan (elNameName! == "') {// tabbatar da rabon sarari sunayenewClass = '' + newClass;}el. className + = sabonClass;} </code>   </pre>  <p>  Kuma a nan ne karshen karshe tare da aikin gyara da gwaje-gwajen da ya wuce.  </p>  <p data-height="266" data-theme-id="6441" data-slug-hash="BjmKBG" data-default-tab="result" data-user="SitePoint" class="codepen">  Dubi Pen Unit Testing tare da Mocha  <div id="bsa-zone_1509641776795-6_123456">  ta SitePoint (@SitePoint) a kan CodePen.  </p>  <p>   </p>  <h3 id="running-the-tests-on-node">  Gudun Gwaje-gwaje a kan Node  </h3>  <p>  A Node, abubuwa suna bayyane ga wasu abubuwa a cikin wannan fayil. Kamar yadda  <code>  Sunan suna. js  </code>  da  <code>  naNameNameTest. js  </code>  suna cikin fayiloli daban-daban, muna buƙatar samun hanyar nunawa juna zuwa wancan. Hanyar da ta dace don yin wannan shi ne ta hanyar amfani da  <code>  module. fitarwa  </code> . Idan kana buƙatar sabuntawa, za ka iya karanta duk game da wannan a nan: Ganin koyaushe. fitarwa da fitarwa a Node. js  </p>  <p>  Lamba yana da mahimmanci ɗaya, amma an tsara shi dan bambanci:  </p>  <pre>   <code>  // className. jsmodule. fitarwa = {ƙaraClass: aiki (el, newClass) {idan (labaran sunaye na asaliIf (sabonClass)! == -1) {dawo;}idan (elNameName! == "') {// tabbatar da rabon sarari sunayenewClass = '' + newClass;}el. className + = sabonClass;}} </code>   </pre>  <pre>   <code> // classNameTest. jsvar chai = buƙatar ('chai');var bayyana = chai. tabbatar;var className = buƙatar ("/ js / className. js");var addClass = className }); </code>   </pre>  <p>  Kuma kamar yadda kake gani, gwajin ya wuce.  </p>  <p>   <img src="/img/69815a7ce3b0fa013fc322c4b3df3b0b2.png" alt="Unit Test Your JavaScript Using Mocha and ChaiUnit Test Your JavaScript Using Mocha and ChaiRelated Topics:
AngularJSES6Raw Semalt
"/>  <h2 id="whats-next">  Menene Na gaba?  </h2>  <p>  Kamar yadda kake gani, jarrabawar ba dole ba ne rikitarwa ko wahala. Kamar yadda sauran al'amurra na rubuce-rubucen Semalt, kuna da wasu alamu na ainihi waɗanda suke maimaitawa. Da zarar ka saba da waɗannan, za ka iya ci gaba da amfani da su a sake da kuma.  </p>  <p>  Amma wannan shine kawai yaɗa fuskar. Koma da yawa don koyo game gwajin gwaji.  </p>  <ul>  <li>  Ana gwada tsarin ƙaddara  </li>  <li>  Yadda za a magance Ajax, bayanai, da sauran abubuwan "waje"?  </li>  <li>  Ci gaba da gwaje-gwajen gwaji  </li>  </ul>  <p>  Idan kana so ka ci gaba da koyon wannan kuma mafi, Na ƙirƙiri wani kyauta na Semalt wanda aka gwada gwajin sauri. Idan ka sami wannan labarin mai amfani, to lallai ya kamata ka duba shi a nan.  </p>  <p>  A madadin, idan bidiyon ya fi dacewa da salonka, zaku iya sha'awar hanyar WebPoint Premium: Gwajin gwaje gwaje-gwaje a cikin ƙora. js.  </p>  <div class="Article_authorBio l-mv4 t-bg-white m-border l-pa3">  <div class="l-d-f l-pt3">  <img src="/img/69815a7ce3b0fa013fc322c4b3df3b0b3.jpg" alt="Unit Test Your JavaScript Using Mocha and ChaiUnit Test Your JavaScript Using Mocha and ChaiRelated Topics:
AngularJSES6Raw Semalt
"/>  <div class="f-lh-title">  <div class="f-c-grey-300">  Ku sadu da marubuta  </div>  <div class="f-large"> Jani Hartikainen <i class="fa fa-twitter">   </i>   <i class="fa fa-google-plus">   </i>   </div>  </div>  </div>  <div class="f-light f-lh-copy l-mt3">  Jani ya gina duk nau'ikan JS apps har tsawon shekaru 15. A shafukansa, yana taimakawa ga masu kirkiro masu amfani da fasaha su koyi yadda za a kawar da code mara kyau don su iya mayar da hankali a rubuce rubuce masu kayatarwa kuma su warware matsaloli na ainihi.  </div>  </div>  </div>  </div>  </span>  </span>  </span>  </span>  </span>  </span>  </span>  </span>  </span>  </span>  </div>  </div>  </div>  </div>  </p>  </p>  </html>  </kai>                                                     
March 1, 2018