跳到内容
Tauri

mocks

函数

clearMocks()

function clearMocks(): void

清除此模块中其他函数注入的模拟函数/数据。当使用不为每个测试提供全新窗口对象的测试运行器时,调用此函数将重置 tauri 特定属性。

示例

import { mockWindows, clearMocks } from "@tauri-apps/api/mocks"
afterEach(() => {
clearMocks()
})
test("mocked windows", () => {
mockWindows("main", "second", "third");
expect(window.__TAURI_INTERNALS__).toHaveProperty("metadata")
})
test("no mocked windows", () => {
expect(window.__TAURI_INTERNALS__).not.toHaveProperty("metadata")
})

返回值

void

始于

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/mocks.ts#L211


mockConvertFileSrc()

function mockConvertFileSrc(osName): void

模拟 convertFileSrc 函数

参数

参数类型描述
osNamestring要模拟的操作系统,可以是 linux、macos 或 windows 之一

返回值

void

示例

import { mockConvertFileSrc } from "@tauri-apps/api/mocks";
import { convertFileSrc } from "@tauri-apps/api/core";
mockConvertFileSrc("windows")
const url = convertFileSrc("C:\\Users\\user\\file.txt")

始于

1.6.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/mocks.ts#L172


mockIPC()

function mockIPC(cb): void

使用给定的模拟处理程序拦截所有 IPC 请求。

此函数可在测试 tauri 前端应用程序或在静态站点生成期间在 Node.js 上下文中运行前端时使用。

示例

使用 vitest 进行测试设置

import { mockIPC, clearMocks } from "@tauri-apps/api/mocks"
import { invoke } from "@tauri-apps/api/core"
afterEach(() => {
clearMocks()
})
test("mocked command", () => {
mockIPC((cmd, payload) => {
switch (cmd) {
case "add":
return (payload.a as number) + (payload.b as number);
default:
break;
}
});
expect(invoke('add', { a: 12, b: 15 })).resolves.toBe(27);
})

回调函数也可以返回 Promise

import { mockIPC, clearMocks } from "@tauri-apps/api/mocks"
import { invoke } from "@tauri-apps/api/core"
afterEach(() => {
clearMocks()
})
test("mocked command", () => {
mockIPC((cmd, payload) => {
if(cmd === "get_data") {
return fetch("https://example.com/data.json")
.then((response) => response.json())
}
});
expect(invoke('get_data')).resolves.toBe({ foo: 'bar' });
})

参数

参数类型
cb(cmd, payload?) => unknown

返回值

void

始于

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/mocks.ts#L64


mockWindows()

function mockWindows(current, ..._additionalWindows): void

模拟一个或多个窗口标签。在非 tauri 上下文中,需要在使用 @tauri-apps/api/window 模块之前调用此函数。

此函数仅模拟窗口的存在,窗口属性(例如宽度和高度)可以使用 mockIPC 函数像常规 IPC 调用一样进行模拟。

示例

import { mockWindows } from "@tauri-apps/api/mocks";
import { getCurrentWindow } from "@tauri-apps/api/window";
mockWindows("main", "second", "third");
const win = getCurrentWindow();
win.label // "main"
import { mockWindows } from "@tauri-apps/api/mocks";
mockWindows("main", "second", "third");
mockIPC((cmd, args) => {
if (cmd === "plugin:event|emit") {
console.log('emit event', args?.event, args?.payload);
}
});
const { emit } = await import("@tauri-apps/api/event");
await emit('loaded'); // this will cause the mocked IPC handler to log to the console.

参数

参数类型描述
currentstring此 JavaScript 上下文正在运行的窗口标签。
_additionalWindowsstring[]-

返回值

void

始于

1.0.0

来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/mocks.ts#L143


© 2025 Tauri 贡献者。CC-BY / MIT