question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Config option for setting pretendToBeVisual

See original GitHub issue

🚀 Feature Proposal

An option to set jsdom pretendToBeVisual inside test cases.

Motivation

I need to test code that set a timestamp value depending on document.hidden. jsdom toggles the value depending on pretendToBeVisual as documented in the jsdom README.

Currently pretendToBeVisual appears to be set on test start and not changeable from within a single test.

I see that I can set a custom environment configuration documentation.

Example

To test the following function getTimeDuration:

/**
 * First call starts a timer and returns a function
 * that when called will give the elapsed time in
 * milisecond, while subtracting "hidden" time.
 * @returns {function} Calling this function will return the elapsed time.
 */
export default function getTimeDuration () {
  const startTimestamp = Date.now()
  let hiddenTimeDelta = 0
  let hiddenTimeStart = 0

  document.addEventListener('visibilitychange', trackHiddenTime)

  function trackHiddenTime () {
    if (document.hidden) {
      hiddenTimeStart = Date.now()
    } else {
      hiddenTimeDelta += (Date.now() - hiddenTimeStart)
    }
  }

  return () => {
    document.removeEventListener('visibilitychange', trackHiddenTime)

    return (Date.now() - hiddenTimeDelta) - startTimestamp
  }
}

We could write the following test code:

test('visibilitychange duration used by PayloadTiming', () => {
  // jestDateMock.advanceTo(0) is
  // needed due to issue: https://github.com/hustcc/jest-date-mock/issues/15
  jestDateMock.advanceTo(0)

  const timer = getTimeDuration()

  jestDateMock.advanceBy(10) // visible state

  jsdom.pretendToBeVisual = false // hidden state
  document.dispatchEvent(new Event('visibilitychange'))
  jestDateMock.advanceBy(10)
  
  jsdom.pretendToBeVisual = true // visible state
  document.dispatchEvent(new Event('visibilitychange'))
  jestDateMock.advanceBy(10)

  expect(timer()).toBe(20)
  jestDateMock.clear()
})

Pitch

Currently, I do not see any way to test the Page Visibility API in jest without this option. Regardless of the actual name. I’m not attached in any way to jsdom.pretendToBeVisual.

Please let me know if this change has to be done upstream in jsdom before it can be available in jest or if there is work-around possible in how jest utilitizes jsdom.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

6reactions
dotnetCarpentercommented, Oct 11, 2018

For future readers who find this thread.

The simple and correct way to set document.hidden in jest and jsdom is to add the following to your test.

let hidden = true
Object.defineProperty(document, "hidden", {
  configurable: true,
  get () { return hidden },
  set (bool) { hidden = Boolean(bool) }
})

In effect you will shadow document.hidden. A declined patch that also sets visibilityState can be found here: https://github.com/jsdom/jsdom/pull/2392

0reactions
github-actions[bot]commented, May 12, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration - Quokka.js
To add Quokka configuration options for your project, you may add the .quokka JSON file in your project and add Quokka settings directly...
Read more >
Configuring Jest
The bail config option can be used here to have Jest stop running tests after n failures. Setting bail to true is the...
Read more >
Testing frameworks integration - Cloudscape Design System
We recommend using our configuration preset for tests involving our components. ... Create a setup.js file and load it to Mocha using the...
Read more >
Check if element is visible in DOM in Node.js - Stack Overflow
I tried to create dom structure with pretendToBeVisual: true option and dom.window.document.hidden returns false but it didn't change ...
Read more >
global-jsdom | Yarn - Package Manager
... update semver for jsdom in peerDeps (fixes #245) https://github.com/modosc/global-jsdom/pull/259; update actions/checkout@v3, actions/setup-node@v3, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found