Skip to content

Commit

Permalink
fix: cy.log() inside cy.then() doesn't break subject value in command…
Browse files Browse the repository at this point in the history
… chain. (#9372)

Co-authored-by: Gleb Bahmutov <[email protected]>
  • Loading branch information
sainthkh and bahmutov authored Dec 17, 2020
1 parent f47d5c0 commit 0f6d7bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/driver/cypress/integration/commands/misc_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
})

Expand Down
18 changes: 17 additions & 1 deletion packages/driver/src/cy/commands/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion packages/driver/src/cypress/cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -1112,6 +1113,7 @@ const create = function (specWindow, Cypress, Cookies, state, config, log) {
type,
chainerId,
userInvocationStack,
injected,
fn: wrap(firstCall),
})

Expand Down

4 comments on commit 0f6d7bd

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0f6d7bd Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.2.0/circle-develop-0f6d7bda79c814f62523221e13b3b2f20529e6fa/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0f6d7bd Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.2.0/appveyor-develop-0f6d7bda79c814f62523221e13b3b2f20529e6fa/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0f6d7bd Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.2.0/appveyor-develop-0f6d7bda79c814f62523221e13b3b2f20529e6fa/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0f6d7bd Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.2.0/circle-develop-0f6d7bda79c814f62523221e13b3b2f20529e6fa/cypress.tgz

Please sign in to comment.