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:
Kukhyeon Heo
2020-12-18 00:16:21 +09:00
committed by GitHub
parent f47d5c0574
commit 0f6d7bda79
3 changed files with 32 additions and 2 deletions
@@ -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)
})
})
})
})
+17 -1
View File
@@ -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,
+3 -1
View File
@@ -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),
})