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
Testing your function on Shopify is a form of exploratory testing and the best way to confirm the function's expected behavior when it's used in a store.
When you run [`app dev`](/docs/api/shopify-cli/app/app-dev), Shopify CLI streams execution logs for your functions to your terminal, and writes details of the function execution input, output, and more to your filesystem. You can use the production-like function input values for other testing methods.
The [`app logs`](/docs/api/shopify-cli/app/app-logs) command can also be used to stream logs for a development store. This command provides additional capabilities such as log filtering and JSON output.
Additionally, all function runs for development stores are logged in your [Partner Dashboard](https://partners.shopify.com/current/apps). Manual steps are required on the store to trigger the function.
### Test your function on a development store
1. If you're developing a function in a language other than JavaScript or TypeScript, ensure you have configured `build.watch` in your [function extension configuration](/docs/api/functions/configuration#properties).
1. Use the Shopify CLI [`dev` command](/docs/api/shopify-cli/app/app-dev) to start app preview:
You can keep the 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.
1. Follow the CLI prompts to preview your app, and install it on your development store.
1. 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](/docs/apps/build/functions#getting-started) for details.
1. Add debugging logs to your function by writing to `eprintln!` in Rust, `console.log` in JavaScript, or `STDERR` in other languages.
> Caution:
> You should always remove debugging logs before deploying and releasing your function.
1. When your function executes, review your debug logs in the `app dev` output.
1. To review execution details, either click `Open log file` in the terminal output, or navigate to the output file path, depending on your terminal's capabilities.
> Tip:
> Windows Terminal users can enable hyperlinks in Shopify CLI by setting the `FORCE_HYPERLINK=1` environment variable.
1. To stream detailed logs for a single function, open a new terminal window and run `app logs` with the `--source` argument:
> Tip:
> You can list log sources for your app with the [`app logs sources`](/docs/api/shopify-cli/app/app-logs-sources) command.
## Execute the function locally using Shopify CLI
Shopify CLI can mimic production execution of your function's WebAssembly module. This allows for faster local testing of function output, measuring your function against [function performance restrictions](/docs/apps/build/functions#limitations-and-considerations). You can use this for local or automated integration tests in WebAssembly and to measure function performance.
The [`app function replay`](/docs/api/shopify-cli/app/app-function-replay) command quickly executes the function using input from a previously logged function execution, and re-executes as your make changes to your code. The [`app function run`](/docs/api/shopify-cli/app/app-function-run) command can be used to perform a single function execution using a provided JSON input, so you can test function logic locally for known or logged input values. You can pass in any JSON input.
> Tip
> You can use the [`--json` argument](/docs/api/shopify-cli/app/app-function-run) of `app function run` to get function measurements and output as JSON. This output can be used in automated testing and scripts.
### Replay a function locally
1. In the output of `app dev`, copy the 6-character log file identifier for the function execution which you wish to replay. For example, `9f1f0e`.
1. Open a separate terminal window and `cd` to your function extension folder.
1. Use the `--log` argument with `app function replay` to specify the execution to run:
> Tip:
> If you don't have a log file identifier, you can use `app function replay` without this argument and select from a list of recent executions.
1. After your function has the desired output, you can [re-test on your development store](#test-your-function-on-a-development-store).
If you're still running `app dev`, then your changes should already be available for testing on Shopify.
## Writing unit tests for functions
We recommend implementing automated testing during development based on known use cases.
Writing unit tests allows you to validate your function logic repeatedly as your code evolves. Unit tests are also useful for debugging, because they enable step debugging using the native tooling of your development stack.
For examples, refer to some [sample apps](https://github.com/Shopify/function-examples/tree/main/sample-apps).
> Caution:
> Unit tests don't run in WebAssembly, which in rare cases might cause different results than what's found in production.
### Write unit tests for functions
You can retrieve a valid input JSON result for your [input query](/docs/apps/build/functions/input-output#input) from the [log files output by `app dev`](#test-your-function-on-a-development-store), the output of the [`app logs`](/docs/api/shopify-cli/app/app-logs) command, function run logs in your [Partner Dashboard](https://partners.shopify.com/current/apps), or by constructing your own mock input. You can use this JSON to write tests which test your function as a single unit.
The recommended tool for writing unit tests depends on your choice of [programming language](/docs/apps/build/functions/programming-languages) for your function:
* For Rust, the `shopify_function` crate provides a [`run_function_with_input`](https://docs.rs/shopify_function/latest/shopify_function/fn.run_function_with_input.html) utility method to simplify unit testing with `cargo test`. [View an example](https://github.com/Shopify/function-examples/blob/main/sample-apps/discounts/extensions/product-discount-rust/src/run.rs#L77-L199).
* For JavaScript or TypeScript, we recommend [Vitest](https://vitest.dev/). Input JSON can be used directly in your JavaScript code to execute the function. [View an example](https://github.com/Shopify/function-examples/blob/main/sample-apps/discounts/extensions/product-discount-js/src/run.test.js).
For complex functions, you might design your code in a way that allows you to break up your tests into multiple units.
## Debugging functions in production
For production stores, all function runs are visible in your Partner Dashboard, but details are only available for runs that have been [shared by users](/docs/apps/build/functions/monitoring-and-errors). If your function execution is failing, or if your function is not behaving as intended, 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.