2

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: "Contact@test.com",
        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);
  });
});

Akash Jain
  • 894
  • 2
  • 10
  • 23

1 Answers1

2

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.

xavdid
  • 5,092
  • 3
  • 20
  • 32
  • When I am using this code inside `/creates/contact.js` file it works but in test case file `/test/contact.js`, the response is an API content object it doesn't have any status or headers. Is there any issue with appTester() function? – Akash Jain Sep 03 '19 at 06:25
  • ah, I had mixed up what you were asking. i've updated my answer. – xavdid Sep 03 '19 at 17:06