Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to specify test environment setup file for Jest #545

Closed
gaelollivier opened this issue Sep 2, 2016 · 17 comments
Closed

Provide a way to specify test environment setup file for Jest #545

gaelollivier opened this issue Sep 2, 2016 · 17 comments
Milestone

Comments

@gaelollivier
Copy link
Contributor

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.

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

Is there any reason you can't do this manually by importing a file like this from your test? Is it because many tests rely on this?

@cpojer What do you think is a reasonable solution? Should we come up with a conventional name for test setup file?

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

I definitely don't want to add mocks for this in CRA because there are hundreds of browser APIs people might need to mock in different ways. This should be controlled by the user.

@cpojer
Copy link
Contributor

cpojer commented Sep 2, 2016

Yeah, I think we should come up with a standard for CRA and if the file exists, it gets added to setupFiles, just like we talked about.

For localStorage mocking, it probably needs a mock that uses ES proxies :)

@gaelollivier
Copy link
Contributor Author

@gaearon I can indeed directly import the polyfill just before importing my file in the test. However, because localStorage is in the global scope, I don't know if we can rely on the order in which the files are imported, I thought it was safer to import it using the setupFiles option.

Can we rely on the fact that import statements are executed in order ? Is this a standard ?

@cpojer
Copy link
Contributor

cpojer commented Sep 2, 2016

Yes, order matters for ES imports.

@cpojer
Copy link
Contributor

cpojer commented Sep 2, 2016

The thing is, --setupTestFrameworkScriptFile should really be deprecated or renamed and we should either expose --setupFile=<path> (which can be array arguments?) to add new setup files or do what @gaearon said and look for a setup file in a standard location.

tbh I'm happy to even support this directly in Jest. We could look for <rootDir>/.jest-setup.js.

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

Is src/index.test.js going to be confusing? I’m asking because we already have src/index.js as required file so it makes sense to “anchor” test setup around it.

@cpojer
Copy link
Contributor

cpojer commented Sep 2, 2016

Yeah I don't think that will work well as an automatic default tbh. Especially since right now it would be run as a test itself.

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

src/setupTests.js

@cpojer
Copy link
Contributor

cpojer commented Sep 2, 2016

I don't want to make an assumption either about folder structure or whether to use camelcase or anything inside of their actual code. I'd be happy to support jest-setup.js or .jest-setup.js in the root automatically. I think anything else I'd recommend you to do in CRA on top :)

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

Yeah, I meant doing this in CRA config. Since CRA already asks src/index.js to exist we can have more conventional names here, as this makes sense given nature of the project.

@cpojer
Copy link
Contributor

cpojer commented Sep 2, 2016

Absolutely, feel free to make any call that you think is best for CRA users and decouple it from a similar setting in Jest we may want to add.

@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

Does Jest fail if passed file doesn’t exist?

@cpojer
Copy link
Contributor

cpojer commented Sep 2, 2016

Yeah it'll fail. A silent error for a setup file wouldn't be very useful I think.

@gaearon
Copy link
Contributor

gaearon commented 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?

@gaearon gaearon added this to the 0.4.0 milestone Sep 2, 2016
@gaearon gaearon changed the title localStorage polyfill Provide a way to specify test environment setup file for Jest Sep 2, 2016
gaelollivier pushed a commit to EtixLabs/create-react-app that referenced this issue Sep 2, 2016
@gaearon
Copy link
Contributor

gaearon commented Sep 2, 2016

Thanks for fixing this!

stayradiated pushed a commit to stayradiated/create-react-app that referenced this issue Sep 7, 2016
feiqitian pushed a commit to feiqitian/create-react-app that referenced this issue Oct 25, 2016
@ebrentnelson
Copy link

#1990

@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants