Cypress: cy.setCookie always sets the same cookie if you pass the same options object

Created on 31 Oct 2018  路  6Comments  路  Source: cypress-io/cypress

cy.setCookie always sets the same cookie if you pass the same options object.

Current behavior:

Calling cy.setCookie multiple times always sets the same cookie as the first call if you pass the same options object each time. For example, the following sets two cookies named "foo" and "baz".

'use strict';

Cypress.Cookies.debug(true);

const options = {};

describe('cookie reuse', function() {
    it('sets the expected cookie', function() {
        cy.setCookie('foo', 'bar', options);
        cy.setCookie('baz', 'qux', {});
        cy.setCookie('apple', 'banana', options);

        cy.getCookie('foo').its('value').should('equal', 'bar');
        cy.getCookie('baz').its('value').should('equal', 'qux');
        cy.getCookie('apple').its('value').should('equal', 'banana');
    });
});

The Cypress log shows the names and values of all three cookies I tried to create, but the console shows that only two cookies were created:

setcookie-always-sets-same-cookie-when-options-object-is-the-same

The cookies section in the Chrome dev tools also shows only two cookies, "foo" and "baz":

setcookie-cookies-list

The "apple" cookie was never created. The call to cy.setCookie('apple', 'banana', options); just set the "foo" cookie again.

Desired behavior:

If I call cy.setCookie twice with different cookie names and values, but use the same options object in both calls, I expect to create two separate cookies.

Steps to reproduce:

See code snippet above.

Versions

Cypress 3.0.3, Mac OS 10.13.6, Chrome 70.0.3538.77

topic bug

Most helpful comment

I'm taking a look right now! Not sure if it'll get in the next release, but we'll do our best.

All 6 comments

Yes, this is a bug. We are mutating the object instead of cloning it as we should be.

@jennifer-shehane - I ran into this recently as well. Any chance something like this might come out within the next release? Looking forward to a fix on this and #1321 (#2685 ?)

I'm taking a look right now! Not sure if it'll get in the next release, but we'll do our best.

@Knaledge #1321 will be in the next release

The code for this is done in cypress-io/cypress#2707, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

Released in 3.6.1.

Was this page helpful?
0 / 5 - 0 ratings