So backstop.js provides ability to run custom script against underlying engine. I use puppeteer as an engine so I try to mock Date.now
with 'onReadyScript':
page.evaluate('window.Date.now = () => 0; Date.now = () => 0;');
...
page.addScriptTag({
// btw `console.log` here is not executed, do I use it in wrong way?
content: 'Date.now = () => 0;'
});
...
page.evaluate(() => {
window.Date.now = () => 0;
Date.now = () => 0;
});
Last one, I think, is modifying Date in context of Node, not inside the puppeteer, but anyway tried that as well.
Nothing worked, script under the test still output real Date.now
. Also I checked Override the browser date with puppeteer but it did not help me.
Yes, I know I'm able to skip particular selectors, but it does not always make sense(think about clock with arrows).
After trying
onBeforeScript
withevaluateOnNewDocument()
it works for me. Complete script:Reason:
onReadyScript
is executed when page under testing has already been loaded and executed. So code is bound to originalDate
by closure, not the mocked version.