Skip to content

Commit a67299d

Browse files
committed
test: add test for parseHTML
1 parent c7fd8ff commit a67299d

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

src/utilities.ts

+2
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,12 @@ export function getChildNodes(node: HtmlNode | Node): (Node | HtmlNode)[] {
218218
}
219219
}
220220

221+
/* istanbul ignore next */
221222
export function perfStart(label: string) {
222223
if (process.env.LOG_PERF) console.time(label);
223224
}
224225

226+
/* istanbul ignore next */
225227
export function perfStop(label: string) {
226228
if (process.env.LOG_PERF) console.timeEnd(label);
227229
}

test/e2e.test.ts

-16
This file was deleted.

test/parse-html.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { parseHTML } from '../src/utilities';
2+
import { defaultOptions } from '../src/config';
3+
import { DOMParser } from 'linkedom';
4+
5+
6+
describe('parseHTML', () => {
7+
test('should parse HTML with native parser in browser', () => {
8+
__IS_BROWSER__ = true;
9+
globalThis.DOMParser = <typeof globalThis.DOMParser>DOMParser;
10+
const html = '<div>test</div>';
11+
const parsedHtml = parseHTML(html, { ...defaultOptions, preferNativeParser: true });
12+
expect(parsedHtml).toBeDefined();
13+
__IS_BROWSER__ = false;
14+
globalThis.DOMParser = <typeof globalThis.DOMParser><unknown>undefined;
15+
});
16+
test('should parse HTML in node when preferNativeParser is true', () => { // This test fails
17+
const html = '<div>test</div>';
18+
const parsedHtml = parseHTML(html, { ...defaultOptions, preferNativeParser: true });
19+
expect(parsedHtml).toBeDefined();
20+
});
21+
test('should parse HTML in node when preferNativeParser is false', () => {
22+
const html = '<div>test</div>';
23+
const parsedHtml = parseHTML(html, defaultOptions);
24+
expect(parsedHtml).toBeDefined();
25+
});
26+
});

0 commit comments

Comments
 (0)