Hello, Good Afternoon, I had written one consumer ...
# pact-js
h
Hello, Good Afternoon, I had written one consumer test for
200
status and it worked well for me, my next task is to validate
401 Unauthorised
state, but my consumer test is failing https://codeshare.io/wnEOZD, could anyone point me to right direction.
y
Does your http client throw an error on non 2xx status codes?
If so please read https://jestjs.io/docs/asynchronous Sample code await expect(fetchData()).rejects.toMatch('error');
h
My test fails with this line
Request failed with status code 401
y
I assume the promise has rejected as the provider is setup to return a 401 await api.getUserSettingsStatus()
h
I feel, it gets the status
401
but does not match the body
Copy code
async getUserSettingsStatus() {
    return axios
      .get(this.withPath("/api/1/user"), {
        headers: authHeader,
      })
      .then((r) => {
        return r.data;
      });
  }
t
In your test you must catch the failing promise and confirm that it meets the expectations of the calling code
h
got it working thanks @Timothy Jones
Copy code
async getUserSettingsStatus() {
    return axios
      .get(this.withPath("/api/1/user"), {
        headers: authHeader,
      })
      .then((r) => {
        return r.data;
      })
      .catch((error) => {
        if (error.response.status === 401) {
          return Promise.resolve(error.response.data);
        }
      });
  }
t
Your code has a bug in that it will swallow errors- you should re-throw an exception if the error is not what you were expecting
h
yeah I changed it later, thanks @Timothy Jones
m
Given this question was literally asked in the previous post and does come up a bit, I’ve created a little guide in our troubleshooting doc for this: https://github.com/pact-foundation/pact-js/blob/master/docs/troubleshooting.md#how-do-i-test-negative-scenarios-such-as-400