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

Cypress.Cookies.defaults() return type is void but the code does return a value #15426

Closed
ben8p opened this issue Mar 11, 2021 · 3 comments · Fixed by #15550
Closed

Cypress.Cookies.defaults() return type is void but the code does return a value #15426

ben8p opened this issue Mar 11, 2021 · 3 comments · Fixed by #15550
Labels
type: typings Issue related to Cypress types (for TypeScript)

Comments

@ben8p
Copy link
Contributor

ben8p commented Mar 11, 2021

What would you like?

Cypress.Cookies.defaults() allow to preserve some cookies during the test execution.

However, there is no official way to extend the existing list of defaults.

Cypress.Cookies.defaults() return the default object but the type tells it returns void

Ideally the returnType of Cypress.Cookies.defaults() should be change, or a method to retrieve defaults could be added

Why is this needed?

We have a cypress plugin doing some user authentication.
This plugin uses Cypress.Cookies.defaults() to make sure the authentication is working.

In our code, we also have a special cookie that we set and we could like it to be preserved.

With current types it seems impossible.

However, we can "hack" something because Cypress.Cookies.defaults() does return a value:

// the Cypress type wrongly says the default method is returning void.
		// in fact it returns the destination object (because the implementation is based on _.extends)
		// see https://github.com/cypress-io/cypress/blob/c12347dc9c4f9dc0662ad9350536c6c6b99225c4/packages/driver/src/cypress/cookies.js#L144
		// thanks to this hack we can extend the existing defaults and add our cookie
		const currentDefault = Cypress.Cookies.defaults({}) as unknown as Partial<Cypress.CookieDefaults> | undefined;
		if (currentDefault && 'preserve' in currentDefault) {
			const { preserve } = currentDefault;
			const toPreserve = [];
			if (Array.isArray(preserve)) {
				toPreserve.push(...preserve, USERNAME_COOKIE);
			} else if (typeof preserve === 'string') {
				toPreserve.push(preserve, USERNAME_COOKIE);
			} else if (!preserve) {
				toPreserve.push(USERNAME_COOKIE);
			} else {
				throw new Error(`could not extend the existing Cypress.Cookies.defaults, received: ${JSON.stringify(currentDefault)}`);
			}
			Cypress.Cookies.defaults({ preserve: toPreserve });
			cy.log(`Preserved cookies: ${toPreserve.join()}`);
		} else {
			// because this whole solution is hack, let's be verbose in case of failure
			throw new Error(`could not fetch the existing Cypress.Cookies.defaults, received: ${JSON.stringify(currentDefault)}`);
		}
@jennifer-shehane jennifer-shehane added type: typings Issue related to Cypress types (for TypeScript) stage: ready for work The issue is reproducible and in scope labels Mar 16, 2021
@jennifer-shehane
Copy link
Member

Yes, feel free to open a PR to fix the types. Check out our contributing doc.

ben8p added a commit to ben8p/cypress that referenced this issue Mar 17, 2021
@cypress-bot cypress-bot bot added stage: work in progress and removed stage: ready for work The issue is reproducible and in scope labels Mar 17, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 14, 2021

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

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 26, 2021

Released in 7.2.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v7.2.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: typings Issue related to Cypress types (for TypeScript)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants