Gwaje-gwajen Rubutu a cikin TypeScript: Amfani Jest, Mocha, da Haɗuwa tare Chai da Sinon

Unit test ing wani muhimmin bangare ne na haɓaka software don tabbatar da daidaito da amincin lambar tushe. Tare da TypeScript, zaku iya rubuta unit test s cikin sauƙi da sassauƙa, ta amfani da mashahuran ginshiƙai kamar Jest da Mocha, haɗe da dakunan karatu kamar Chai da izgili da ɗakunan karatu kamar Sinon.

Anan akwai cikakken jagora kan rubuta unit test s TypeScript tare da waɗannan kayan aikin da ɗakunan karatu:

 

Jest

Jest tsarin da aka yi amfani da shi sosai don rubuta unit test s in TypeScript da JavaScript. Yana ba da sauƙi mai sauƙi da fasali mai ƙarfi kamar izgili, gwajin hoto, da rahotannin ɗaukar hoto.

Don fara rubuta unit test s tare da Jest, kuna buƙatar shigarwa Jest ta npm ko yarn ta hanyar aiwatar da umarni mai zuwa:

npm install jest --save-dev

Sannan, zaku iya ƙirƙirar fayilolin gwaji tare da tsawo na .spec.ts ko .test.ts kuma ku rubuta shari'o'in gwaji.

Misali:

// math.ts  
export function add(a: number, b: number): number {  
  return a + b;  
}  
  
// math.spec.ts  
import { add } from './math';  
  
test('add function adds two numbers correctly',() => {  
  expect(add(2, 3)).toBe(5);  
});  

 

Mocha

Mocha tsari ne mai sassauƙan gwajin gudu don TypeScript da JavaScript. Yana goyan bayan bayyanannen tsarin aiki da nau'ikan gwaje-gwaje iri-iri kamar unit test s, gwaje-gwajen haɗin kai, da gwaje-gwajen aiki.

Don amfani Mocha a cikin TypeScript, kuna buƙatar shigarwa Mocha kuma Chai ta npm ko yarn ta hanyar aiwatar da umarni mai zuwa:

npm install mocha chai --save-dev

Sannan, zaku iya ƙirƙirar fayilolin gwaji kuma ku rubuta shari'o'in gwaji.

Misali:

// math.ts  
export function add(a: number, b: number): number {  
  return a + b;  
}  
  
// math.spec.ts  
import { expect } from 'chai';  
import { add } from './math';  
  
describe('add function',() => {  
  it('should add two numbers correctly',() => {  
    expect(add(2, 3)).to.equal(5);  
  });  
});  

 

Chai

Chai sanannen ɗakin karatu ne na tabbatarwa da ake amfani da shi don rubuta iƙirarin a cikin unit test s. Yana ba da madaidaicin daidaitawa mai sauƙi, yana ba ku damar tabbatar da sakamakon lambar tushen ku. Kuna iya amfani Chai da ko dai Jest ko Mocha don rubuta ikirari a cikin shari'ar gwajin ku.

Misali:

import { expect } from 'chai';  
import { add } from './math';  
  
it('add function should add two numbers correctly',() => {  
  expect(add(2, 3)).to.equal(5);  
});  

 

Sinon

Sinon sanannen ɗakin karatu ne na izgili da leƙo asirin ƙasa da ake amfani da shi don izgili da bin ɗabi'u a cikin shari'o'in gwaji. Kuna iya amfani Sinon da ko dai Jest ko Mocha don yin izgili da waƙa da ayyuka a cikin abubuwa da ayyuka.

Misali:

import { expect } from 'chai';  
import { add } from './math';  
import sinon from 'sinon';  
  
it('add function should call console.log with the correct result',() => {  
  const consoleSpy = sinon.spy(console, 'log');  
  add(2, 3);  
  expect(consoleSpy.calledWith(5)).to.be.true;  
  consoleSpy.restore();  
});  

 

Haɗuwa Jest ko Mocha tare da Chai ba Sinon ku damar gina ƙarfi da sassauƙa unit test s a cikin TypeScript. Ta amfani da hanyoyi da ayyukan Jest, Mocha, Chai, da Sinon, za ku iya tabbatar da daidaito da amincin lambar tushen ku yayin aikin haɓaka software.