jsdom-global icon indicating copy to clipboard operation
jsdom-global copied to clipboard

Using localStorage and sessionStorage

Open tomprats opened this issue 6 years ago • 1 comments

I just ran into an issue where a library I'm using uses sessionStorage, so I'm getting sessionStorage is not defined in my tests. Is there any plan to support these types of storages in jsdom-global?

After some research I noticed there is a PR in jsdom that's pretty related. Would jsdom-global then just need to put them in the global scope like it does document and window?

https://github.com/jsdom/jsdom/pull/2076

tomprats avatar Jun 14 '18 20:06 tomprats

It seems there is no way to send the required url parameter into jsdom allowing mocha tests to use localstorage. This has been a hard requirement since jsdom v11.12.0 (6 months now)

Can we get an update on this?

Edit: Minimal test case reproduction in Mocha. We should have some mechanism to set url config so the following passes:

mocha --exit --require ts-node/register --require jsdom-global/register ./app/test/*.ts

  @test public async sessionStorate() {
    try {
      if(window && window.localStorage) {
        //have storage
        assert.ok("does not break control flow");
      } else {
        //doesnt have storage
        assert.ok("does not break control flow");
      }
    } catch {
      assert.fail("breaks control flow");
    }
  }

KyleGalvin avatar Nov 17 '18 18:11 KyleGalvin

Probably not relevant anymore with more recent versions

tomprats avatar Jun 21 '23 18:06 tomprats