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.

Impossible to set cookies on runtime

See original GitHub issue

I’ve come up across such problem: it’s impossible to set a cookie during page runtime. My test is:

import Cookies from 'js-cookie';
import { jsdom, createCookieJar } from 'jsdom';

const doc = jsdom('<!doctype html><html><body></body></html>');
global.document = doc;
global.window = doc.defaultView;
global.window.name = '';

describe('Cookies test', () => {
    it('should set and get cookie', () => {
        const name = 'coo';
        const value = 'foo';
        Cookies.set(name, value);
        expect(Cookies.get(name)).to.be.equal(value);
    })
});

the same goes for

document.cookie='coo=foo';

The cookie remains undefined.

My goal is to test whether the cookie is set when I click on some button on the page, which should be technically possible.

My environment: “jsdom”: “^9.8.3”, “mocha”: “^3.1.2”,“sinon”: “^1.17.6”, “expect.js”: “^0.3.1”, Node v6.9

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
domeniccommented, Jan 4, 2017

This is presumably failing because you are using an unsupported pattern: https://github.com/tmpvar/jsdom/wiki/Don’t-stuff-jsdom-globals-onto-the-Node-global

Also, you are using syntax (import) which is not supported on the platforms jsdom supports.

Closing for now, but happy to reopen if you can provide a repro using supported syntax and not using global variables.

3reactions
domeniccommented, Jan 4, 2017

Your jsdom-fail repo has two problems:

  • It is trying to use require in a browser environment. That will just throw an error, causing nothing to happen.
  • Even if that worked, it would be using two separate js-cookie modules: one in Node.js, and one inside the DOM. You need to consistently use the one inside the DOM if you want to see changes that are made to the DOM.
Read more comments on GitHub >

github_iconTop Results From Across the Web

php - Why are my cookies not setting?
The problem comes from the fact that setcookie() doesn't set the cookies immediately, it sends the headers so the browser sets the cookies....
Read more >
How to Set Cookie and Update Cookie with JavaScript
Set a cookie · document.cookie is the command used to create a new cookie. · 'newCookie' is a string that sets the cookie...
Read more >
Cookies, document.cookie
A write operation to document.cookie updates only cookies mentioned in it, but doesn't touch other cookies. For instance, this call sets a ...
Read more >
Upcoming changes in cookie handling in Google Chrome
Google Chrome will change its default cookie behavior in Feb 2020. ... flows using cross-site requests; Custom cookies set in JavaScript.
Read more >
'Failed to set Cookie' in CF10/11
Failed to set cookie. ColdFusion is unable to add the cookie you specified to the response. This is probably because you have used...
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