Răsfoiți Sursa

fix(api): avoid crashing in `clearMocks`, closes #8179 (#8219)

* fix(api): do nothing in clearMocks if __TAURI_INTERNALS__ is not defined (fix: #8179)

Prevents tests that didn't define any mocks from failing in `afterEach` test hook.

* Update fix-clearmocks.md

* check for indvidiual properties instead
Merlin Gough 1 an în urmă
părinte
comite
14544e4b87
2 a modificat fișierele cu 8 adăugiri și 3 ștergeri
  1. 5 0
      .changes/fix-clearmocks.md
  2. 3 3
      tooling/api/src/mocks.ts

+ 5 - 0
.changes/fix-clearmocks.md

@@ -0,0 +1,5 @@
+---
+"@tauri-apps/api": 'patch:bug'
+---
+
+Avoid crashing in `clearMocks`

+ 3 - 3
tooling/api/src/mocks.ts

@@ -202,9 +202,9 @@ export function mockConvertFileSrc(
  */
 export function clearMocks(): void {
   // @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
-  delete window.__TAURI__.convertFileSrc
+  if (window.__TAURI__?.convertFileSrc) delete window.__TAURI__.convertFileSrc
   // @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
-  delete window.__TAURI_IPC__
+  if (window.__TAURI_IPC__) delete window.__TAURI_IPC__
   // @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
-  delete window.__TAURI_METADATA__
+  if (window.__TAURI_METADATA__) delete window.__TAURI_METADATA__
 }