Skip to content

Commit

Permalink
test: add test for parseHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
SettingDust committed Dec 6, 2022
1 parent c7fd8ff commit a67299d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/utilities.ts
Expand Up @@ -218,10 +218,12 @@ export function getChildNodes(node: HtmlNode | Node): (Node | HtmlNode)[] {
}
}

/* istanbul ignore next */
export function perfStart(label: string) {
if (process.env.LOG_PERF) console.time(label);
}

/* istanbul ignore next */
export function perfStop(label: string) {
if (process.env.LOG_PERF) console.timeEnd(label);
}
Expand Down
16 changes: 0 additions & 16 deletions test/e2e.test.ts

This file was deleted.

26 changes: 26 additions & 0 deletions test/parse-html.test.ts
@@ -0,0 +1,26 @@
import { parseHTML } from '../src/utilities';
import { defaultOptions } from '../src/config';
import { DOMParser } from 'linkedom';


describe('parseHTML', () => {
test('should parse HTML with native parser in browser', () => {
__IS_BROWSER__ = true;
globalThis.DOMParser = <typeof globalThis.DOMParser>DOMParser;
const html = '<div>test</div>';
const parsedHtml = parseHTML(html, { ...defaultOptions, preferNativeParser: true });
expect(parsedHtml).toBeDefined();
__IS_BROWSER__ = false;
globalThis.DOMParser = <typeof globalThis.DOMParser><unknown>undefined;
});
test('should parse HTML in node when preferNativeParser is true', () => { // This test fails
const html = '<div>test</div>';
const parsedHtml = parseHTML(html, { ...defaultOptions, preferNativeParser: true });
expect(parsedHtml).toBeDefined();
});
test('should parse HTML in node when preferNativeParser is false', () => {
const html = '<div>test</div>';
const parsedHtml = parseHTML(html, defaultOptions);
expect(parsedHtml).toBeDefined();
});
});

0 comments on commit a67299d

Please sign in to comment.