Search code examples
javascriptmocha.jschakram

How to execute multiple API calls using chakram and mocha?


I'm using mocha as a test framework, and chakram for API calls.

I have an array of objects, I want to loop over them and POST each one to an API, how can I do this? What I've tried so far doesn't give any output from the each iteration.

This is what I've come up with so far, using it-each to loop over an array, then chakram to post each one. I'm not quite sure how to tie in the chakram promise chain with the it-each

  const itEach = require('it-each')({ testPerIteration: true });

  const url = ""
  things = [{"title":"A"},{"title":"B"}]

  it.each(things, "calling API", ['element'], (element, next) =>{
      console.log("about to post a thing " + JSON.stringify(element))
      chakram
          .post(url, element)
          .then(uploadResponse => {
              expect(uploadResponse).to.have.status(200)
              console.log("finished upload for element " + element)
              next()
           })
  })

What have I got wrong? JS isn't my language, I'm not very good with promise chains.


Solution

  • trigger all your request and then use chakram.waitfor property to get all your responses.

    it("get all responses",function() {
    response1=chakram.put(url,param,options)
    response2=chakram.put(url,param,options)
    response3=chakram.put(url,param,options)
    response4=chakram.put(url,param,options)
    
     return chakram.waitFor([
                    expect(response1).to.have.json(function (json) {
    })
     expect(response2).to.have.json(function (json) {})
     expect(response3).to.have.json(function (json) {})
     expect(response4).to.have.json(function (json) {})
    ])
    });