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

Automatically save Cypress fixture from API response - was working before and has broken #30409

Open
activedecay opened this issue Oct 15, 2024 · 1 comment
Labels
stage: awaiting response Potential fix was proposed; awaiting response

Comments

@activedecay
Copy link

activedecay commented Oct 15, 2024

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 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.

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.")

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-ignore
    cy.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!

@jennifer-shehane
Copy link
Member

@activedecay Why are you using cy.now? Can you just use cy.writeFile?

@jennifer-shehane jennifer-shehane added the stage: awaiting response Potential fix was proposed; awaiting response label Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: awaiting response Potential fix was proposed; awaiting response
Projects
None yet
Development

No branches or pull requests

2 participants