Skip to main content
Best answer

Issues writing unit test for z.cursor


Hello, 

 

I am trying to write a unit test on a trigger that calls z.cursor and the first issue I ran into was I need to set environment variables for ZAPIER_DEPLOY_KEY after doing that I get the following error:

 

only absolute urls are supported

which is referring to z.cursor.get() . Im guessing this is due to the fact that the function is using relative pathing but I am unsure how to move forward. Is there a way to mock out this call so that the test can continue?

zapier-platform-core@8.0.1

Best answer by ikbelkirasanBest answer by ikbelkirasan

Hi @Raman - If you’re using Jest to run your tests, try mocking the z.cursor.get and z.cursor.set functions before calling the trigger’s perform function. Here’s an example showing how I do it:

// ...

const originalPerform = App.triggers["your_trigger_key"].operation.perform;

let cursor = "";
App.triggers["your_trigger_key"].operation.perform = (z, bundle) => {
  jest.spyOn(z.cursor, "set").mockImplementation(async (value) => {
    cursor = value;
    return null;
  });
  jest.spyOn(z.cursor, "get").mockImplementation(async () => cursor);

  return originalPerform(z, bundle);
};

const response = await appTester(App.triggers["your_trigger_key"].operation.perform, bundle);
// ...

 

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • Answer
  • June 24, 2021

Hi @Raman - If you’re using Jest to run your tests, try mocking the z.cursor.get and z.cursor.set functions before calling the trigger’s perform function. Here’s an example showing how I do it:

// ...

const originalPerform = App.triggers["your_trigger_key"].operation.perform;

let cursor = "";
App.triggers["your_trigger_key"].operation.perform = (z, bundle) => {
  jest.spyOn(z.cursor, "set").mockImplementation(async (value) => {
    cursor = value;
    return null;
  });
  jest.spyOn(z.cursor, "get").mockImplementation(async () => cursor);

  return originalPerform(z, bundle);
};

const response = await appTester(App.triggers["your_trigger_key"].operation.perform, bundle);
// ...

 


  • Author
  • Beginner
  • 1 reply
  • June 24, 2021

@ikbelkirasan  Perfect, that solution was exactly what I needed. Thank you!