kentcdodds / testing-workshop

A workshop for learning how to test JavaScript applications
http://git.io/testing-workshop
1.36k stars 710 forks source link

Local Storage error when running tests #90

Closed saylerb closed 6 years ago

saylerb commented 6 years ago

Environment info:

Problem description: On fem-2018 branch. When I run the simple react tests, I get the error SecurityError: localStorage is not available for opaque origins.

Commands run: npm run setup

npm run react:test

Here is the output from running the test:

 FAIL   react  other/simple-react/__tests__/item-list.js
  ● Test suite failed to run

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

 FAIL   react  other/simple-react/__tests__/item-list.todo.js
  ● Test suite failed to run

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

Suggested solution:

Doing some googling around, I found this thread: https://github.com/jsdom/jsdom/issues/2304 Adding a testURL to jest.config.js seemed to fix the problem.

module.exports = {
  displayName: 'react',
  testURL: 'http://localhost',
}

Happy to open a PR for this, if this is something that is reproducible.

kentcdodds commented 6 years ago

A pull request would be welcome!

saylerb commented 6 years ago

@kentcdodds I just noticed this issues appears in the fem-2018 branch, but not master. Is that branch deprecated in favor of master?

kentcdodds commented 6 years ago

You can feel free to fix it in fem-2018. I want to avoid changing too much otherwise the course recording could be confusing, but this was a breaking change in jest that we should probably fix.