Zum Inhalt springen

Mock Tauri APIs

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

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.

import { beforeAll, expect, test } from 'vitest';
import { randomFillSync } from 'crypto';
import { mockIPC } from '@tauri-apps/api/mocks';
import { invoke } from '@tauri-apps/api/core';
// jsdom doesn't come with a WebCrypto implementation
beforeAll(() => {
Object.defineProperty(window, 'crypto', {
value: {
// @ts-ignore
getRandomValues: (buffer) => {
return randomFillSync(buffer);
},
},
});
});
test('invoke simple', async () => {
mockIPC((cmd, args) => {
// simulated rust command called "add" that just adds two numbers
if (cmd === 'add') {
const { a, b } = args as { a: number; b: number };
return a + b;
}
});
});

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:

import { beforeAll, expect, test, vi } from 'vitest';
import { randomFillSync } from 'crypto';
import { mockIPC } from '@tauri-apps/api/mocks';
import { invoke } from '@tauri-apps/api/core';
// jsdom doesn't come with a WebCrypto implementation
beforeAll(() => {
Object.defineProperty(window, 'crypto', {
value: {
// @ts-ignore
getRandomValues: (buffer) => {
return randomFillSync(buffer);
},
},
});
});
test('invoke', async () => {
mockIPC((cmd, args) => {
// simulated rust command called "add" that just adds two numbers
if (cmd === 'add') {
const { a, b } = args as { a: number; b: number };
return a + b;
}
});
// we can use the spying tools provided by vitest to track the mocked function
const spy = vi.spyOn(window.__TAURI_INTERNALS__, 'invoke');
expect(invoke('add', { a: 12, b: 15 })).resolves.toBe(27);
expect(spy).toHaveBeenCalled();
});

To mock a sidecar or shell command, handle the commands the shell plugin invokes: execute() resolves with the process output, while spawn() receives a channel you can use to send the events the backend would emit:

import { mockIPC } from '@tauri-apps/api/mocks';
import type { Channel } from '@tauri-apps/api/core';
mockIPC(async (cmd, args) => {
if (cmd === 'plugin:shell|execute') {
return {
code: 0,
signal: null,
stdout: 'some data sent from the process',
stderr: '',
};
}
if (cmd === 'plugin:shell|spawn') {
const { onEvent } = args as { onEvent: Channel<unknown> };
// 'Stdout' event can be called multiple times
onEvent.onmessage({
event: 'Stdout',
payload: 'some data sent from the process',
});
// 'Terminated' event must be called at the end to resolve the promise
onEvent.onmessage({
event: 'Terminated',
payload: {
code: 0,
signal: null,
},
});
// the process id
return 1;
}
});
Since 2.7.0

There is partial support of the Event System to simulate events emitted by your Rust code via the shouldMockEvents option:

import { mockIPC, clearMocks } from '@tauri-apps/api/mocks';
import { emit, listen } from '@tauri-apps/api/event';
import { afterEach, expect, test, vi } from 'vitest';
test('mocked event', () => {
mockIPC(() => {}, { shouldMockEvents: true }); // enable event mocking
const eventHandler = vi.fn();
listen('test-event', eventHandler);
emit('test-event', { foo: 'bar' });
expect(eventHandler).toHaveBeenCalledWith({
event: 'test-event',
payload: { foo: 'bar' },
});
});

emitTo and emit_filter are not supported yet.

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.

import { beforeAll, expect, test } from 'vitest';
import { randomFillSync } from 'crypto';
import { mockIPC, mockWindows } from '@tauri-apps/api/mocks';
// jsdom doesn't come with a WebCrypto implementation
beforeAll(() => {
Object.defineProperty(window, 'crypto', {
value: {
// @ts-ignore
getRandomValues: (buffer) => {
return randomFillSync(buffer);
},
},
});
});
test('invoke', async () => {
mockWindows('main', 'second', 'third');
// `getAllWebviewWindows()` asks the backend for the window list
mockIPC((cmd) => {
if (cmd === 'plugin:window|get_all_windows') {
return ['main', 'second', 'third'];
}
});
const { getCurrentWebviewWindow, getAllWebviewWindows } =
await import('@tauri-apps/api/webviewWindow');
expect(getCurrentWebviewWindow()).toHaveProperty('label', 'main');
expect((await getAllWebviewWindows()).map((w) => w.label)).toEqual([
'main',
'second',
'third',
]);
});

© 2026 Tauri Contributors. CC-BY / MIT