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

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." #28915

Closed
sadhishsk opened this issue Feb 10, 2024 · 7 comments
Labels
stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period v12.5.0 🐛

Comments

@sadhishsk
Copy link

sadhishsk commented Feb 10, 2024

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();
});

Issue

Cypress Version

13.6.4

Node version

v20.11.0

Operating System

Windows 10 Enterprise

Debug Logs

No response

Other

No response

@sadhishsk sadhishsk changed the title 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." error 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." Feb 10, 2024
@jennifer-shehane
Copy link
Member

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 jennifer-shehane added v12.5.0 🐛 stage: needs investigating Someone from Cypress needs to look at this labels Feb 12, 2024
@sadhishsk
Copy link
Author

sadhishsk commented Feb 13, 2024

@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",
                .........
	},
});

@sadhishsk
Copy link
Author

@jennifer-shehane Any updates on this? Its severely impacting our execution.

Note: Its happening in the latest Cypress version(13.6.6) as well

@jennifer-shehane
Copy link
Member

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.

@coco1979ka
Copy link

We ran into the same problem @sadhishsk
But it wasn't actually the cy.readFile() which caused the issue, but a workaround suggested here: #25397 (comment)

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

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Oct 23, 2024
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: needs investigating Someone from Cypress needs to look at this stale no activity on this issue for a long period v12.5.0 🐛
Projects
None yet
Development

No branches or pull requests

4 participants