Testing your hook

Cloudhooks provides a hook testing facility.
For testing, you can press the "Run test" button on the "Hook" tab of your hook.

The hook's code along with the test payload are sent to the test runner, and the run logs are sent back.

Tips for improving your testing experience

Branching the hook code based on run mode

The run mode is injected into the context of the hook, so the hook code can branch based on run mode.

As an example, you can use 3rd-party test APIs in test mode:

module.exports = async function (payload, actions, context) {
  const api = context.isTestMode ? 
                'https://dev.myapi.com' : 
                'https://myapi.com';

  const { data } = await actions.http.post(api, { a: 1 });
}

The Shopify API is always in live mode

Be aware that calling Shopify APIs in test mode do make changes in Shopify - there is no way to mock Shopify APIs.

There are some ways around it:

  1. Use identifiers reserved for testing purposes in your test payloads.
  2. Branch your code based on run mode, and use reserved identifiers in test mode.
  3. Create a development store where you can call the Shopify API as you see fit.

What to do if your hook times out

Hook test runs time out after 30 seconds. Most timeouts are related to mishandling promises.

All actions return a promise, so make sure that:

  1. Wait for the promise to complete.
    You can use the await operator or the then() -style handling of promises as well.
  2. Catch errors and handle them.
    Unhandled promise errors bubble up on the call stack, and crash the test runner.


Use logging to mark and debug

Mark important data and debug the hook with log entries.