Testing and debugging practices for Shopify Functions
This guide describes recommended practices for testing and debugging when you are developing functions. Depending on your needs, you will use some combination of testing on Shopify, local execution, and unit tests.
Testing on your development store
Anchor link to section titled "Testing on your development store"Testing your function on Shopify is the best way to ensure it has the desired behavior when used in a store. When functions are run on a development store, all function runs are logged in your Partner Dashboard.
Usage | Advantages | Limitations |
---|---|---|
|
|
|
Test your function on a development store
Anchor link to section titled "Test your function on a development store"If you're developing a function in a language other than JavaScript, TypeScript, or Rust, ensure you have configured
build.watch
in your function settings.Use the Shopify CLI
dev
command to start app preview:You can keep the this preview running as you work on your function. When you make changes to a watched file, Shopify CLI rebuilds your function and updates the function extension's drafts, so you can immediately test your changes.
Follow the CLI prompts to preview your app, and install it on your development store.
Enable the function and test it on your development store.
The steps that you need to follow to test your function depend on the function API that you're implementing. Refer to the Shopify Functions use case tutorials for details.
Add debugging output to your function by writing to
STDERR
. For example, useconsole.error
in JavaScript, oreprintln!
in Rust.To debug, review function logs in the Partner Dashboard:
- In the Partner Dashboard, navigate to Apps > {your app} > Extensions > {your function}.
- Click on any function run to view its input, output, and logs.
Execute the function locally using the function runner
Anchor link to section titled "Execute the function locally using the function runner"The function runner mimics production execution of your function's WebAssembly module. You can access the function runner using the Shopify CLI app function run
command. The function runner is useful for fast local testing, and measuring your function against function performance restrictions.
Usage | Advantages | Limitations |
---|---|---|
|
|
|
Test a function using the function runner
Anchor link to section titled "Test a function using the function runner"- Retrieve a valid input JSON result for your input query from function run logs in your Partner Dashboard, or construct your own mock input.
- Open a terminal in your function extension folder.
Pipe the input JSON into the
app function run
command. For example, you mightecho
the JSON directly, or paste it into a file first, andcat
the file as input.
Writing unit tests for functions
Anchor link to section titled "Writing unit tests for functions"Writing unit tests allows you to validate your function logic on an ongoing basis as your code evolves. Unit tests are also useful for debugging, because they enable step debugging using the native tooling of your development stack.
Usage | Advantages | Limitations |
---|---|---|
|
|
|
Write unit tests for functions
Anchor link to section titled "Write unit tests for functions"You can retrieve a valid input JSON result for your input query from function run logs in your Partner Dashboard, or construct your own mock input. You can use this JSON to write tests which test your function as a single unit:
- For JavaScript or TypeScript, we recommend Vitest. Input JSON can be used directly in your JavaScript code to execute the function. View an example.
- For Rust, the
shopify_function
crate provides arun_function_with_input
utility method to simplify unit testing withcargo test
. View an example.
For complex functions, you might design your code in a way that allows you break up your tests into multiple units.
Debugging functions in production
Anchor link to section titled "Debugging functions in production"For production stores, only function failure logs shared by users are available in your Partner Dashboard. If your function execution is failing, you should request that the user share those logs. You can also reproduce the issue in a development store, or one of the local testing options.