Search code examples
zapierzapier-cli

How to check response's status code in zapier's test cases


I am writing some code to test action in Zapier's CLI. I want to add one more condition here something like response.status == 200 or 201; to check API response code is 200 or 201.

How can I do it? when I log response it gives me whole JSON object that API is returning.

describe("contact create", () => {
  it("should create a contact", done => {
    const bundle = {
      inputData: {
        firstName: "Test",
        lastName: "Contact",
        email: "[email protected]",
        mobileNumber: "+12125551234",
        type: "contact"
      }
    };
    appTester(App.creates.contact.operation.perform, bundle)
      .then(response => {

        // Need one more condition whether response status is 200 or 201.

        response.should.not.be.an.Array();
        response.should.have.property('id');
        done();
      })
      .catch(done);
  });
});


Solution

  • appTester returns the result of the perform method, which isn't an API response. It's the data that's passed back into Zapier.

    The best thing to do is to add a line like this to your perform:

    // after your `z.request`
    if (!(response.status === 200 || response.status === 201)) {
      throw new Error('need a 200/201 response')
    }
    

    That will ensure you're getting exactly the response you want. But, more likely, you can add a response.throwForStatus() to make sure it's not an error code and not worry if it's exactly 200/201.