Skip to content

Commit

Permalink
fix(api): do nothing in clearMocks if __TAURI_INTERNALS__ is not defi…
Browse files Browse the repository at this point in the history
…ned (#8071)
  • Loading branch information
G07cha authored Oct 21, 2023
1 parent c6c59cf commit 287066b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-clearmocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tauri-apps/api": 'patch:bug'
---

No longer crashing in tests without mocks when `clearMocks` is defined in `afterEach` hook.
13 changes: 9 additions & 4 deletions tooling/api/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ export function mockWindows(
*
* @since 1.6.0
*/
export function mockConvertFileSrc(
osName: string
): void {
export function mockConvertFileSrc(osName: string): void {
window.__TAURI_INTERNALS__ = window.__TAURI_INTERNALS__ ?? {}
window.__TAURI_INTERNALS__.convertFileSrc = function (filePath, protocol = 'asset') {
window.__TAURI_INTERNALS__.convertFileSrc = function (
filePath,
protocol = 'asset'
) {
const path = encodeURIComponent(filePath)
return osName === 'windows'
? `http://${protocol}.localhost/${path}`
Expand Down Expand Up @@ -191,6 +192,10 @@ export function mockConvertFileSrc(
* @since 1.0.0
*/
export function clearMocks(): void {
if (typeof window.__TAURI_INTERNALS__ !== 'object') {
return
}

// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
delete window.__TAURI_INTERNALS__.convertFileSrc
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
Expand Down

0 comments on commit 287066b

Please sign in to comment.