cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.02k stars 3.19k forks source link

cy.writeFile() must only be invoked from the spec file or support file. for earlier used files is working fine, but in new spec file it is throwing error #29205

Open SairamNewron opened 7 months ago

SairamNewron commented 7 months ago

Current behavior

Unable to use writefile in new spec file , but works fine in old spec files

Desired behavior

No response

Test code to reproduce

..................................................... 
 jsonUpdateFalse(fs) {
        let filepath = "cypress/fixtures/HRManagementInputData.json"
        cy.fixture("HRManagementInputData.json").then((data) => {
            const existingData = data
            // Modify the existing data
            existingData[0].Shift_master_flag = 'false';

            // Convert modified data back to JSON string
            const updatedJsonString = JSON.stringify(existingData, null, 2);
            console.log(updatedJsonString)
            // Write the updated JSON string to the file
           cy.writeFile(filepath,updatedJsonString)
        })
    }
.............................................
[
    {
        "Shift_master_flag": "false",
        "create_shift": [
            "Full_Shift"
        ]
    }
]
.........................................

import shiftMasterEntity from "../../../../../../Pages/Entity/Settings/D_HR_Management/a_HR_Master/ShiftMaster/shiftMasterEntity";

  shiftMaster.jsonUpdateFalse(fs);

Cypress Version

13.7.1

Node version

20.9

Operating System

windows 64

Debug Logs

17
writeFilecypress/fixtures/HRManagementInputData.json
CypressError
cy.writeFile() must only be invoked from the spec file or support file.

Other

No response

jennifer-shehane commented 7 months ago

@SairamNewron Could you provide a full example that we could run and see the error? Thanks.

SairamNewron commented 7 months ago

@jennifer-shehane , I observe that , the same spec file used in direct in e2e folder , it is not throwing error for cy.writefile(), but when i keep spec file in sub folders like fourth or fifth level sub - sub folder , it started throwing error for new spec ,may be the cy.writefile is not recognizing the imported js file as supported file / unable to call cy.writefile if spec file is in sub folders (it is getting failed when i used the same method in command.js).

it seems issue is with spec file location in folder structure

folder structure used for POM module is as follows

cypress > e2e / pages / main menu / sub menu / module / sub module / page.js cypress > e2e / testscripts / main menu / sub menu / module / sub module / test.cy.js

issue in import (only for cy.writefile) import shiftMasterEntity from "../../../../../../Pages/Entity/Settings/D_HR_Management/a_HR_Master/ShiftMaster/shiftMasterEntity";

no issue in the following import same file (only for cy.writefile)

import shiftMasterEntity from '../../../Pages/Entity/Settings/D_HR_Management/a_HR_Master/ShiftMaster/shiftMasterEntity';

SairamNewron commented 4 months ago

The issue still exist for one spec file , what I have used, but it is working fine in new spec files

juanpaflo commented 3 months ago

@SairamNewron could this be related to the path of the file being too long? It is just a thought.

Example (I know it is not the full path): "../../../../../../Pages/Entity/Settings/D_HR_Management/a_HR_Master/ShiftMaster/shiftMasterEntity" Has 98 characters "../../../Pages/Entity/Settings/D_HR_Management/a_HR_Master/ShiftMaster/shiftMasterEntity" Has 89 characters

Maybe the first path exceeds whatever the maximum limit is.

SairamNewron commented 3 months ago

I don't know the exact reason for this, but this is not working only in single spec file , all the previous and new files are working fine , I am unable to reproduce the issue