0

I have a test that creates a new "municipality" record with a POST call to an API... then in the same test, I want to GET it back and see if it was created successfully. The problem is, I think it's getting it back too fast (before the record was successfully created). How do I fix that? I don't want the "GET" to be called until the POST is complete.

My test looks like this:

    it('Insert random muncipality with name of APIAutomation-CurrentDateTime', function (done) {
    let newMuncID = 0;
    //create a random string first for the name.
    var currentDateTime = new Date().toLocaleString();
    api.post('/rs/municipalities')
      .set('Content-Type', 'application/json')
      .send({
        "muncplName": "APIAutomation-" + currentDateTime,
        "effDate": "2018-01-25",
        "provId": 8
      })
      .end(function (err, res) {    
        expect(res).to.have.property('status', 200);
        expect(res.body).to.have.property('provId', 8);
        newMuncID = res.body.muncplId;
        done();
      });

      //Now, query it back out again
      api.get('/rs/municipalities/' + newMuncID)
      .set('Content-Type', 'application/json')
      .end(function (err, res) {    
        expect(res.body).to.have.property("provId", 8);
        done();
      });
  });

Initialising this code looks like this:

import {expect} from 'chai';
import 'mocha';
import {environment} from "../envConfig"

var supertest = require("supertest");
var tags = require('mocha-tags');
var api = supertest(environment.URL);
user952342
  • 2,602
  • 7
  • 34
  • 54
  • Where is supertest in it? Have you initialized the 'api' to supertest? Looking at the code, it looks likeyou are making a direct API call. – user2347763 Mar 20 '18 at 23:06
  • Sorry, I've added my init code now. These tests are going out to an HTTP endpoint – user952342 Mar 21 '18 at 15:15
  • What is your environment? Node.js? And webserver is expessjs? – user2347763 Mar 22 '18 at 22:24
  • I don't think my environment really matters. I am simply making http requests to an API. If it matters, the API is node.js – user952342 Mar 23 '18 at 13:13
  • please edit the question title because your accepted answer/solution has nothing to do with supertest but with async – user2347763 Mar 25 '18 at 22:26
  • @rjv I think it does. The initial problem I was having was that the GET command was being executed before the first PUT command was finished. Using this Async package, I could execute them in series to ensure they are executed in the correct order. – user952342 Mar 26 '18 at 13:27
  • you don't understand. The questions on SO are indexed on keywords by search engines and your question as well as the solution is majorly about async processing even though supertest is used so, including the word async in the question title will help users who google for solutions using 'async.series or async get/put' in their google search to find this question and accepted answer. – user2347763 Mar 27 '18 at 19:01

1 Answers1

0

I found a good solution using the Async package. It allows you to make API calls in series. You can have it wait for the answer to come back before executing the next test.

The code ends up looking like this:

it('Change a municipality name', function (done) {   
        async.series([

            function(cb) { //First API call is to get the municipality 
                api.get('/rs/municipalities/' + muncToBeAltered)
                .set('Content-Type', 'application/json')
                .end(function (err, res) {   
                    actualVersion = res.body.version;
                    cb();
                }); 
            }, //end first - GET API call

            function(cb) { //second API call is to make a change
                api.put('/rs/municipalities/' + muncToBeAltered)
                .set('Content-Type', 'application/json')
                .send({
                    "muncplName": newMuncName,
                    "provId": "3",
                    "status": "01",
                    "version": actualVersion
                })
                .end(function (err, res) {    
                expect(res).to.have.property('status', 200); 
                cb();
                });
            }, //end second - PUT API call

        ], done);
    });
user952342
  • 2,602
  • 7
  • 34
  • 54