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

Using in jest tests #113

Closed
taviroquai opened this issue Oct 20, 2017 · 9 comments
Closed

Using in jest tests #113

taviroquai opened this issue Oct 20, 2017 · 9 comments

Comments

@taviroquai
Copy link

Hi,

I would like to know out to use it in jest environment once I'm getting this error:

Cannot read property 'cookie' of undefined
at Cookies._updateBrowserValues (/home/bla/app/node_modules/universal-cookie/lib/Cookies.js:47:55)

I tried to set the cookie in the test like this, before calling the usage in app, but it didn't work:

import Cookies from 'universal-cookie'
const cookieHandler = new Cookies()
cookieHandler.set('foo', {}, { path: '/' });

Thanks

@charliewilco
Copy link

running into the same thing with cannot read get of undefined.
i wonder if we can just mock that out.

@eXon
Copy link
Collaborator

eXon commented Nov 23, 2017

It should be easy to use universal-cookie with unit test. Just make sure you are defining the cookies, you don't need to mock it. All you need is pass an object to Cookies just like that:

import Cookies from 'universal-cookie'
const cookieHandler = new Cookies({ myCookie: 'meow!' });
cookieHandler.set('foo', {}, { path: '/' });

If you don't have any cookie, you can set an empty object.

@eXon eXon closed this as completed Nov 23, 2017
@sylvainpina
Copy link

sylvainpina commented May 23, 2018

i have the same issue.
to fix this with jest 22.4.4, I do that, it's creepy but it works.

cookies = new Cookies({})
cookies.HAS_DOCUMENT_COOKIE = false

if not, universal-cookie believes environnement is browser, because jest load jsdom...

@pdesantis
Copy link

I had the same issue, and cookies.HAS_DOCUMENT_COOKIE = false fixed it. Thanks @sylvainpina!

Any thoughts on exposing this publicly? Or are there any better ways to make this jest-friendly?

@chj-damon
Copy link

@pdesantis it won't work in typescript.

@eXon
Copy link
Collaborator

eXon commented Jul 4, 2019

In v4.0.1, it should detect JSDOM and skip the browser.

@mocantimoteidavid
Copy link

Would there be any other solution in making this work in typescript too?

@kevin-brotcke
Copy link

I was able to fix this in my case by specifying node as the jest environment since the code I was testing only runs on the server.

https://jestjs.io/docs/en/configuration.html#testenvironment-string

@jappareti
Copy link

@chj-damon @mocantimoteidavid I got it to work in typescript by doing this:

cookies = new Cookies({})
cookies['HAS_DOCUMENT_COOKIE'] = false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants