Skip to content

Commit 03da508

Browse files
committed
fix: add ResizeObserver mock for Cypress
1 parent afd9de5 commit 03da508

3 files changed

Lines changed: 58 additions & 11 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class ResizeObserverMock {
2+
constructor(callback) {
3+
this.callback = callback;
4+
this.elements = new Set();
5+
this.handleResize = this.handleResize.bind(this);
6+
}
7+
8+
observe(element) {
9+
this.elements.add(element);
10+
this.trigger();
11+
window.addEventListener('resize', this.handleResize);
12+
}
13+
14+
unobserve(element) {
15+
this.elements.delete(element);
16+
}
17+
18+
disconnect() {
19+
this.elements.clear();
20+
window.removeEventListener('resize', this.handleResize);
21+
}
22+
23+
handleResize() {
24+
this.trigger();
25+
}
26+
27+
trigger() {
28+
if (this.elements.size == 0) return;
29+
30+
const entries = Array.from(this.elements).map(e => ({
31+
target: e,
32+
contentRect: e.getBoundingClientRect(),
33+
}));
34+
35+
requestAnimationFrame(() => {
36+
this.callback(entries, this);
37+
});
38+
}
39+
}
40+
41+
export default ResizeObserverMock;

cypress/support/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
* See https://github.com/cypress-io/cypress/issues/20341
44
* See https://github.com/cypress-io/cypress/issues/29277
55
*/
6-
Cypress.on('uncaught:exception', err => {
7-
if (
8-
err.message.includes(
9-
'ResizeObserver loop completed with undelivered notifications'
10-
)
11-
) {
12-
return false;
13-
}
6+
7+
import ResizeObserverMock from './ResizeObserverMock';
8+
9+
Cypress.on('window:before:load', win => {
10+
win.ResizeObserver = ResizeObserverMock;
1411
});

cypress/tsconfig.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4-
"rootDir": "e2e"
4+
"rootDir": ".",
5+
"noEmit": true
56
},
6-
"types": ["cypress", "node"]
7-
}
7+
"types": [
8+
"cypress",
9+
"node"
10+
],
11+
"include": [
12+
"**/*.ts",
13+
"**/*.js",
14+
"cypress.config.js"
15+
]
16+
}

0 commit comments

Comments
 (0)