Test and Debug Functions

This section describes how to test and validate your Function before deploying and using it.

Run Your Function Locally

As you're developing your Function you'll want to run it locally on your development workstation to validate your Function is working properly. You'll eventually also want to validate your Function after it's deployed, but it's easier to catch bugs early on using local invocation.

For more details on how to run and invoke a Function locally, see Invoke a Function.

Test Your Function

When you create a new Function in your project, a default test file is generated as well. Depending on the language you specified when creating the Function, the test file will use some default test frameworks. You are free to use your preferred testing frameworks instead, if needed.

When writing tests for your Function, you might need to mock code that accesses your org. Frameworks like Sinon or Jest (for JavaScript) and Mokito (for Java) may help when writing mocks.

For examples of tests using mocking, see the test folders for the various Functions in the Functions recipe samples. Some examples:

See also any additional language-specific test considerations in the following docs:

Automate Testing

If you've added your Functions project to github, you can connect CI/CD solutions to your github repo to automate testing when changes are made.

For beta, support for integrating Functions into a CI/CD system is limited. CLI commands that require authentication to compute environments for running or deploying Functions can't be run as part of an automated CI/CD system yet.

Testing Your Apex Code

You'll want to ensure your Apex code that invokes Functions is properly tested. Functions cannot be invoked in an Apex test. A “Function invocations from Apex tests are not supported” exception is thrown if Apex determines that a Function is being invoked during a test. To test Apex code that invokes a Function, you’ll need to mock your Function invocations during the tests. See FunctionInvocation Example Implementation for an example of a mocked FunctionInvocation that you can use in tests.

For general information on writing Apex tests, see Apex Testing.

Debug Your Function

You can debug your Function using VS Code, or logs. If you're using VS Code with the Salesforce Extensions for VS Code installed, you can use extension commands to make it easy to debug local running Functions.

For debugging Java Functions, you may need to do some additional configuration in your VS Code. See Java Functions for more details.

To debug your Function in VS Code, start your Function, set breakpoints in your Function code, create or open a payload JSON file, and click Debug Invoke. This invokes the Function and attaches the VS Code debugger to the Function container.

See Invoke a Function for more details on how to start a Function locally using VS Code.

Use Logs to Debug

Salesforce Functions generate run time log information that can be accessed via the Salesforce CLI or Functions Manager. You can generate custom log lines in your Function code that help you debug your Function. See Function Logging for more details.