mirror of
https://github.com/cypress-io/cypress.git
synced 2026-05-06 15:00:50 -05:00
fix: cy.log() inside cy.then() doesn't break subject value in command chain. (#9372)
Co-authored-by: Gleb Bahmutov <gleb.bahmutov@gmail.com>
This commit is contained in:
@@ -72,6 +72,18 @@ describe('src/cy/commands/misc', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// https://github.com/cypress-io/cypress/issues/8084
|
||||
it('log does not corrupt the stack and returns subject correctly', () => {
|
||||
cy.wrap({ a: 42 }).then(async (data) => {
|
||||
cy.log('count', Object.keys(data).length)
|
||||
cy.log('another log')
|
||||
|
||||
return await Object.keys(data).length
|
||||
}).then((test) => {
|
||||
expect(test).to.eq(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ const Promise = require('bluebird')
|
||||
const $dom = require('../../dom')
|
||||
const $errUtils = require('../../cypress/error_utils')
|
||||
|
||||
module.exports = (Commands, Cypress, cy) => {
|
||||
module.exports = (Commands, Cypress, cy, state) => {
|
||||
Commands.addAll({ prevSubject: 'optional' }, {
|
||||
end () {
|
||||
return null
|
||||
@@ -17,6 +17,22 @@ module.exports = (Commands, Cypress, cy) => {
|
||||
},
|
||||
|
||||
log (msg, args) {
|
||||
// https://github.com/cypress-io/cypress/issues/8084
|
||||
// The return value of cy.log() corrupts the command stack, so cy.then() returned the wrong value
|
||||
// when cy.log() is used inside it.
|
||||
// The code below restore the stack when cy.log() is injected in cy.then().
|
||||
if (state('current').get('injected')) {
|
||||
const restoreCmdIndex = state('index') + 1
|
||||
|
||||
cy.queue.splice(restoreCmdIndex, 0, {
|
||||
args: [state('subject')],
|
||||
name: 'log-restore',
|
||||
fn: (subject) => subject,
|
||||
})
|
||||
|
||||
state('index', restoreCmdIndex)
|
||||
}
|
||||
|
||||
Cypress.log({
|
||||
end: true,
|
||||
snapshot: true,
|
||||
|
||||
@@ -1099,8 +1099,9 @@ const create = function (specWindow, Cypress, Cookies, state, config, log) {
|
||||
// dont enqueue / inject any new commands if
|
||||
// onInjectCommand returns false
|
||||
const onInjectCommand = state('onInjectCommand')
|
||||
const injected = _.isFunction(onInjectCommand)
|
||||
|
||||
if (_.isFunction(onInjectCommand)) {
|
||||
if (injected) {
|
||||
if (onInjectCommand.call(cy, name, ...args) === false) {
|
||||
return
|
||||
}
|
||||
@@ -1112,6 +1113,7 @@ const create = function (specWindow, Cypress, Cookies, state, config, log) {
|
||||
type,
|
||||
chainerId,
|
||||
userInvocationStack,
|
||||
injected,
|
||||
fn: wrap(firstCall),
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user