Skip to content

Commit

Permalink
test: add explicit test for broken plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ramboz committed Jan 16, 2025
1 parent 8964f80 commit 8ee7b04
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
11 changes: 4 additions & 7 deletions modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const PLUGINS = {
// Martech
martech: { url: `${pluginBasePath}/martech.js`, when: ({ urlParameters }) => urlParameters.size > 0 },
onetrust: { url: `${pluginBasePath}/onetrust.js`, when: () => (document.querySelector('#onetrust-consent-sdk') || hasCookieKey('OptanonAlertBoxClosed')), isBlockDependent: true },
// test: broken-plugin
};

const PLUGIN_PARAMETERS = {
Expand All @@ -59,16 +60,12 @@ function loadPlugin(key, params) {
return null;
}
if (!pluginCache.has(key)) {
try {
pluginCache.set(key, import(`${plugin.url || plugin}`));
/* c8 ignore next 3 */
} catch (e) {
return null;
}
pluginCache.set(key, import(`${plugin.url || plugin}`));
}
const pluginLoadPromise = pluginCache.get(key);
return pluginLoadPromise
.then((p) => (p.default && p.default(params)) || (typeof p === 'function' && p(params)));
.then((p) => (p.default && p.default(params)) || (typeof p === 'function' && p(params)))
.catch(() => { /* silent plugin error catching */ });
}

function loadPlugins(filter = () => true, params = PLUGIN_PARAMETERS) {
Expand Down
62 changes: 62 additions & 0 deletions test/it/broken-plugin.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<html lang="en-NZ">

<head>
<title>Test Runner</title>
</head>

<body>
<p>Some content</p>
<script type="module">
import { runTests } from '@web/test-runner-mocha';
import { sendMouse } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';

runTests(async () => {
describe('HTML Index Tests with broken Plugins', () => {

it('can load rum enhancer as a script', async () => {
let called = [];
window.hlx = {
rum: {
sampleRUM: (...args) => {
called.push(args);
}
}
};


const script = document.createElement('script');
script.src = new URL('/modules/index-broken.js', window.location).href;
script.type = 'module';
document.head.appendChild(script);

await new Promise((resolve) => {
script.onload = resolve;
});
// wait one second for the script to run
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});

// click anywhere
await sendMouse({ type: 'click', position: [10, 10] });

// the order is not stable across browsers, so we
// sort called by the first argument
called.sort((a, b) => a[0].localeCompare(b[0]));

expect(called).to.have.lengthOf(3);
expect(called[0][0]).to.equal('click');

expect(called[1][0]).to.equal('enter');

expect(called[2][0]).to.equal('language');
expect(called[2][1].source).to.equal('en-NZ');
});

});
});
</script>
</body>

</html>
9 changes: 9 additions & 0 deletions web-test-runner.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export default {
// rewriting dynamic plugins base url
.replace(/document\.currentScript\.src/g, '"http://localhost:8000/plugins"');
return true;
} else if (context.url.startsWith('/modules/index-broken.js')) {
const [_, search] = context.url.split('?');
context.url = `/modules/index.js?${search}`;
await next();
context.body = context.body
// rewriting dynamic plugins base url
.replace(/document\.currentScript\.src/g, '"http://localhost:8000/plugins"')
.replace(/\/\/ test: broken-plugin/g, 'foo: "foo.js",');
return true;
} else if (context.url.startsWith('/.rum')) {
if (context.url.startsWith('/.rum/@adobe/helix-rum-js@%5E2/dist/')
|| context.url.startsWith('/.rum/@adobe/helix-rum-js@^2/dist/')
Expand Down

0 comments on commit 8ee7b04

Please sign in to comment.