Test and Debug Functions
This section describes how to test and validate your function before deploying and using it.
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 Local Function.
When you create a 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:
- Test for Context_SalesforceSDK_JS JavaScript Function
- Test for Context_SalesforceSDK_Java Java Function
- Example of Mocking and Testing UnitOfWork in JavaScript
- Example of Mocking and Testing UnitOfWork in Java
See also any additional language-specific test considerations in the following docs:
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.
You'll want to ensure your Apex code that invokes your 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. Use the FunctionInvokeMock Interface and the MockFunctionInvocationFactory Class to test Apex code that invokes a function. 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.
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.