Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explain expect.fail #11

Open
rjwilliams87 opened this issue Oct 26, 2018 · 3 comments
Open

explain expect.fail #11

rjwilliams87 opened this issue Oct 26, 2018 · 3 comments

Comments

@rjwilliams87
Copy link

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
Copy link
Contributor

oampo commented Oct 29, 2018

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
Copy link
Contributor

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
Copy link
Contributor

oampo commented Dec 31, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants