Skip to content

Commit fdb7ec9

Browse files
committed
fix: Fix coverage runs in sandboxed test env
1 parent 80017cf commit fdb7ec9

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

core/src/__tests__/index.test.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,65 @@ import { render, fireEvent, screen } from '@testing-library/react';
66
// import '@testing-library/jest-dom';
77
import CodeMirror, { ReactCodeMirrorRef } from '..';
88

9+
// Setup JSDOM mocks for CodeMirror
10+
beforeAll(() => {
11+
// Mock Range.getClientRects
12+
Object.defineProperty(global.Range.prototype, 'getClientRects', {
13+
writable: true,
14+
value: jest.fn(() => ({
15+
length: 1,
16+
item: () => ({ bottom: 16, height: 16, left: 0, right: 100, top: 0, width: 100 }),
17+
[Symbol.iterator]: function* () {
18+
yield { bottom: 16, height: 16, left: 0, right: 100, top: 0, width: 100 };
19+
},
20+
})),
21+
});
22+
23+
Object.defineProperty(global.Range.prototype, 'getBoundingClientRect', {
24+
writable: true,
25+
value: jest.fn(() => ({ bottom: 16, height: 16, left: 0, right: 100, top: 0, width: 100 })),
26+
});
27+
28+
// Mock observers used by CodeMirror internals in JSDOM.
29+
class MockIntersectionObserver {
30+
observe = jest.fn();
31+
unobserve = jest.fn();
32+
disconnect = jest.fn();
33+
takeRecords = jest.fn(() => []);
34+
}
35+
class MockResizeObserver {
36+
observe = jest.fn();
37+
unobserve = jest.fn();
38+
disconnect = jest.fn();
39+
}
40+
class MockMutationObserver {
41+
observe = jest.fn();
42+
disconnect = jest.fn();
43+
takeRecords = jest.fn(() => []);
44+
}
45+
global.IntersectionObserver = MockIntersectionObserver as unknown as typeof IntersectionObserver;
46+
global.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
47+
global.MutationObserver = MockMutationObserver as unknown as typeof MutationObserver;
48+
window.IntersectionObserver = global.IntersectionObserver;
49+
window.ResizeObserver = global.ResizeObserver;
50+
window.MutationObserver = global.MutationObserver;
51+
52+
// Mock element properties
53+
Object.defineProperty(global.HTMLElement.prototype, 'clientHeight', { get: () => 100 });
54+
Object.defineProperty(global.HTMLElement.prototype, 'clientWidth', { get: () => 500 });
55+
Object.defineProperty(global.HTMLElement.prototype, 'offsetHeight', { get: () => 100 });
56+
Object.defineProperty(global.HTMLElement.prototype, 'offsetWidth', { get: () => 500 });
57+
Object.defineProperty(global.Element.prototype, 'scrollIntoView', { writable: true, value: jest.fn() });
58+
59+
// Suppress CodeMirror JSDOM warnings
60+
const originalError = console.error;
61+
console.error = (...args) => {
62+
if (args[0]?.toString?.().includes('getClientRects') || args[0]?.toString?.().includes('observe is not a function'))
63+
return;
64+
originalError.apply(console, args);
65+
};
66+
});
67+
968
it('CodeMirror', async () => {
1069
const component = renderer.create(<CodeMirror />);
1170
let tree = component.toJSON();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"doc": "npm run-script build --workspace www",
99
"start": "npm run-script start --workspace www",
1010
"⬆️⬆️⬆️⬆️⬆️ package ⬆️⬆️⬆️⬆️⬆️": "▲▲▲▲▲ package ▲▲▲▲▲",
11-
"test": "lerna exec \"tsbb test\" --scope @uiw/react-codemirror",
12-
"coverage": "lerna exec \"tsbb test --coverage --bail\" --scope @uiw/react-codemirror",
11+
"test": "lerna exec \"tsbb test --no-watchman\" --scope @uiw/react-codemirror",
12+
"coverage": "lerna exec \"tsbb test --coverage --bail --no-watchman\" --scope @uiw/react-codemirror --scope @uiw/codemirror-extensions-langs",
1313
"prepare": "husky",
1414
"version": "lerna version --exact --force-publish --no-push --no-git-tag-version",
1515
"prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,json}'",

0 commit comments

Comments
 (0)