0

Suppose there are two js files called main.js and test.js. main.js is the code that I intend to test it by Mocha and Chai. For that, I created test.js that includes expect() function. expect() is a Chai method and takes assessment the result we expect (Is the main function for testing code).

In main.js, There is a request method that I need to test it. The request method is a Promise and is a thenable function. As I read Chai doc, I should try expect() inside the callback of .then(), just like this:

chai.request(app)
  .put('/user/me')
  .send({ password: '123', confirmPassword: '123' })
  .then(function (res) {
     expect(res).to.have.status(200);
  })

Hence, I should merge main.js to test.js. So, because of any modify on main.js, I must duplicate that change on test.js too. While, I need to include entire of main.js by require() in test.js and test it there. So my question is:

How can I test a request method by expect() in such a way that the request method remains in main.js and expect() remains in the test.js, there is no need to try request method in the test.js (Like chai.request(app)) and there is no need to try expect() inside of the then() callback (Like the mentioned code)?

Alireza
  • 227
  • 1
  • 11
  • Please clarify your question. It's unclear. How about `async/await`? – Lin Du Feb 15 '23 at 03:13
  • @LinDu I modified and clarified question. – Alireza Feb 15 '23 at 05:58
  • See this post for how you could use the Chai-HTTP for testing your web APIs: [Post request via Chai](https://stackoverflow.com/questions/35697763/post-request-via-chai) and [How can I return the response from my api when testing with chai-http and Jest](https://stackoverflow.com/questions/65598586/how-can-i-return-the-response-from-my-api-when-testing-with-chai-http-and-jest). – prasad_ Feb 15 '23 at 06:12
  • @prasad_ The first link talks about sending request by chai. The second doesn't solve my problem. Thanks anyway. – Alireza Feb 15 '23 at 07:06

0 Answers0