0

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

3 Answers3

0

not sure if this will work, but it should be:

it('should add address supertest', async function(done) {
 const res = await 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)
  .catch(done)   
 done()
});  

there is a section in https://github.com/visionmedia/supertest that talks about promises.

LostJon
  • 2,287
  • 11
  • 20
  • you may even be able to get rid of the `done()` at the base of the function and just return the request. not sure... – LostJon Sep 04 '18 at 20:30
0

Ok, about your error: you need to import your app and pass it as a parameter to request, like this:

request(app).post(...

Now about the async await: this is what your code should look like

it('should add address supertest', async () => {
  const res = await request(app)
    .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(res.statusCode).to.equal(200);
});

Edit: Note this on supertest documentations

Blockquote You may pass an http.Server, or a Function to request() - if the server is not already listening for connections then it is bound to an ephemeral port for you so there is no need to keep track of ports.

I don't think passing an URL to request() will work

iagowp
  • 2,428
  • 1
  • 21
  • 32
0

@iagowp I wrote the code as in your example, and it throws me

  1) 0_auth
       should return token for unauthorized user:
     Error: incorrect header check
      at Unzip.zlibOnError (zlib.js:153:15)

const chai = require('chai');
//const request = require('request-promise-native');
const mocha = require('mocha');
const config = require('../config');
const request = require('supertest');
const assert = chai.assert;
auth_token = '';


describe('0_auth', () => {
    it('should return token for unauthorized user', async () => {
    const res = await request(url)
      .post('/rest/v1/auth/get-token')
      .set(config.headers)
      //.set('Accept', 'application/json')  
      .send({
          "deviceUuidSource": "DEVICE",
          "source" : "KIOSK_KFC",
          "deviceUuid" : "uniquedeviceuuid"
      })
      .end(function(err,res){
        assert.equal(res.status,200)
        assert.property(res.body, 'token')
        assert.isString(res.body.token)
        auth_token=res.body.token
        console.log('unathorized token: '+auth_token) 
        done(err);
      });    
      expect(res.statusCode).to.equal(200);   
   });


   it('should return token for authorized user', async () => {
    const res = await request(url)
      .post('/rest/v1/auth/with-password')
      .set(config.headers)
      .set('Authorization', 'Bearer ' + auth_token) 
      //.set('Accept', 'application/json')  
      .send({
        "email" : user,
        "password" : password
      })
      .end(function(err,res){
        assert.equal(res.status,200)
        assert.property(res.body,'token')
        assert.isString(res.body.token)
        assert.equal(res.body.user.email,user)
        assert.isFalse(res.body.user.locked)
        auth_token=res.body.token
        console.log('authorized token: '+auth_token) 
        done(err)
      });  
      expect(res.statusCode).to.equal(200);       
   });

});