Skip to content

Commit 230dd24

Browse files
committed
fix(vitest): stub IntersectionObserver in teardown
1 parent c4c67d9 commit 230dd24

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/environments/vitest/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ export default <Environment>{
3030
}))
3131

3232
if (environmentOptions?.nuxt?.mock?.intersectionObserver) {
33-
win.IntersectionObserver ||= class IntersectionObserver {
34-
observe() {}
35-
unobserve() {}
36-
disconnect() {}
37-
takeRecords() { return [] }
38-
}
33+
win.IntersectionObserver ||= IntersectionObserver
3934
}
4035

4136
if (environmentOptions?.nuxt?.mock?.indexedDb) {
@@ -57,8 +52,21 @@ export default <Environment>{
5752
keys.forEach(key => delete global[key])
5853
teardownWindow()
5954
originals.forEach((v, k) => (global[k] = v))
55+
56+
// Stub to prevent errors from delayed callbacks
57+
if (!global.IntersectionObserver) {
58+
global.IntersectionObserver = IntersectionObserver
59+
}
60+
6061
teardown()
6162
},
6263
}
6364
},
6465
}
66+
67+
class IntersectionObserver {
68+
observe() {}
69+
unobserve() {}
70+
disconnect() {}
71+
takeRecords() { return [] }
72+
}

0 commit comments

Comments
 (0)