Skip to content

Commit

Permalink
test: upd tests pollyfills (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v authored Oct 24, 2024
1 parent 930404e commit 0c388e8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 45 deletions.
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@
"sass-loader": "^13.3.2",
"stylelint": "15.11.0",
"ts-jest": "^27.0.7",
"typescript": "^4.5.2",
"web-streams-polyfill": "4.0.0"
"typescript": "^4.5.2"
},
"peerDependenciesMeta": {
"@diplodoc/folding-headings-extension": {
Expand Down
30 changes: 30 additions & 0 deletions tests/pollyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable @typescript-eslint/no-redeclare */
import {Blob} from 'node:buffer';
import {ReadableStream} from 'node:stream/web';
import {TextDecoder, TextEncoder} from 'node:util';

Object.assign(global, {Blob, ReadableStream, TextDecoder, TextEncoder});

// fix from https://github.com/jsdom/jsdom/issues/3002
document.createRange = () => {
const range = new Range();
range.getBoundingClientRect = () => ({
x: 0,
y: 0,
width: 0,
height: 0,
top: 0,
right: 0,
bottom: 0,
left: 0,
toJSON() {
return JSON.stringify(this);
},
});
range.getClientRects = () => ({
length: 0,
item: () => null,
[Symbol.iterator]: jest.fn(),
});
return range;
};
33 changes: 1 addition & 32 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,2 @@
import {TextDecoder as TextDecoderUtil, TextEncoder as TextEncoderUtil} from 'util';

import {ReadableStream as ReadableStreamPolyfill} from 'web-streams-polyfill';

import './pollyfills';
import './toMatchNode';

global.TextDecoder = TextDecoderUtil as any; // TS error in global.TextDecoder
global.TextEncoder = TextEncoderUtil;
global.ReadableStream = ReadableStreamPolyfill;

// fix from https://github.com/jsdom/jsdom/issues/3002
document.createRange = () => {
const range = new Range();
range.getBoundingClientRect = () => ({
x: 0,
y: 0,
width: 0,
height: 0,
top: 0,
right: 0,
bottom: 0,
left: 0,
toJSON() {
return JSON.stringify(this);
},
});
range.getClientRects = () => ({
length: 0,
item: () => null,
[Symbol.iterator]: jest.fn(),
});
return range;
};

0 comments on commit 0c388e8

Please sign in to comment.