forked from adobecom/gmii-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-test-runner.config.js
79 lines (76 loc) · 2.97 KB
/
web-test-runner.config.js
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
import { importMapsPlugin } from '@web/dev-server-import-maps';
import { defaultReporter } from '@web/test-runner';
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({})],
reporters: [
defaultReporter({ reportTestResults: true, reportTestProgress: true }),
customReporter(),
],
testRunnerHtml: (testFramework) => `
<html>
<head>
<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/814#discussioncomment-6060759 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/814#discussioncomment-6060759 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>`,
};