Repository navigation
Expand file tree
/
Copy pathweb-test-runner.config.js
More file actions
142 lines (138 loc) · 5.61 KB
/
Copy pathweb-test-runner.config.js
File metadata and controls
142 lines (138 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// eslint-disable-next-line import/no-extraneous-dependencies
import { importMapsPlugin } from '@web/dev-server-import-maps';
// eslint-disable-next-line import/no-extraneous-dependencies
import { defaultReporter } from '@web/test-runner';
function axTreePlugin() {
return {
name: 'ax-tree',
async executeCommand({ command, payload, session }) {
if (command !== 'ax-tree') return undefined;
const page = session.browser.getPage(session.id);
const root = await page.$(payload.selector);
return root && page.accessibility.snapshot({ interestingOnly: false, root });
},
};
}
function customReporter() {
return {
async reportTestFileResults({ logger, sessionsForTestFile }) {
sessionsForTestFile.forEach((session) => {
session.testResults.tests.forEach((test) => {
if (!test.passed && !test.skipped) {
logger.log(test);
}
});
});
},
};
}
export default {
coverageConfig: {
exclude: [
'**/mocks/**',
'**/node_modules/**',
'**/test/**',
'**/deps/**',
],
},
plugins: [
importMapsPlugin(
{
inject: {
importMap: {
imports: {
'da-y-wrapper': '/deps/da-y-wrapper/dist/index.js',
'da-lit': '/deps/lit/dist/index.js',
'da-parser': '/deps/da-parser/dist/index.js',
'hast-util-from-html': '/test/fixtures/hast-stub.js',
},
},
},
},
),
axTreePlugin(),
],
reporters: [
defaultReporter({ reportTestResults: true, reportTestProgress: true }),
customReporter(),
],
testRunnerHtml: (testFramework) => `
<html>
<head>
<script>
// WTR clones every message with structuredClone before sending it. A failed assertion
// whose actual/expected holds a function or DOM node makes that throw, so the result
// is never reported and the run hangs until testsFinishTimeout. For WTR's own messages
// only, fall back to a lossy copy so the failure is reported instead.
(() => {
const nativeClone = window.structuredClone;
const toCloneable = (value, seen) => {
if (typeof value === 'function') return '[Function ' + (value.name || 'anonymous') + ']';
if (typeof value !== 'object' || value === null) return value;
if (value === window) return '[Window]';
if (value instanceof Node) return '[' + value.nodeName + ']';
if (value instanceof Error) return { name: value.name, message: value.message, stack: value.stack };
if (seen.has(value)) return seen.get(value);
try { return nativeClone.call(window, value); } catch (e) { /* has uncloneable parts */ }
const copy = Array.isArray(value) ? [] : {};
seen.set(value, copy);
Object.keys(value).forEach((key) => {
copy[key] = toCloneable(value[key], seen);
});
return copy;
};
window.structuredClone = function structuredClone(value, options) {
try {
return nativeClone.call(window, value, options);
} catch (e) {
const fromRunner = String(new Error().stack).includes('__web-dev-server__web-socket');
if (e.name !== 'DataCloneError' || !fromRunner) throw e;
return toCloneable(value, new WeakMap());
}
};
})();
</script>
<script type='module'>
const oldFetch = window.fetch;
window.fetch = async (resource, options) => {
if (!resource.startsWith('/') && !resource.startsWith('http://localhost')) {
console.error(
'** fetch request for an external resource is disallowed in unit tests, please find a way to mock! https://github.com/orgs/adobecom/discussions/127 provides guidance on how to fix the issue.',
resource
);
}
return oldFetch.call(window, resource, options);
};
const oldXHROpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = async function (...args) {
let [method, url, asyn] = args;
if (!resource.startsWith('/') && url.startsWith('http://localhost')) {
console.error(
'** XMLHttpRequest request for an external resource is disallowed in unit tests, please find a way to mock! https://github.com/orgs/adobecom/discussions/127 provides guidance on how to fix the issue.',
url
);
}
return oldXHROpen.apply(this, args);
};
const observer = new MutationObserver((mutationsList, observer) => {
for(let mutation of mutationsList) {
if (mutation.type === 'childList') {
for(let node of mutation.addedNodes) {
if(node.nodeName === 'SCRIPT' && node.src && !node.src.startsWith('http://localhost')) {
console.error(
'** An external 3rd script has been added. This is disallowed in unit tests, please find a way to mock! https://github.com/orgs/adobecom/discussions/814#discussioncomment-6060891 provides guidance on how to fix the issue.',
node.src
);
}
}
}
}
});
observer.observe(document.head, { childList: true });
</script>
</head>
<body>
<script type='module' src='${testFramework}'></script>
</body>
</html>`,
};