Gabatarwa zuwa Mocha da Chai

Mocha kuma Chai su ne tsarin gwaji guda biyu da aka amince da su sosai a cikin yanayin yanayin Node.js. Suna ba wa masu haɓaka kayan aiki masu ƙarfi da ƙarfi don gwada aikace-aikacen su, suna tabbatar da ƙarfinsu da amincin su. Bari mu bincika abin da ke haifar Mocha da Chai mahimman abubuwan tsarin gwaji da kuma dalilin da yasa masu haɓakawa suka dogara da su.

 

Shigarwa da daidaitawa Mocha kuma Chai a cikin aikin Node.js

Don shigarwa da daidaitawa Mocha kuma Chai a cikin aikin Node.js, zaku iya bin matakan da ke ƙasa:

Mataki 1 : Fara aikin Node.js

   - Buɗe a terminal kuma kewaya zuwa kundin aikin.

   - Gudun umarni mai zuwa don fara sabon aikin Node.js:

npm init -y

   - Wannan umarnin zai ƙirƙiri package.json  fayil ɗin da ke ɗauke da bayanai game da aikin da abubuwan dogaronsa.

Mataki 2: Shigar Mocha da Chai

   - Buɗe a terminal kuma gudanar da umarni mai zuwa don shigarwa Mocha da Chai: 

 npm install --save-dev mocha chai

   - Wannan umarnin zai shigar Mocha kuma Chai a cikin node_module  kundin tsarin aikin ku kuma ƙara su zuwa devDependencies sashin da ke cikin package.json fayil ɗin.

Mataki 3: Ƙirƙiri littafin gwaji

   - Ƙirƙiri sabon kundin adireshi a cikin aikin ku don adana fayilolin gwaji. Yawanci, ana kiran wannan directory  test ko spec.

   - A cikin kundin gwajin, ƙirƙiri fayil ɗin gwajin misali tare da sunan `example.test.js`.

Mataki na 4: Rubuta gwaje-gwaje ta amfani Mocha da kuma Chai

   - Buɗe example.test.js  fayil ɗin kuma ƙara shigo da abubuwa masu zuwa:

const chai = require('chai');  
const expect = chai.expect;  
  
// Define the test suite  
describe('Example Test',() => {  
  // Define individual test cases  
  it('should return true',() => {  
    // Define test steps  
    const result = true;  
      
    // Use Chai to assert the result  
    expect(result).to.be.true;  
  });  
});  

Mataki na 5: Gudanar da gwaje-gwaje

   - Buɗe terminal a kuma gudanar da umarni mai zuwa don aiwatar da gwaje-gwaje:

npx mocha

   - Mocha zai bincika da gudanar da duk fayilolin gwaji a cikin littafin gwajin.

Wannan shine yadda zaku iya shigarwa da daidaitawa Mocha kuma Chai a cikin aikin ku na Node.js. Kuna iya ƙirƙira da gudanar da ƙarin fayilolin gwaji don gwada ayyuka da hanyoyi daban-daban a cikin aikinku.

 

Kammalawa: A cikin wannan labarin, mun aza harsashin fahimta Mocha, da Chai. An sanye ku da ilimin Mocha da Chai, ƙaƙƙarfan tsarin gwaji guda biyu waɗanda zasu taimaka muku gina ingantattun ɗakunan gwaji masu inganci don aikace-aikacen ku na Node.js. Kasance da mu don labarin na gaba a cikin wannan silsilar, inda za mu zurfafa zurfafa cikin ƙirƙirar gwaji masu sauƙi tare Mocha da Chai.