Skip to content

Commit 7a4c59a

Browse files
authored
fix: do not attempt to trim objects when printing to console (#18341)
1 parent 1d08280 commit 7a4c59a

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

packages/runner-shared/src/logger.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export const logger = {
3030
_.each(formattedLog, (value, key) => {
3131
// don't log empty strings
3232
// _.trim([]) returns '' but we want to log empty arrays, so account for that
33-
if (_.trim(value) === '' && !_.isArray(value)) return
33+
// Skip trim if we know value is an object
34+
if (typeof value !== 'object' && _.trim(value) === '' && !_.isArray(value)) return
3435

3536
this.log(`%c${key}`, 'font-weight: bold', value)
3637
})
Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
const sinon = require('sinon')
22
const { logger } = require('./logger')
3+
import _ from 'lodash'
34

45
describe('logger', () => {
6+
let spyLog = sinon.spy(logger, 'log')
7+
8+
afterEach(() => {
9+
// reset after each unit test
10+
spyLog.resetHistory()
11+
})
12+
513
// https://github.com/cypress-io/cypress/issues/17542
614
it('cy.log() shows all arguments in each line when there are multiple args', () => {
7-
const spy = sinon.spy(logger, 'log')
8-
915
logger.logFormatted({ args: [1, 2, 3] })
1016

11-
expect(spy).to.have.been.calledWith(`%cArgs:`, 'font-weight: bold')
12-
expect(spy).to.have.been.calledWith(`%c [0]:`, 'font-weight: bold', 1)
13-
expect(spy).to.have.been.calledWith(`%c [1]:`, 'font-weight: bold', 2)
14-
expect(spy).to.have.been.calledWith(`%c [2]:`, 'font-weight: bold', 3)
17+
expect(spyLog).to.have.been.calledWith(`%cArgs:`, 'font-weight: bold')
18+
expect(spyLog).to.have.been.calledWith(`%c [0]:`, 'font-weight: bold', 1)
19+
expect(spyLog).to.have.been.calledWith(`%c [1]:`, 'font-weight: bold', 2)
20+
expect(spyLog).to.have.been.calledWith(`%c [2]:`, 'font-weight: bold', 3)
21+
})
22+
23+
describe('_logValues', () => {
24+
let spyTrim = sinon.spy(_, 'trim')
25+
26+
afterEach(() => {
27+
// reset after each unit test
28+
spyTrim.resetHistory()
29+
})
30+
31+
it('should not call trim', () => {
32+
logger._logValues({})
33+
logger._logValues({ test: {} })
34+
logger._logValues(null)
35+
logger._logValues(undefined)
36+
37+
expect(spyTrim.getCalls()).to.have.length(0)
38+
})
39+
40+
// The positive unit tests to capture if log has been called are already written in
41+
// the 'cy.log() shows all arguments in each line when there are multiple args' unit test.
1542
})
1643
})

0 commit comments

Comments
 (0)