mardi 4 septembre 2018

Converting Mocha api test from async await to Supertest

i have following question, i start writing API test, now it's look like:

  xit('should add address ', async () => {
    const result = await request({
      headers: Object.assign(config.headers, { 'Authorization': 'Bearer '+auth_token }),
      url: `${config.url}/rest/v1/address/`,
      method: "POST",
      json: {
        "name": generatedAddressName,
        "city": "WARSZAWA",
        "street": "UL. KASPROWICZA",
        "houseNumber": "49XX",
        "apartNumber": "",
        "lat": 52.176903,
        "lng": 21.028369,
        "zipCode": "02-732",
        "isDefault": false,
        "inDeliveryZone": true
      }
    });
  });

now i reading about Supertest library, test under Supertest look more readable and i want to convert my test cases to Supertest I tried to do that, and there's no effects, now i have:

  it('should add address supertest', function(done) {
    request
      .post('/rest/v1/address/')
      .set(config.headers)
      //.set('Accept', 'application/json')
      .set('Authorization', 'Bearer ' + auth_token)    
      .send({
        "name": generatedAddressName,
        "city": "WARSZAWA",
        "street": "UL. KASPROWICZA",
        "houseNumber": "51",
        "apartNumber": "",
        "lat": 52.176903,
        "lng": 21.028369,
        "zipCode": "02-732",
        "isDefault": false,
        "inDeliveryZone": true
      })
      .expect(200)
      .end(function(err,res){
        done(err);
      });       
   });

and i have 'TypeError: request.post is not a function' Can you help me with converting to Supertest? And at the same time I still want to use async/await

Aucun commentaire:

Enregistrer un commentaire