Skip to content

Commit 8ee7b04

Browse files
committed
test: add explicit test for broken plugin
1 parent 8964f80 commit 8ee7b04

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

modules/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const PLUGINS = {
4040
// Martech
4141
martech: { url: `${pluginBasePath}/martech.js`, when: ({ urlParameters }) => urlParameters.size > 0 },
4242
onetrust: { url: `${pluginBasePath}/onetrust.js`, when: () => (document.querySelector('#onetrust-consent-sdk') || hasCookieKey('OptanonAlertBoxClosed')), isBlockDependent: true },
43+
// test: broken-plugin
4344
};
4445

4546
const PLUGIN_PARAMETERS = {
@@ -59,16 +60,12 @@ function loadPlugin(key, params) {
5960
return null;
6061
}
6162
if (!pluginCache.has(key)) {
62-
try {
63-
pluginCache.set(key, import(`${plugin.url || plugin}`));
64-
/* c8 ignore next 3 */
65-
} catch (e) {
66-
return null;
67-
}
63+
pluginCache.set(key, import(`${plugin.url || plugin}`));
6864
}
6965
const pluginLoadPromise = pluginCache.get(key);
7066
return pluginLoadPromise
71-
.then((p) => (p.default && p.default(params)) || (typeof p === 'function' && p(params)));
67+
.then((p) => (p.default && p.default(params)) || (typeof p === 'function' && p(params)))
68+
.catch(() => { /* silent plugin error catching */ });
7269
}
7370

7471
function loadPlugins(filter = () => true, params = PLUGIN_PARAMETERS) {

test/it/broken-plugin.test.html

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<html lang="en-NZ">
2+
3+
<head>
4+
<title>Test Runner</title>
5+
</head>
6+
7+
<body>
8+
<p>Some content</p>
9+
<script type="module">
10+
import { runTests } from '@web/test-runner-mocha';
11+
import { sendMouse } from '@web/test-runner-commands';
12+
import { expect } from '@esm-bundle/chai';
13+
14+
runTests(async () => {
15+
describe('HTML Index Tests with broken Plugins', () => {
16+
17+
it('can load rum enhancer as a script', async () => {
18+
let called = [];
19+
window.hlx = {
20+
rum: {
21+
sampleRUM: (...args) => {
22+
called.push(args);
23+
}
24+
}
25+
};
26+
27+
28+
const script = document.createElement('script');
29+
script.src = new URL('/modules/index-broken.js', window.location).href;
30+
script.type = 'module';
31+
document.head.appendChild(script);
32+
33+
await new Promise((resolve) => {
34+
script.onload = resolve;
35+
});
36+
// wait one second for the script to run
37+
await new Promise((resolve) => {
38+
setTimeout(resolve, 1000);
39+
});
40+
41+
// click anywhere
42+
await sendMouse({ type: 'click', position: [10, 10] });
43+
44+
// the order is not stable across browsers, so we
45+
// sort called by the first argument
46+
called.sort((a, b) => a[0].localeCompare(b[0]));
47+
48+
expect(called).to.have.lengthOf(3);
49+
expect(called[0][0]).to.equal('click');
50+
51+
expect(called[1][0]).to.equal('enter');
52+
53+
expect(called[2][0]).to.equal('language');
54+
expect(called[2][1].source).to.equal('en-NZ');
55+
});
56+
57+
});
58+
});
59+
</script>
60+
</body>
61+
62+
</html>

web-test-runner.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ export default {
4646
// rewriting dynamic plugins base url
4747
.replace(/document\.currentScript\.src/g, '"http://localhost:8000/plugins"');
4848
return true;
49+
} else if (context.url.startsWith('/modules/index-broken.js')) {
50+
const [_, search] = context.url.split('?');
51+
context.url = `/modules/index.js?${search}`;
52+
await next();
53+
context.body = context.body
54+
// rewriting dynamic plugins base url
55+
.replace(/document\.currentScript\.src/g, '"http://localhost:8000/plugins"')
56+
.replace(/\/\/ test: broken-plugin/g, 'foo: "foo.js",');
57+
return true;
4958
} else if (context.url.startsWith('/.rum')) {
5059
if (context.url.startsWith('/.rum/@adobe/helix-rum-js@%5E2/dist/')
5160
|| context.url.startsWith('/.rum/@adobe/helix-rum-js@^2/dist/')

0 commit comments

Comments
 (0)