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

cookie not getting read correctly from jest test in 4.0.2 #237

Closed
jonathanhculver opened this issue Sep 12, 2019 · 3 comments
Closed

cookie not getting read correctly from jest test in 4.0.2 #237

jonathanhculver opened this issue Sep 12, 2019 · 3 comments

Comments

@jonathanhculver
Copy link

Hello!

Opening in case other's have had the same issue. In 4.0.2 I have a jest test that set's a cookie and a function being tested that read's the cookie. The cookie comes back as undefined.

Downgrading to 4.0.0 fixes the issue for me.

Example:

// testFunc.js
import Cookies from 'universal-cookie';

const cookies = new Cookies();
const testFunc = () => (cookies.get('testCookie'));

// testFunc.spec.js
import Cookies from 'universal-cookie'
import testFunc from './testFunc';

const cookies = new Cookies();

test('testFunc', () => {
  cookies.set('testCookie', 'blah');
  // logging cookies.get('testCookie') correctly logs 'blah'

  const result = testFunc();
  expect(result).toEqual('blah'); // fails - result is actually `undefined`
});
@JHughes11
Copy link

Hello @jonathanhculver! I'm having the same issue that you're having. Looking through the source code, I believe I found the root of the problem.

Seen here

the DOCUMENT_HAS_COOKIE is initialized as false in versions greater than 4.0.0.

Setting this value to true in both the .js file and test file seems to fix this issue. But I would just stick with the 4.0.0 version until it can get fixed :D

@eXon
Copy link
Collaborator

eXon commented Dec 27, 2019

Fixed with v4.0.3

@bhushan-patil-official
Copy link

bhushan-patil-official commented May 26, 2022

For me I think while testing I had to wrap the component with CookieProvider fixed it

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

4 participants