As you write Functions, you will likely need to inspect the state of your execution to fix issues with code correctness or performance. Below are features you can use to do this. Note that these debugging steps also apply to unit tests.
Use the debugger tool in Code Repositories to examine the behavior of your unit test while it runs. Set breakpoints to pause the execution of the unit test in order to examine variables, and understand functions and libraries.
To use the debugger, you need to set breakpoints. These breakpoints indicate the specific points where the debugger should pause the code execution, enabling you to interact with variables.
Set a breakpoint by selecting the faded red dot in the margins of each line of code. The debugger suspends the execution before the marked line runs. You can set multiple breakpoints across several files, if needed.
After adding breakpoints in your code, select Run and debug, located in the functions panel.
After adding breakpoints in your code, select Run test, located next to the unit test in the code editor.
Once the debugger has started, the debugger panel will open and pause on the first breakpoint it encounters. The left bar of the debugger allows you to navigate the code, remove breakpoints, and finish or stop the debugging session.
As you navigate the code, the editor highlights the line of code to be executed next. Use the following buttons to advance the debugger:
While the debugger is running, you can examine the variables and data at the exact point of code execution.
Frames represent the functions in which the debugger is active or in which breakpoints exist. Each frame indicates the name of the function followed by the name of the file and the line number in which the function is written.
Select a frame to examine the variables within that frame and run console commands against it.
The variables section displays the values stored in both local and global variables while the transform is executed.
The console allows you to interact with your data using JavaScript console commands while running the debugger.
Note that the console operates within the context of the selected frame. Attempting to execute commands on variables local to a different frame will lead to an error.
Functions supports emitting console logs during execution for debugging purposes. To do so, simply use the console.log
command to emit logs. For example:
Copied!1 2 3 4 5 6 7
@Function() public testConsoleLogging(n: Integer): Integer { for (let i = 0; i < n; i++) { console.log(`Iteration ${i}`); } return n; }
Using console logs in this way can be useful for debugging correctness issues. You can also add console logs to identify performance bottlenecks in your code. See the guide for optimizing performance for more information on how to improve the performance of link traversal logic.
When you run a Function using the Tests helper in Authoring, console logs will be captured and displayed below:
When you run a Function using the Functions helper in Authoring, console logs will be captured and displayed below, along with timestamps: