When writing your frontend tests, having a “fake” Tauri environment to simulate windows or intercept IPC calls is common, so-called mocking.
The @tauri-apps/api/mocks module provides some helpful tools to make this easier for you:
Most commonly, you want to intercept IPC requests; this can be helpful in a variety of situations:
Ensure the correct backend calls are made
Simulate different results from backend functions
Tauri provides the mockIPC function to intercept IPC requests. You can find more about the specific API in detail here.
Sometimes you want to track more information about an IPC call; how many times was the command invoked? Was it invoked at all?
You can use mockIPC() with other spying and mocking tools to test this:
To mock IPC requests to a sidecar or shell command you need to grab the ID of the event handler when spawn() or execute() is called and use this ID to emit events the backend would send back:
Sometimes you have window-specific code (a splash screen window, for example), so you need to simulate different windows.
You can use the mockWindows() method to create fake window labels. The first string identifies the “current” window (i.e., the window your JavaScript believes itself in), and all other strings are treated as additional windows.