Thinkful-Ed / node-jwt-auth

15 stars 118 forks source link

explain expect.fail #11

Open rjwilliams87 opened 6 years ago

rjwilliams87 commented 6 years ago

I'm having an issue understanding the test. When running the test expect.fail always fails the test. Can you give some insight into how expect.fail works?

oampo commented 6 years ago

That's basically the idea of expect.fail - if you hit that line the test should fail. Take this test for example:

    it('Should reject requests with no credentials', function () {
      return chai
        .request(app)
        .post('/api/auth/login')
        .then(() =>
          expect.fail(null, null, 'Request should not succeed')
        )
        .catch(err => {
          if (err instanceof chai.AssertionError) {
            throw err;
          }

          const res = err.response;
          expect(res).to.have.status(400);
        });
});

We expect a 400 response from the server, so we should never hit the .then block. .post will return a rejected promise if the response isn't in the 2xx range. So that assertion is basically testing that the response is not a 2xx.

PeasOfMind commented 5 years ago

This behavior is not happening with newer versions of chai-http (I am using 4.2.0, not 3.0.0). All 4xx and 5xx responses are hitting the .then block and are being thrown as chai.AssertionError. That makes them funnel into the if statement in the .catch block, NOT the expect() statements as they should.

oampo commented 5 years ago

Aha, yeah - that was a breaking change with chai-http v4. Happy to take a PR to update, otherwise I'll leave this issue open and work on it when I get a moment.