You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While saving responses in a request handler, such as cy.intercept(..., REQUEST_HANDLER) I get a new error that says I can't execute 'writeFile' from outside a support file. I believe the reason the error message was worded this way is because I'm using cy.now("writeFile"... which apparently the handler of this function loses context that it was in fact written inside a support AND/OR test spec file. The function defined was in a support file.
This behavior used to work. I used to be able to write cy.intercept with a request handler that resolved the request normally and saved the intercepted request body to a file. This was the perfect way to automate stroring a Test Fixture for every endpoint that I had intercepted. It was beautiful. It's now broken.
This use to work, and I don't when the last time I saw "Saving every intercepted request to a fixture file" behavior working. So, it's hard to tell when this feature was broken. If I had to guess, it was around cypress version 12 that it used to work.
Test code to reproduce
The code example is in the stack overflow answer pasted above, at which time it was working great. Maybe that's a hint as to when this feature was working in cypress to help narrow down when it broke. Here is the example again.
Note: even when moving the cy.now call to the spec file (as shown in this example), it throws the exact same error (see stacktrace below, reading "cy.writeFile() must only be invoked from the spec file or support file.")
describe('fixture creation',()=>{it('should save fixtures of every endpoint that matches a url',()=>{cy.login()cy.visit('/ubmc/system/pci-topology')cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**',req=>{req.continue((res)=>{cy.now('writeFile',`cypress/fixtures/${req.url.replace('https://covfefe/','')}.json`,res.body)})})})it('should produce results from the fixtures directory',()=>{cy.intercept('**redfish/v1/Chassis/Asrock/PCIeDevices/**',req=>{req.reply({fixture: `${req.url.replace('https://covfefe/','')}.json`})})// @ts-ignorecy.login()cy.visit('/ubmc/system/pci-topology')})})
Cypress Version
v13.6.3
Node version
v18.12.0
Operating System
linux
Debug Logs
npm -v 8.19.2; node -v v18.12.0
uname -a; lsb_release -a
Linux beastmode 6.8.0-45-generic #45~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Sep 11 15:25:05 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.4 LTS
Release: 22.04
Codename: jammy
(uncaught exception)CypressError: cy.writeFile() must only be invoked from the spec file or support file.
(xhr)GET https://i-tyan/redfish/v1/Managers/SDS-7471481/LogServices/Log/Entries
managersPath
(xhr)GET https://i-tyan/redfish/v1/Systems/Asrock
systemsPath
CypressError
The following error originated from your application code, not from Cypress. It was caused by an unhandled promise rejection.
>cy.writeFile() must only be invoked from the spec file or support file.
When Cypress detects uncaught errors originating from your application it will automatically fail the current test.
This behavior is configurable, and you can choose to turn this off by listening to the uncaught:exception event.Learn more
cypress/e2e/wait.cy.ts:2:6
1 |export const wait = (ms: number = 30000) => {
> 2 | cy.wait('@sessionService', {timeout: ms})
| ^
Other
If there were some way to write a fixture file for every response in a normal fashion, please let me know. I feel like this hacky solution is being guarded against and I would like to understand what changed.
Thank you!
The text was updated successfully, but these errors were encountered:
Current behavior
While saving responses in a request handler, such as
cy.intercept(..., REQUEST_HANDLER)
I get a new error that says I can't execute 'writeFile' from outside a support file. I believe the reason the error message was worded this way is because I'm usingcy.now("writeFile"...
which apparently the handler of this function loses context that it was in fact written inside a support AND/OR test spec file. The function defined was in a support file.This behavior used to work. I used to be able to write cy.intercept with a request handler that resolved the request normally and saved the intercepted request body to a file. This was the perfect way to automate stroring a Test Fixture for every endpoint that I had intercepted. It was beautiful. It's now broken.
Desired behavior
https://stackoverflow.com/questions/72664114/automatically-save-cypress-fixture-from-api-response
This use to work, and I don't when the last time I saw "Saving every intercepted request to a fixture file" behavior working. So, it's hard to tell when this feature was broken. If I had to guess, it was around cypress version 12 that it used to work.
Test code to reproduce
The code example is in the stack overflow answer pasted above, at which time it was working great. Maybe that's a hint as to when this feature was working in cypress to help narrow down when it broke. Here is the example again.
Note: even when moving the
cy.now
call to the spec file (as shown in this example), it throws the exact same error (see stacktrace below, reading "cy.writeFile() must only be invoked from the spec file or support file.")Cypress Version
v13.6.3
Node version
v18.12.0
Operating System
linux
Debug Logs
Other
If there were some way to write a fixture file for every response in a normal fashion, please let me know. I feel like this hacky solution is being guarded against and I would like to understand what changed.
Thank you!
The text was updated successfully, but these errors were encountered: