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.

Provide a way to specify test environment setup file for Jest

See original GitHub issue

My App is using localStorage but when I run tests, I get this error:

ReferenceError: localStorage is not defined

Should we provide a polyfill to be able to mock it with Jest ?

Workaround

Add a --setupTestFrameworkScriptFile ./localStoragePolyfill.js to the test command in package.json, where ./localStoragePolyfill looks like this:

const localStorageMock = (() => {
  let store = {}
  return {
    getItem(key) {
      return store[key]
    },
    setItem(key, value) {
      store[key] = value.toString()
    },
    clear() {
      store = {}
    }
  };
})()

global.localStorage = localStorageMock

I guess it should be added to config/polyfills.js.

I can PR if that’s fine. Not sure if we should use a simple inline polyfill or use something like node-localstorage though.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:17 (16 by maintainers)

github_iconTop GitHub Comments

4reactions
gaearoncommented, Sep 2, 2016

This makes sense. I propose the following: if src/setupTests.js exists it should be used, otherwise we don’t pass it. This logic would live in createJestConfig.js. Path itself would be determined in paths.js.

@gaelduplessix Would you like to submit a PR implementing this?

3reactions
gaearoncommented, Sep 2, 2016

Thanks for fixing this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
Configuring Jest. The Jest philosophy is to work great by default, but sometimes you just need more configuration power.
Read more >
Configuring package.json · Jest
Configuring package.json. Jest's configuration can be defined in the package.json file of your project or through the --config <path/to/json> option.
Read more >
Using .env files for unit testing with jest - Stack Overflow
if you are like me trying to find a way to change the dotenv config path to use .env.test instead of .env ,...
Read more >
Run/Debug Configuration: Jest | IntelliJ IDEA Documentation
In this area, specify the tests to be executed. The available options are: All tests: choose this option to run all the tests...
Read more >
Unit and snapshot test initial setup and configuration - IBM
Use the provided files to easily configure a default Jest and Enzyme test environment that you can use to start writing your unit...
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