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.

[FEATURE] toggle document.hidden in jsdom

See original GitHub issue

Basic info:

  • Node.js version: 8.12.0
  • jsdom version: 12.2.0

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.

I am using jest which uses jsdom.

Minimal reproduction case

I need a way to toggle document.hidden in jsdom in order to test code that has different code paths depending on the value.

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()
})

Ref: https://github.com/facebook/jest/issues/7142

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
kapouercommented, Oct 11, 2018

@dotnetCarpenter try something like

Object.defineProperty(document, "hidden", {
  configurable: true,
  get: function() { return whatever; }
});
1reaction
princefishthrowercommented, Nov 13, 2021

Super old thread here, but is there a way to mock visibilityState? Would using the same form of @kapouer 's solution work?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Document.hidden - Web APIs - MDN Web Docs
The Document.hidden read-only property returns a Boolean value indicating if the page is considered hidden or not.
Read more >
How to Make Simple Feature Flags in React - Webtips
Feature flags — or feature toggles — is a technique where you can provide some means to turn on and off certain features...
Read more >
Use Scroll and Width in JSDOM - Stack Overflow
It's default is to set things like document.hidden to indicate this, ... Jest has the ability to temporarily switch out the environment used ......
Read more >
9. Feature Toggle - Programming JavaScript Applications [Book]
Feature Toggle Continuous deployment is the process of testing, integrating ... You can hide features with CSS or by wrapping the lines of...
Read more >
Media queries in React for responsive design - Material UI - MUI
Some of the key features: ... It's performant, it observes the document to detect when its media queries ... For instance, jsdom doesn't...
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