magnificent-finland-58048
04/25/2022, 4:50 PMgray-kilobyte-89541
04/25/2022, 5:55 PMmagnificent-finland-58048
04/25/2022, 10:04 PMlemon-kite-81269
04/25/2022, 11:11 PMadorable-smartphone-87280
04/26/2022, 3:07 AMgreen-ram-11259
04/26/2022, 9:39 AMgreat-journalist-58601
04/26/2022, 12:51 PMsome-salesclerk-23018
04/26/2022, 1:25 PMadorable-smartphone-87280
04/26/2022, 2:47 PMgray-kilobyte-89541
04/26/2022, 2:48 PMable-monkey-72947
04/26/2022, 2:57 PMcy
command I'm trying to write, I'm consistently running into this error -
Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.
Cypress will resolve your command with whatever the final Cypress command yields.
The code triggering this looks something like this -
cy.intercept({ method: method, url: url, ...cyRequestOptions }, request => {
request.continue(response => {
cy.log('Recording response...')
cy.writeFile(filePath, response)
})
}
I am trying to record the response
here so that it can be replayed during CI/CD testing, but Cypress is unhappy that I'm trying to cy.anything
inside of cy.intercept
.
Is there some use of .then
I'm not thinking of that will solve my issue here?gray-kilobyte-89541
04/26/2022, 3:21 PMable-monkey-72947
04/26/2022, 3:27 PMgray-kilobyte-89541
04/26/2022, 3:56 PMable-monkey-72947
04/26/2022, 3:59 PMicy-exabyte-68383
04/26/2022, 4:45 PMable-monkey-72947
04/26/2022, 7:20 PMon('task', {
doesSavedMockExist(filename) {
if (fs.existsSync(filename)) {
return {
exists: true,
mock: fs.readFileSync(filename, { encoding: 'utf8' }),
save: null
}
} else {
return {
exists: false,
mock: null,
save: (path, data) => fs.writeFileSync(path, data, { encoding: 'utf8' })
}
}
}
})
I'm curious why I'm not able to pass a function to save
in my else
block here... when I get into that code path I only get {exists: false, mock: null}
, I'm not seeing any reason in the docs why this should be the case?some-salesclerk-23018
04/26/2022, 11:41 PMsome-salesclerk-23018
04/27/2022, 12:00 AMsome-salesclerk-23018
04/27/2022, 12:00 AM/**
* (under domain 1)
* cy.findByRole('button', { name: 'example' }).click();
*
* (the button is doing something complicated)
*
* (now we are under domain 2)
* cy.get('#example').should('be.visible')
*/
adorable-smartphone-87280
04/27/2022, 12:46 AMsome-salesclerk-23018
04/27/2022, 12:48 AMadorable-smartphone-87280
04/27/2022, 12:51 AMadorable-smartphone-87280
04/27/2022, 12:52 AM.origin
feature. I think it's intended to do things like open a login page to get a cookie and then return to another context/page. I'm not sure that a page redirect that happens from the app itself would be supported by Cypress still.some-salesclerk-23018
04/27/2022, 1:23 AMadorable-smartphone-87280
04/27/2022, 1:23 AMsome-salesclerk-23018
04/27/2022, 1:26 AMable-monkey-72947
04/27/2022, 6:47 PMexport default function(method, url, options = {}) {
if (!method) throw new Error('You must pass a valid http method')
if (!url) throw new Error('You must pass a valid url')
const recordingsDirectory = Cypress.config('recordingsDirectory')
const requestName = `${method}__${url.replace(/(\.|\/)/g, '_')}`.toLowerCase()
const fileTreePath = Cypress.currentTest.titlePath.join('/')
const filePath = `${recordingsDirectory}/${fileTreePath}/${requestName}.json`
const cyRequestOptions = options?.cyRequestOptions || {}
cy.task('doesFileExist', filePath)
.then(fileExists => {
return {
shouldRecord: !!(!fileExists || options.record), // coerce to a single boolean here
recordReason: options.record ? '`record` option explicitly set' : 'matching recording did not exist'
}
})
.then(file => {
if (file.shouldRecord) {
cy.log(`${file.recordReason}, saving recorded response...`)
} else {
cy.log(`there was already a recording for ${url}, playing that back...`)
}
const cyInterceptArgs = { method: method, url: url, ...cyRequestOptions }
if (file.shouldRecord) {
cy.intercept(cyInterceptArgs, request => {
request.on('before:response', response => {
Cypress.env('response', response)
})
request.continue()
}).as(url)
} else {
cy.readFile(filePath).then((mock) => {
cy.intercept(cyInterceptArgs, request => {
request.continue(response => {
response.send({...mock})
})
}).as(url)
})
}
// wrap and pass this data into the next .then block
cy.wrap(file)
})
.then((file) => {
if (file.shouldRecord && Cypress.env('response')) {
cy.writeFile(filePath, Cypress.env('response'))
}
})
}
able-monkey-72947
04/27/2022, 6:48 PMcy.
(or do anything async, really) inside the cy.intercept
request
callback... so instead I stashed the data I wanted to write to a file in Cypress.env
then grabbed that in a final .then
call like cy.writeFile(filePath, Cypress.env('response'))
.
Running tests right now but in my manual testing its looking like this works 🙌rough-nightfall-90576
04/27/2022, 7:13 PM