Situation is:
I have a bunch of cypress test spec that are depending on each other (the specification of tested project makes it impossibly to make them independent).
In the first spec I’m creating an array of objects and making it an environmental variable so I can operate on that array in other specs.
In head-full mode with the cypress UI dashboard it works pretty smoothly, but in headless mode the value of environmental variable created in one spec is ‘undefined’ in the next spec.
So
spec1.cy.ts
const arrafOfObjects = pageObject.functionThatReturnsTheArray();
Cypress.env("arrafOfObjects", arrafOfObjects);
spec2.cy.ts
const arrafOfObjects = Cypress.env("arrafOfObjects");
const length = arrafOfObjects.length //TypeError: Cannot read properties of undefined (reading 'length')
cypress.config.js
const { defineConfig } = require("cypress");
const { verifyDownloadTasks } = require('cy-verify-downloads');
const { removeDirectory } = require('cypress-delete-downloads-folder');
const env = 'staging'
module.exports = defineConfig({
env:{
godUser: '[email protected]',
passwordChargeGodUser: 'secretPassword'
securityconfigHash: process.env.secretConfigHash
},
e2e: {
baseUrl: 'https://url.' + env + '.domain.cloud',
excludeSpecPattern : ["cypress/e2e/2-advanced-examples","cypress/e2e/1-getting-started", "cypress/e2e/0-financeportal"],
downloadsFolder: "cypress/downloads",
testIsolation: false,
trashAssetsBeforeRuns : true,
experimentalRunAllSpecs : true,
chromeWebSecurity: false,
experimentalModifyObstructiveThirdPartyCode: true,
experimentalOriginDependencies: false,
defaultCommandTimeout: 10000,
numTestsKeptInMemory : 0,
requestTimeout : 30000,
responseTimeout : 50000,
pageLoadTimeout: 1000000,
setupNodeEvents(on, config) {
on('task', verifyDownloadTasks);
on('task', { removeDirectory })
},
},
});
EDIT: I did some more investigation and it seems that headless mode is ignoring testIsolation: false
configuration and is wiping out memory… any idea how it’s possible?
2
Answers
Cypress.env
variables are reset in each spec file. Headed mode may be doing something wonky or different than I’d expect, but it makes sense that the environment variable is undefined in the new spec file. My recommendation is to re-structure your setup so that your spec files aren’t dependent on one another – there are no assurances that your files will always run in a specific order, and it is generally best practice to have each test be as independent as possible.The plugin cypress-data-session has an option shareAcrossSpecs that seems to work for your scenario.
This is an example, since you have pageObjects I added a property that is a function (in case serialization breaks functions)
spec1.cy.js
spec2.cy.js