I have a global function in background page like so:
window.myfn = function(){
return new Promise((resolve, reject) => { stuff; });
};
I am using Jest and Puppeteer. Consider this in my test.js file:
async function getBackgroundPage() {
const targets = await browser.targets(),
backgroundPageTarget = targets.find(
target => target.type() === "background_page",
),
backgroundPage = await backgroundPageTarget.page();
return backgroundPage;
}
async function sendUpdate() {
const bgPage = await getBackgroundPage(),
argString = `() => window.myfn()`;
await bgPage.evaluateHandle(argString);
await sleep(30000);
}
getBackgroundPage is literally copied from the docs, so I hope it's correct. However, sendUpdate() does not work as expected. Ideally, window.myfn should be called but actually it is never called. I know this because:
- I put several
console.logs in it, so if it was called there should have been some log output. - The 30second sleep gives me enough time to goto
chrome://extensions, and open the background page console via Inspect Views, and I could not find any log outputs there.
There are no errors in both the background page, and the Jest console. What is the problem then? Why is that global function not being called?