cancel
Showing results for 
Search instead for 
Did you mean: 

Problems automating tests with Javascript/Mocha/chai-http

SOLVED
Highlighted
New Contributor

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Staff

Re: Problems automating tests with Javascript/Mocha/chai-http

Hi @paul_i !

 

Have you had the opportunity to try the --delay flag or to work with hooks?

View solution in original post

1 REPLY 1
Staff

Re: Problems automating tests with Javascript/Mocha/chai-http

Hi @paul_i !

 

Have you had the opportunity to try the --delay flag or to work with hooks?

View solution in original post

New Here?
Join us and watch the welcome video:
Top Kudoed Authors
Labels