An warware: supertest npm aika node js

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.

Shafi posts:

Leave a Comment