Skip to content

Commit

Permalink
fix(gatsby-cli): Fix console methods incorrectly handling falsy values (
Browse files Browse the repository at this point in the history
#23021)

* fix(gatsby-cli): Fix console.* methods incorectly handling falsy values

* handle more situations
  • Loading branch information
blainekasten authored Apr 13, 2020
1 parent 854241b commit 66a1b7f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 38 deletions.
70 changes: 50 additions & 20 deletions packages/gatsby-cli/src/reporter/__tests__/patch-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,57 @@ import { reporter as gatsbyReporter } from "../reporter"
describe(`patchConsole`, () => {
const reporter = {
log: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
}
patchConsole((reporter as unknown) as typeof gatsbyReporter)

beforeEach(reporter.log.mockReset)

it(`handles an empty call`, () => {
console.log()

// intentionally empty arguments
expect(reporter.log).toBeCalledWith()
})

it(`handles multiple arguments`, () => {
console.log(`foo`, `bar`, `baz`)

expect(reporter.log).toBeCalledWith(`foo bar baz`)
})

it(`handles formatting`, () => {
console.log(`%s %d`, `bar`, true)

expect(reporter.log).toBeCalledWith(`bar 1`)
patchConsole((reporter as unknown) as typeof gatsbyReporter)
;[`info`, `log`, `warn`].forEach(method => {
describe(method, () => {
beforeEach(reporter[method].mockReset)

it(`handles an empty call`, () => {
console[method]()

expect(reporter[method]).toBeCalledWith(``)
})

it(`handles multiple arguments`, () => {
console[method](`foo`, `bar`, `baz`)

expect(reporter[method]).toBeCalledWith(`foo bar baz`)
})

it(`handles formatting`, () => {
console[method](`%s %d`, `bar`, true)

expect(reporter[method]).toBeCalledWith(`bar 1`)
})

it(`handles normal values`, () => {
console[method](1)
console[method](0)
console[method](true)
console[method](false)
console[method]([1, true, false, {}])
console[method]({ 1: 1, true: true, false: `false`, obj: {} })

expect(reporter[method].mock.calls[0][0]).toBe(`1`)
expect(reporter[method].mock.calls[1][0]).toBe(`0`)
expect(reporter[method].mock.calls[2][0]).toBe(`true`)
expect(reporter[method].mock.calls[3][0]).toBe(`false`)
expect(reporter[method].mock.calls[4][0]).toBe(`[ 1, true, false, {} ]`)
expect(reporter[method].mock.calls[5][0]).toBe(
`{ '1': 1, true: true, false: 'false', obj: {} }`
)
})

it(`handles undefined variables`, () => {
let a
console[method](a)

expect(reporter[method]).toBeCalledWith(``)
})
})
})
})
27 changes: 9 additions & 18 deletions packages/gatsby-cli/src/reporter/patch-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,17 @@ import util from "util"
import { reporter as gatsbyReporter } from "./reporter"

export const patchConsole = (reporter: typeof gatsbyReporter): void => {
console.log = (format: any, ...args: any[]): void => {
if (format) {
reporter.log(util.format(format, ...args))
return
}
reporter.log()
console.log = (...args: any[]): void => {
const [format, ...rest] = args
reporter.log(util.format(format === undefined ? `` : format, ...rest))
}
console.warn = (format: any, ...args: any[]): void => {
if (format) {
reporter.warn(util.format(format, ...args))
return
}
reporter.warn()
console.warn = (...args: any[]): void => {
const [format, ...rest] = args
reporter.warn(util.format(format === undefined ? `` : format, ...rest))
}
console.info = (format: any, ...args: any[]): void => {
if (format) {
reporter.info(util.format(format, ...args))
return
}
reporter.info()
console.info = (...args: any[]): void => {
const [format, ...rest] = args
reporter.info(util.format(format === undefined ? `` : format, ...rest))
}
console.error = (format: any, ...args: any[]): void => {
reporter.error(util.format(format, ...args))
Expand Down

0 comments on commit 66a1b7f

Please sign in to comment.