https://cypress.io logo
Events - Environment Variables vs WriteFile
# i-need-help
b
I'm trying to create a list of values during the tests execution adding them to an environment variable. After all the tests execute, I want to write those values into a file. If I use the event 'after: spec' on the file cypress.config.js, I can't access the environment variables but I can write into files using 'fs-extra' If I use the event 'test:after:run' on the commands.js, I can acess to the environment variables but I can't write into a file. Using cy.writeFile doesn't trigger any error but doesn't do anything. If I import fs-extra inside the file commands.js const fs = require('fs-extra') I get an error ---- When Cypress detects uncaught errors originating from your test code it will automatically fail the current test. Cypress could not associate this error to any specific test. We dynamically generated a new test to display this failure. node_modules/fs-extra/lib/fs/index.js:121:1 119 | 120 | // fs.realpath.native sometimes not available if fs is monkey-patched > 121 | if (typeof fs.realpath.native === 'function') { | ^ 122 | exports.realpath.native = u(fs.realpath.native) 123 | } else { 124 | process.emitWarning( So, I'm a little blocked
g
Your tests run in the browser, so you cannot use
fs
- cause there is no file system. Instead, collect all the data you want to write and use
afterEach
hook with
cy.writeFile
I use this approach to write collected network calls into a JSON file after the test in my paid course https://cypress.tips/courses/network-testing/lessons/bonus69
b
great site btw.. yes, that's what i'm trying to do. I want to extract all the cy.request information along the tests, and dump it to a file
it worked on the after event
Shouldn't the "after: spec" event be equal or at least similar to the after event?
g
nah, you want to use Cypress cy. commands - you got to be inside a test that is still running
6 Views