Skip to content

Commit 3971806

Browse files
ci(github): add workflow e2e.yml and run multiple browser tests
1 parent d30c67f commit 3971806

File tree

10 files changed

+71
-33
lines changed

10 files changed

+71
-33
lines changed

.github/workflows/e2e.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: e2e
2+
on: [push, pull_request]
3+
4+
permissions:
5+
contents: read
6+
7+
jobs:
8+
e2e:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
browser: [chromium, firefox, webkit]
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v6
17+
18+
- name: Use Node.js
19+
uses: actions/setup-node@v6
20+
with:
21+
cache: npm
22+
node-version-file: .nvmrc
23+
24+
- name: Install dependencies
25+
run: npm ci --prefer-offline
26+
27+
- name: Install Playwright browsers
28+
run: npx playwright install --with-deps ${{ matrix.browser }}
29+
30+
- name: Run browser tests on Chrome
31+
run: npx vitest run --config vitest.browser.config.mts --browser=${{ matrix.browser }}

.github/workflows/test.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,3 @@ jobs:
3030

3131
- name: Run module tests
3232
run: npm run test:esm
33-
34-
- name: Install Playwright browsers
35-
run: npx playwright install chromium
36-
37-
- name: Run browser tests
38-
run: npm run test:browser

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"prepublishOnly": "run-s build lint lint:tsc lint:package",
4747
"size-limit": "size-limit",
4848
"test": "vitest",
49-
"test:browser": "vitest run --config vitest.browser.config.mts",
49+
"test:browser": "vitest run --config vitest.browser.config.mts --browser=chromium",
5050
"test:coverage": "vitest run --color --coverage",
5151
"test:esm": "npm run build:esm && node --test test/esm"
5252
},

src/client/domparser.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ const HEAD_TAG_REGEX = /<head[^]*>/i;
1111
const BODY_TAG_REGEX = /<body[^]*>/i;
1212

1313
// falls back to `parseFromString` if `createHTMLDocument` cannot be used
14-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
14+
/* eslint-disable @typescript-eslint/no-unused-vars */
15+
/* istanbul ignore start */
1516
let parseFromDocument = (html: string, tagName?: string): Document => {
16-
/* istanbul ignore next */
1717
throw new Error(
1818
'This browser does not support `document.implementation.createHTMLDocument`',
1919
);
2020
};
2121

22-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2322
let parseFromString = (html: string, tagName?: string): Document => {
24-
/* istanbul ignore next */
2523
throw new Error(
2624
'This browser does not support `DOMParser.prototype.parseFromString`',
2725
);
2826
};
27+
/* istanbul ignore stop */
28+
/* eslint-enable @typescript-eslint/no-unused-vars */
2929

3030
const DOMParser = typeof window === 'object' && window.DOMParser;
3131

@@ -46,10 +46,11 @@ if (typeof DOMParser === 'function') {
4646
* @returns - Document.
4747
*/
4848
parseFromString = (html: string, tagName?: string): Document => {
49-
/* istanbul ignore if */
49+
/* istanbul ignore start */
5050
if (tagName) {
5151
html = `<${tagName}>${html}</${tagName}>`;
5252
}
53+
/* istanbul ignore stop */
5354

5455
return domParser.parseFromString(html, mimeType);
5556
};
@@ -74,7 +75,7 @@ if (typeof document === 'object' && document.implementation) {
7475
* @returns - Document
7576
*/
7677
parseFromDocument = function (html: string, tagName?: string): Document {
77-
/* istanbul ignore if */
78+
/* istanbul ignore start */
7879
if (tagName) {
7980
const element = htmlDocument.documentElement.querySelector(tagName);
8081

@@ -84,6 +85,7 @@ if (typeof document === 'object' && document.implementation) {
8485

8586
return htmlDocument;
8687
}
88+
/* istanbul ignore stop */
8789

8890
htmlDocument.documentElement.innerHTML = html;
8991
return htmlDocument;
@@ -114,8 +116,8 @@ if (template && template.content) {
114116
};
115117
}
116118

117-
/* istanbul ignore next */
118-
const createNodeList = () => document.createDocumentFragment().childNodes;
119+
const createNodeList = /* istanbul ignore next */ () =>
120+
document.createDocumentFragment().childNodes;
119121

120122
/**
121123
* Parses HTML string to DOM nodes.
@@ -171,11 +173,11 @@ export default function domparser(html: string): NodeList {
171173
return parseFromTemplate(html);
172174
}
173175

174-
/* istanbul ignore next */
176+
/* istanbul ignore start */
175177
const element = parseFromDocument(html, BODY).querySelector(BODY);
176178

177-
/* istanbul ignore next */
178179
return element?.childNodes ?? createNodeList();
180+
/* istanbul ignore stop */
179181
}
180182
}
181183
}

