Skip to content

Jest tests fail - SecurityError: localStorage is not available for opaque origins #1424

@bigtunacan

Description

@bigtunacan

To reproduce just generate a new project using vue init webpack my-project and choose Jest as the test suite. Then run npm run unit and it will fail with error

SecurityError: localStorage is not available for opaque origins
at Window.get localStorage [as localStorage]
(node_modules/jsdom/lib/jsdom/browser/Window.js:257:15)
at Array.forEach ()

This appears related to this issue with jsdom jsdom/jsdom#2304

For now there is a workaround by locking jsdom to version 11.11.0

Activity

shayneo

shayneo commented on Aug 3, 2018

@shayneo

Same issue here, was able to resolve by adding
testEnvironment: 'node' to jest.config.js

scriada

scriada commented on Aug 12, 2018

@scriada

Based on the link in @bigtunacan 's original comment I was able to resolve this by adding:

testURL: 'http://localhost'

to the jest.conf.js file.

I also noticed that this filename doesn't match the default jest config filename (should be test.config.js) so jest won't pick it up without an explicit --config argument.

agirorn

agirorn commented on Feb 8, 2019

@agirorn

When I added testEnvironment: 'node' as @shayneo suggested to a new project. I got the tests running but the following error.

> vue-webpack@1.0.0 test /home/agirorn/code-play/vue-webpack                                                                                                                                                                                  
> npm run unit && npm run e2e                                                                                                                                                                                                                 
                                                                                                                                                                                         
> vue-webpack@1.0.0 unit /home/agirorn/code-play/vue-webpack                                                                                                                                                                                  
> jest --config test/unit/jest.conf.js --coverage                                                                                                                                                                                             
                                                                                                                                                                                                                                              
● Deprecation Warning:                                                                                                                                                                                                                        

  Option "mapCoverage" has been removed, as it's no longer necessary.                                                                                                                                                                         

  Please update your configuration.

  Configuration Documentation:
  https://facebook.github.io/jest/docs/configuration.html

 FAIL  test/unit/specs/HelloWorld.spec.js
  HelloWorld.vue
    ✕ should render correct contents (14ms)

  ● HelloWorld.vue › should render correct contents

    TypeError: Cannot read property 'querySelector' of undefined

       6 |     const Constructor = Vue.extend(HelloWorld);
       7 |     const vm = new Constructor().$mount();
    >  8 |     expect(vm.$el.querySelector('.hello h1').textContent)
       9 |       .toEqual('Welcome to Your Vue.js App');
      10 |   });
      11 | });

      at Object.querySelector (test/unit/specs/HelloWorld.spec.js:8:19)

But when I add the testURL: 'http://localhost' as @scriada suggest everything runs fine.

linked a pull request that will close this issue on Feb 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @agirorn@scriada@shayneo@bigtunacan

      Issue actions

        Jest tests fail - SecurityError: localStorage is not available for opaque origins · Issue #1424 · vuejs-templates/webpack