Skip to content

Commit

Permalink
fix: do not attempt to trim objects when printing to console (#18341)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbolgiano committed Oct 11, 2021
1 parent 1d08280 commit 7a4c59a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/runner-shared/src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const logger = {
_.each(formattedLog, (value, key) => {
// don't log empty strings
// _.trim([]) returns '' but we want to log empty arrays, so account for that
if (_.trim(value) === '' && !_.isArray(value)) return
// Skip trim if we know value is an object
if (typeof value !== 'object' && _.trim(value) === '' && !_.isArray(value)) return

this.log(`%c${key}`, 'font-weight: bold', value)
})
Expand Down
39 changes: 33 additions & 6 deletions packages/runner-shared/src/logger.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
const sinon = require('sinon')
const { logger } = require('./logger')
import _ from 'lodash'

describe('logger', () => {
let spyLog = sinon.spy(logger, 'log')

afterEach(() => {
// reset after each unit test
spyLog.resetHistory()
})

// https://github.com/cypress-io/cypress/issues/17542
it('cy.log() shows all arguments in each line when there are multiple args', () => {
const spy = sinon.spy(logger, 'log')

logger.logFormatted({ args: [1, 2, 3] })

expect(spy).to.have.been.calledWith(`%cArgs:`, 'font-weight: bold')
expect(spy).to.have.been.calledWith(`%c [0]:`, 'font-weight: bold', 1)
expect(spy).to.have.been.calledWith(`%c [1]:`, 'font-weight: bold', 2)
expect(spy).to.have.been.calledWith(`%c [2]:`, 'font-weight: bold', 3)
expect(spyLog).to.have.been.calledWith(`%cArgs:`, 'font-weight: bold')
expect(spyLog).to.have.been.calledWith(`%c [0]:`, 'font-weight: bold', 1)
expect(spyLog).to.have.been.calledWith(`%c [1]:`, 'font-weight: bold', 2)
expect(spyLog).to.have.been.calledWith(`%c [2]:`, 'font-weight: bold', 3)
})

describe('_logValues', () => {
let spyTrim = sinon.spy(_, 'trim')

afterEach(() => {
// reset after each unit test
spyTrim.resetHistory()
})

it('should not call trim', () => {
logger._logValues({})
logger._logValues({ test: {} })
logger._logValues(null)
logger._logValues(undefined)

expect(spyTrim.getCalls()).to.have.length(0)
})

// The positive unit tests to capture if log has been called are already written in
// the 'cy.log() shows all arguments in each line when there are multiple args' unit test.
})
})

4 comments on commit 7a4c59a

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7a4c59a Oct 11, 2021

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/8.6.0/circle-develop-7a4c59a12cf3c4c4ac64dbbf5896f7931118fc43/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7a4c59a Oct 11, 2021

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/8.6.0/appveyor-develop-7a4c59a12cf3c4c4ac64dbbf5896f7931118fc43/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7a4c59a Oct 11, 2021

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/8.6.0/appveyor-develop-7a4c59a12cf3c4c4ac64dbbf5896f7931118fc43/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 7a4c59a Oct 11, 2021

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/8.6.0/circle-develop-7a4c59a12cf3c4c4ac64dbbf5896f7931118fc43/cypress.tgz

Please sign in to comment.