Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
})
})