Skip to content

[bug] Unable to mock events in 2.6.0 #13741

@masiama

Description

@masiama

Describe the bug

In 2.6.0 callbacks were moved from window['_{cb}'] to window.__TAURI_INTERNALS__.runCallback. I use mockIPC to handle add and remove event listeners events, and save handlers into to array. Then, in the test, I emit events just by calling all the callbacks in that array. However, there is no runCallback in window.__TAURI_INTERNALS__.

Reproduction

const ipcListeners: Record<string, ((payload: unknown) => void)[]> = {};

const emitEvent = <T extends keyof EventPayload>(name: T, payload: EventPayload[T]) => {
  const listeners = ipcListeners[name];
  assert(listeners !== undefined, `No listeners found for "${name}" event`);
  listeners.forEach((listener) => listener({ payload }));
};

mockIPC((cmd, args) => {
  if (
    cmd === "plugin:event|listen" &&
    args &&
    "event" in args &&
    typeof args.handler === "number" &&
    "event" in args &&
    typeof args.event === "string"
  ) {
    const eventCallbackId = `_${args.handler}` as keyof typeof window;
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    const listener: (payload: unknown) => void = window[eventCallbackId];
    ipcListeners[args.event] = [...(ipcListeners[args.event] ?? []), listener];
  }
  if (
    cmd === "plugin:event|unlisten" &&
    args &&
    "event" in args &&
    typeof args.event === "string"
  ) {
    delete ipcListeners[args.event];
  }
});

In the frontend, I have

useEffect(() => {
  const unlistener = events.runUpdate.listen(({ payload }) => {});
  return unlisten(unlistener);
}, []);

In the test, I have

emitEvent("RunUpdate", { });

Expected behavior

Previously, I was just saving const listener: (payload: unknown) => void = window[eventCallbackId]; to an array of listeners. Now, AFAIU, callbacks are not saved to window directly but should be called by window.__TAURI_INTERNALS__.runCallback. However, window.__TAURI_INTERNALS__ has only invoke and transformCallback.

Full tauri info output

In frontend folder


[✔] Environment
    - OS: Mac OS 15.5.0 arm64 (X64)
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.86.0 (05f9846f8 2025-03-31)
    ✔ cargo: 1.86.0 (adf9b6ad1 2025-02-28)
    ✔ rustup: 1.28.2 (e4f3ad6f8 2025-04-28)
    ✔ Rust toolchain: 1.86-aarch64-apple-darwin (overridden by environment variable RUSTUP_TOOLCHAIN)
    - node: 22.17.0
    - yarn: 3.6.3
    - npm: 11.4.2

[-] Packages
    - tauri-cli 🦀: 2.6.2
    - @tauri-apps/api : 2.6.0
    - @tauri-apps/cli : 2.6.2

[-] Plugins

[-] App


In root

WARNING: no lock files found, defaulting to npm

[✔] Environment
    - OS: Mac OS 15.5.0 arm64 (X64)
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.86.0 (05f9846f8 2025-03-31)
    ✔ cargo: 1.86.0 (adf9b6ad1 2025-02-28)
    ✔ rustup: 1.28.2 (e4f3ad6f8 2025-04-28)
    ✔ Rust toolchain: 1.86-aarch64-apple-darwin (overridden by environment variable RUSTUP_TOOLCHAIN)
    - node: 22.17.0
    - pnpm: 9.14.2
    - yarn: 1.22.22
    - npm: 11.4.2

[-] Packages
    - tauri 🦀: 2.5.1, (outdated, latest: 2.6.2)
    - tauri-build 🦀: 2.2.0, (outdated, latest: 2.3.0)
    - wry 🦀: 0.51.2, (outdated, latest: 0.52.1)
    - tao 🦀: 0.33.0, (outdated, latest: 0.34.0)
    - tauri-cli 🦀: 2.6.2
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli : 2.6.2

[-] Plugins
    - tauri-plugin-opener 🦀: 2.3.1, (outdated, latest: 2.4.0)
    - @tauri-apps/plugin-opener : not installed!
    - tauri-plugin-fs 🦀: 2.3.0, (outdated, latest: 2.4.0)
    - @tauri-apps/plugin-fs : not installed!
    - tauri-plugin-clipboard-manager 🦀: 2.2.3, (outdated, latest: 2.3.0)
    - @tauri-apps/plugin-clipboard-manager : not installed!
    - tauri-plugin-dialog 🦀: 2.2.2, (outdated, latest: 2.3.0)
    - @tauri-apps/plugin-dialog : not installed!

[-] App
    - build-type: build
    - CSP: default-src 'self'; img-src 'self' data:; script-src 'wasm-unsafe-eval'; frame-src 'self' asset: https://asset.localhost
    - frontendDist: ../../frontend/dummy

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions