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.

Cookies.get() always returns empty when used with jsdom.

See original GitHub issue

In this block of code:

if (this.json) {
  try {
    cookie = JSON.parse(cookie);
  } catch (e) {}
}

If this is undefined, it will throw an error causing a Cookies.get() to always return an empty {}.

The solution is to change the conditional to the following:

if (this && this.json) {
  try {
    cookie = JSON.parse(cookie);
  } catch (e) {}
}

This is only failing in my test suite where jsdom is used instead of a browser. Everything seems to work in the browser.

Working on a PR.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
FagnerMartinsBrackcommented, Sep 2, 2016

@dmatteo

Does that help in any way?

I will take a look for v2.1.4.

1reaction
dmatteocommented, Sep 2, 2016

Hey @FagnerMartinsBrack, I finally managed to get around this thread and try and reproduce whatever issue was here, without success though, or actually without failures…

I’ve created a small repo here https://github.com/dmatteo/jsCookieRepro that will also help clear out the jsdomify.default case. Good point on the fact that the CommonJS use case is not documented on the README, I will add that asap.

There are 3 branches on the repo:

  • master: running on jsdomify 3.0.0 which depends on jsdom 9.4.2
    • It’s using ES6 syntax, including ES6 modules: import jsdomify from 'jsdomify'
    • Tests can be run with ./node_modules/.bin/mocha --compilers js:babel-register
  • no-transpiling: running on jsdomify 3.0.0 which depends on jsdom 9.4.2
    • Dear 'ol ES5, which means var jsdomify = require('jsdomify').default
    • The .default part is not a jsdomify specific thing, but it’s there because the library is exported as a default ES6 module and then transpiled via babel before publishing, so it can be used also via CommonJS
    • Tests can be run with ./node_modules/.bin/mocha
  • jsdomify-2: running on jsdomify 2.1.0 which depends on jsdom 8.2.0
    • Again ES6 here, but using the version of jsdomify that was mentioned in the issue here.
    • Tests can be run with ./node_modules/.bin/mocha --compilers js:babel-register

All the (2) tests in the 3 branches passes, so I’m not sure what was the issue here.

Does that help in any way?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript document.cookie always returns empty string
Title: Javascript document.cookie always empty string . Answer: Can happen when there are only cookies with the HttpOnly flag set. No you can't ......
Read more >
Document.cookie - Web APIs | MDN
The Document property cookie lets you read and write cookies associated with the document. It serves as a getter and setter for the...
Read more >
jsdom-no-contextify | Yarn - Package Manager
A JavaScript implementation of the WHATWG DOM and HTML standards. This fork has no dependency on contextify. Be aware: the global scope of...
Read more >
Changelog.md - jest-environment-jsdom-fourteen - GitLab
JSDOM.fragment() now creates fragments whose document has no ... isConnected to not always return false for nodes inside a shadow tree.
Read more >
client/node_modules/jest-environment-jsdom-fourteen ...
Functions on the module you get from require('tough-cookie') . ... For use with .sort() , sorts a list of cookies into the recommended...
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