Skip to content

Commit

Permalink
test(hydration): ignore expected console warnings (#4649)
Browse files Browse the repository at this point in the history
Fixes #4577
  • Loading branch information
nolanlawson authored Oct 17, 2024
1 parent 0d268d8 commit 6900f06
Showing 1 changed file with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ async function getCompiledModule(dirName) {
return { code, watchFiles };
}

function throwOnUnexpectedConsoleCalls(runnable) {
// The console is shared between the VM and the main realm. Here we ensure that known warnings
// are ignored and any others cause an explicit error.
const methods = ['error', 'warn', 'log', 'info'];
const originals = {};
for (const method of methods) {
originals[method] = console[method];
console[method] = function (error) {
if (
method === 'warn' &&
/Cannot set property "(inner|outer)HTML"/.test(error?.message)
) {
return;
}

throw new Error(`Unexpected console.${method} call: ${error}`);
};
}
try {
runnable();
} finally {
Object.assign(console, originals);
}
}

function getSsrCode(moduleCode, testConfig) {
const script = new vm.Script(
`
Expand All @@ -110,8 +135,10 @@ function getSsrCode(moduleCode, testConfig) {
moduleOutput = LWC.renderComponent('x-${COMPONENT_UNDER_TEST}-${guid++}', Main, config.props || {});`
);

vm.createContext(context);
script.runInContext(context);
throwOnUnexpectedConsoleCalls(() => {
vm.createContext(context);
script.runInContext(context);
});

return context.moduleOutput;
}
Expand Down

0 comments on commit 6900f06

Please sign in to comment.