Forum Discussion

paul_i's avatar
paul_i
New Contributor
5 years ago

Problems automating tests with Javascript/Mocha/chai-http

Anybody have experience integrating CucumberStudio with JavaScript/Mocha framework?

I’m trying to integrate existing chai-http tests, and having problems with the fact that the chai calls are asynchronous.

 

The original chai call looks something like this…

 

// When
  chai.request(restServiceUrl)
  .post(quoteEndPoint)
  .send()
    .end((err, res) => {
// Then
    expect(res).to.have.status(HttpStatus.OK);
}

 

 

I’ve created a Cucumber Studio scenario with a When action for the http post, and a Then action for the status check. Thus I have 2 Actionwords methods generated by hiptest-publisher – in the When method I make the http request and save the status in a global variable, in the Then method I verify the status.

 

whenActionWord: function () {
  chai.request(restServiceUrl)
  .post(quoteEndPoint)
  .send()
    .end((err, res) => {
	 responseStatus = res.status; //global variable
  }
}

thenActionWord: function () {
    expect(responseStatus).to.be.equal(HttpStatus.OK); 
}
        

 

 

The problem is the chai calls are asynchronous, so thenActionWord() is called before whenActionword() has completed (status is not saved) and the assertion fails.

 

I have searched online for possible solution (TBH I’m new to Javascript/Mocha), and tried using done() and await() but without success so far …

Anybody come across this problem and can point me a solution?