src/client/utilities.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,10 @@ export function formatDOM(
121121
}
122122

123123
case 3:
124-
/* istanbul ignore next */
125124
current = new Text(revertEscapedCharacters(node.nodeValue ?? ''));
126125
break;
127126

128127
case 8:
129-
/* istanbul ignore next */
130128
current = new Comment(node.nodeValue ?? '');
131129
break;
132130

test/cases/html.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isNode } from '../helpers';
2+
13
export default [
24
// html tags
35
{
@@ -224,8 +226,8 @@ export default [
224226
name: 'noscript with p',
225227
data: '<noscript><p>JS is disabled</p></noscript>',
226228
get skip() {
227-
// jsdom template renders noscript children as text instead of nodes
228-
return typeof process !== 'undefined';
229+
// template renders noscript children as text instead of nodes
230+
return isNode() || /firefox/i.test(navigator.userAgent);
229231
},
230232
},
231233

test/client/index.test.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import htmlToDOM from '../../src/client/html-to-dom';
33
import htmlCases from '../cases/html';
44
import {
5+
isBrowser,
56
parseDOM,
67
runTests,
78
testCaseSensitiveTags,
@@ -15,16 +16,18 @@ describe('client parser', () => {
1516
runTests(htmlToDOM, parseDOM, htmlCases);
1617
testCaseSensitiveTags(htmlToDOM);
1718

18-
describe('performance', () => {
19-
it('executes 1000 times in less than 50ms', () => {
20-
let times = 1000;
21-
const start = performance.now();
22-
while (--times) {
23-
htmlToDOM('<div>test</div>');
24-
}
25-
const end = performance.now();
26-
const elapsed = end - start;
27-
expect(elapsed).below(50);
19+
if (isBrowser()) {
20+
describe('performance', () => {
21+
it('executes 1000 times in less than 50ms', () => {
22+
let times = 1000;
23+
const start = performance.now();
24+
while (--times) {
25+
htmlToDOM('<div>test</div>');
26+
}
27+
const end = performance.now();
28+
const elapsed = end - start;
29+
expect(elapsed).below(50);
30+
});
2831
});
29-
});
32+
}
3033
});

test/helpers/environment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const isBrowser = () => typeof window === 'object' && !isNode();
2+
3+
export const isNode = () => typeof process === 'object';

test/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { isBrowser, isNode } from './environment';
12
export { parseDOM } from './parse-dom';
23
export { runTests } from './run-tests';
34
export { testCaseSensitiveTags } from './test-case-sensitive-tags';

vitest.browser.config.mts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export default mergeConfig(
1212
enabled: true,
1313
provider: playwright(),
1414
headless: true,
15-
instances: [{ browser: 'chromium' }],
15+
instances: [
16+
{ browser: 'chromium' },
17+
{ browser: 'firefox' },
18+
{ browser: 'webkit' },
19+
],
1620
},
1721
include: ['test/client/**'],
1822
},

0 commit comments

Comments
 (0)