Tabbas, ga yadda zan tsara da gabatar da labarin da ake nema:
SuperTest babban matakin abstraction ne don gwada HTTP, yana ba da hanya mai sauฦi kuma mai sassauฦa ga masu haษaka Node.js don gwada APIs ษin su yadda ya kamata. Yana aiki tare da kowane tsarin gwaji, kuma ana iya shigar dashi cikin sauฦi ta npm.
SuperTest yana sauฦaฦa yin kwatancen buฦatun HTTP zuwa uwar garken ku kuma duba martanin, ta yadda za ku tabbatar da aikace-aikacenku ya kasance kamar yadda aka zata.
Aiwatar da masu kai a cikin SuperTest
Aika kan kai tare da SuperTest kai tsaye - an ฦera shi don zama mai hankali da sauฦin amfani. Kuna iya haษa da kanun labarai yayin da kuke neman mara kyau ta aikin .set().
const request = require('supertest'); const app = require('../app'); describe('GET /', function() { it('responds with json', function(done) { request(app) .get('/') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); });
A cikin wannan snippet lambar, muna aika buฦatar GET zuwa tushen URL ("/") na app ษin mu. Mun saita taken Yarda zuwa โapplication/jsonโ, yana nuna cewa muna son amsawa a tsarin JSON, sannan mu ayyana tsammaninmu don amsa.
Fahimtar Code
Bari mu nutsu a cikin ginin wannan gwajin:
- nema (app): Yi amfani da aikin buฦatar da aka shigo da shi daga 'supertest' don ฦirฦirar buฦatar HTTP zuwa misalin uwar garken ku, wanda app ke wakilta.
- .samu ('/'): Wannan sarkar hanyar HTTP GET zuwa buฦatarmu, da nufin tushen hanyar sabar mu.
- .set('A karษa', 'application/json'): Wannan yana aika taken "Karษa: aikace-aikacen/json" a cikin buฦatarmu, yana gaya wa uwar garken ya amsa da JSON.
- . tsammanin ('Nau'in Abun ciki', /json/): Wannan yana ฦayyadad da tsammanin amsa - muna tsammanin taken-Nau'in Abun ciki ya haษa da "json".
- .sa ran (200, yi): Wannan wani fata ne - muna tsammanin lambar matsayi 200 a cikin martani. Ana ฦetare aikin da aka yi azaman sake kira don lokacin da gwajin ya ฦare.
ฦarin Hanyoyi a cikin SuperTest
SuperTest yana ba da wasu hanyoyi masu kyau don daidaita buฦatunku da tsammaninku, yana ba da madaidaiciyar hanya mai sauฦi ga gwajin API. Wasu hanyoyin lura sun haษa da:
- .post(hanyar): Don yin buฦatun POST.
- .sa (hanyar): Don yin buฦatar PUT.
- .share(hanyar): Don yin buฦatun SHAFE.
- .tsamman (tsari[, fn])
- .tsammani (matsayi, jiki[, fn])
SuperTest rpm yana ba wa masu haษaka ฦaฦฦarfan hanya mai hankali don gwada ฦarshen ฦarshen API, yana mai da shi muhimmin kayan aiki a cikin kayan aikin haษakawa don tabbatar da ingancin aikace-aikacen yanar gizo.