Skip to content

Commit 287066b

Browse files
authored
fix(api): do nothing in clearMocks if __TAURI_INTERNALS__ is not defined (#8071)
1 parent c6c59cf commit 287066b

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

.changes/fix-clearmocks.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": 'patch:bug'
3+
---
4+
5+
No longer crashing in tests without mocks when `clearMocks` is defined in `afterEach` hook.

tooling/api/src/mocks.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ export function mockWindows(
152152
*
153153
* @since 1.6.0
154154
*/
155-
export function mockConvertFileSrc(
156-
osName: string
157-
): void {
155+
export function mockConvertFileSrc(osName: string): void {
158156
window.__TAURI_INTERNALS__ = window.__TAURI_INTERNALS__ ?? {}
159-
window.__TAURI_INTERNALS__.convertFileSrc = function (filePath, protocol = 'asset') {
157+
window.__TAURI_INTERNALS__.convertFileSrc = function (
158+
filePath,
159+
protocol = 'asset'
160+
) {
160161
const path = encodeURIComponent(filePath)
161162
return osName === 'windows'
162163
? `http://${protocol}.localhost/${path}`
@@ -191,6 +192,10 @@ export function mockConvertFileSrc(
191192
* @since 1.0.0
192193
*/
193194
export function clearMocks(): void {
195+
if (typeof window.__TAURI_INTERNALS__ !== 'object') {
196+
return
197+
}
198+
194199
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
195200
delete window.__TAURI_INTERNALS__.convertFileSrc
196201
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case

0 commit comments

Comments
 (0)