cypress
cypress copied to clipboard
After upgrading to latest Cypress version 13.6.4, we are getting "cy.readFile() must only be invoked from the spec file or support file."
Current behavior
We are getting this error "Timed out retrying after 60000ms: cy.readFile() must only be invoked from the spec file or support file. Because this error occurred during a before all hook we are skipping all of the remaining tests." while reading a config document in the before all hook class. This issue is happening only on Chromium browsers and working fine in Electron.
This was working fine for all these days and suddenly started to happen now.
Note: this function(cy.readfile) written in custom commands and we are importing and using in the before all hook class
Desired behavior
We should able to read the file written in custom commands and can be used in before all hook class
Test code to reproduce
Custom Command:
Cypress.Commands.add('exportReportMetaData', () => {
cy.readFile('cypress/reporter/reportMetaData.json', { log: false }).then((obj) => {
if (obj['baseUrl'] == "") {
const triggeredURL = String(Cypress.config('baseUrl'));
let triggeredEnvironment, browserName, platformName;
(triggeredURL.includes('dvl')) ? triggeredEnvironment = 'DVL' :
(triggeredURL.includes('www')) ? triggeredEnvironment = 'PROD' :
triggeredEnvironment = 'UNDEFINED ENVIRONMENT';
(Cypress.browser.family == 'chromium') ? browserName = 'Chrome' : browserName = Cypress.browser.family;
(Cypress.platform == 'win32') ? platformName = 'Windows' :
(Cypress.platform == 'linux') ? platformName = 'Linux' :
(Cypress.platform == 'darwin') ? platformName = 'OSX' :
platformName = Cypress.platform;
const jsonObj = {
'baseUrl': triggeredURL,
'environment': triggeredEnvironment,
'browserName': browserName,
'browserVersion': Cypress.browser.version,
'platform': platformName,
'arch': Cypress.arch,
'device': obj['device'],
'isMobile': obj['isMobile']
};
cy.writeFile('cypress/reporter/reportMetaData.json', jsonObj, { log: false });
};
});
});
Before hook class:
before(function() {
cy.exportReportMetaData();
});
Cypress Version
13.6.4
Node version
v20.11.0
Operating System
Windows 10 Enterprise
Debug Logs
No response
Other
No response
Could you include the code and explanation around which file has the addCommand code and how it's included in the support file?
@jennifer-shehane Thanks for the response and here is the detailed explanation: we are referring to a json in before all hook class to set report custom data (like platform, URL, browser)
Custom Command written in cypress/support/command.ts
Cypress.Commands.add('exportReportMetaData', () => {
cy.readFile('cypress/reporter/reportMetaData.json', { log: false }).then((obj) => {
if (obj['baseUrl'] == "") {
const triggeredURL = String(Cypress.config('baseUrl'));
let triggeredEnvironment, browserName, platformName;
(triggeredURL.includes('dvl')) ? triggeredEnvironment = 'DVL' :
(triggeredURL.includes('www')) ? triggeredEnvironment = 'PROD' :
triggeredEnvironment = 'UNDEFINED ENVIRONMENT';
(Cypress.browser.family == 'chromium') ? browserName = 'Chrome' : browserName = Cypress.browser.family;
(Cypress.platform == 'win32') ? platformName = 'Windows' :
(Cypress.platform == 'linux') ? platformName = 'Linux' :
(Cypress.platform == 'darwin') ? platformName = 'OSX' :
platformName = Cypress.platform;
const jsonObj = {
'baseUrl': triggeredURL,
'environment': triggeredEnvironment,
'browserName': browserName,
'browserVersion': Cypress.browser.version,
'platform': platformName,
'arch': Cypress.arch,
'device': obj['device'],
'isMobile': obj['isMobile']
};
cy.writeFile('cypress/reporter/reportMetaData.json', jsonObj, { log: false });
};
});
});
Before class written in cypress/support/hook.ts and custom command is imported here
import './commands';
before(function() {
cy.exportReportMetaData();
});
Hook path is described in the cypress.config.ts
export default defineConfig({
e2e: {
.............
supportFile: "cypress/support/hooks.ts",
.........
},
});
@jennifer-shehane Any updates on this? Its severely impacting our execution.
Note: Its happening in the latest Cypress version(13.6.6) as well
Does the error go away if you move this customCommand directly into the support file or the test file? Unfortunately we don't have time to prioritize this work right now, so trying to find a workaround for you.
We ran into the same problem @sadhishsk But it wasn't actually the cy.readFile() which caused the issue, but a workaround suggested here: https://github.com/cypress-io/cypress/issues/25397#issuecomment-2047781361
So maybe you should check your e2e.ts if there's something which escapes the spec context.
We're currently running on Cypress 13.7.3, node version 20.12.2