chaijs / chai-http

HTTP Response assertions for the Chai Assertion Library.
http://chaijs.com/plugins/chai-http
632 stars 112 forks source link

Chai promisses without any error catch possible #171

Open ignachiou opened 7 years ago

ignachiou commented 7 years ago

I'm using wdio(using this because all my test run over wdio and I suppose to use this for api rest test), chai and chai-http in async mode to make some api rest test over a system, at first, the command line console show me the errors in red and everything was good but since the weekend the test case havent show me the errors in red and the test cases just put the green check at the comand line like it wouldn't be any errors. I think this happend after I update chai cnah chai-http. Someone knows whats going on?

  var chai = require('chai');
  var chaiHttp = require('chai-http');
  global.expect = chai.expect;
  chai.Should();
  chai.use(chaiHttp);

   var username = 'salvador.salvatierra@alegra.com';
   var token = 'a5ff7f1063582fd0140d';

  describe('We get a cotnact list in body', function(){

it(' GET at endpoint /contacts', function(){

    chai.request('https://app.alegra.com/api/v1/contacts')
    .get('/')
    .set('Accept','application/json')
    .query({limit:'10', order_direction: 'DESC', order_field: 'id'})
    //.auth(username,token)
    .then(function(res){
        console.log(res);
        res.should.have.status(200);
        res.body.should.be.a.json;
    })
    .catch(function (err){
        setTimeout(function(){
            throw new err;
        });         
    })      
});

  });

This is my package.json:

      "devDependencies": {
          "chai": "^4.1.1",
          "chai-http": "^3.0.0",
          "wdio-dot-reporter": "0.0.8",
          "wdio-mocha-framework": "^0.5.11",
          "wdio-selenium-standalone-service": "0.0.8",
           "wdio-spec-reporter": "^0.1.2",
            "webdriverio": "^4.8.0"
          }
leggsimon commented 6 years ago

If I understand your problem correctly, you're expecting to see tests fail and they are not. I can see a couple of things that might be the case. This is an asynchronous test so you would want to return the promise with your test assertions. (Or possibly use the sync: false config in wdio but I'm not familiar with it)

Example:

it(' GET at endpoint /contacts', function(){

    return chai.request('https://app.alegra.com/api/v1/contacts')
      .get('/')
      .set('Accept','application/json')
      .query({limit:'10', order_direction: 'DESC', order_field: 'id'})
      //.auth(username,token)
      .then(function(res){
        console.log(res);
        res.should.have.status(200);
        res.body.should.be.a.json;
      })
      .catch(function (err){
        setTimeout(function(){
            throw new err;
        });         
      })        
});

Also, the fact that you're catching your error might hide any failing messages, you are throwing it again but inside a setTimeout which again is asynchronous.

ignachiou commented 6 years ago

Yes, this is a async test. So how can i receive the red warning of an error? Any idea?

leggsimon commented 6 years ago

So I think if you remove the .catch() and return the Promise to the test runner. It might show you the error it is encountering. (Assuming your test runner is similar to ones I've used)

leggsimon commented 6 years ago

Did that work at all for you @ignachiou?