Impossible to set cookies on runtime
See original GitHub issueI’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:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Your jsdom-fail repo has two problems:
require
in a browser environment. That will just throw an error, causing nothing to happen.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.