Skip to content

Commit

Permalink
fix(gatsby-cli): Address an issue that caused empty logs to print und…
Browse files Browse the repository at this point in the history
…efined (#23000)

* fix(gatsby-cli): Address an issue that caused empty logs to print undefined

* fix lint
  • Loading branch information
blainekasten committed Apr 10, 2020
1 parent ec80671 commit be85f2e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
30 changes: 30 additions & 0 deletions packages/gatsby-cli/src/reporter/__tests__/patch-console.ts
@@ -0,0 +1,30 @@
import { patchConsole } from "../patch-console"
import { reporter as gatsbyReporter } from "../reporter"

describe(`patchConsole`, () => {
const reporter = {
log: 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`)
})
})
18 changes: 15 additions & 3 deletions packages/gatsby-cli/src/reporter/patch-console.ts
Expand Up @@ -7,13 +7,25 @@ import { reporter as gatsbyReporter } from "./reporter"

export const patchConsole = (reporter: typeof gatsbyReporter): void => {
console.log = (format: any, ...args: any[]): void => {
reporter.log(util.format(format, ...args))
if (format) {
reporter.log(util.format(format, ...args))
return
}
reporter.log()
}
console.warn = (format: any, ...args: any[]): void => {
reporter.warn(util.format(format, ...args))
if (format) {
reporter.warn(util.format(format, ...args))
return
}
reporter.warn()
}
console.info = (format: any, ...args: any[]): void => {
reporter.info(util.format(format, ...args))
if (format) {
reporter.info(util.format(format, ...args))
return
}
reporter.info()
}
console.error = (format: any, ...args: any[]): void => {
reporter.error(util.format(format, ...args))
Expand Down
8 changes: 4 additions & 4 deletions packages/gatsby-cli/src/reporter/reporter.ts
Expand Up @@ -164,13 +164,13 @@ class Reporter {
}
}

success = (text: string): CreateLogAction =>
success = (text?: string): CreateLogAction =>
reporterActions.createLog({ level: LogLevels.Success, text })
info = (text: string): CreateLogAction =>
info = (text?: string): CreateLogAction =>
reporterActions.createLog({ level: LogLevels.Info, text })
warn = (text: string): CreateLogAction =>
warn = (text?: string): CreateLogAction =>
reporterActions.createLog({ level: LogLevels.Warning, text })
log = (text: string): CreateLogAction =>
log = (text?: string): CreateLogAction =>
reporterActions.createLog({ level: LogLevels.Log, text })

pendingActivity = reporterActions.createPendingActivity
Expand Down

0 comments on commit be85f2e

Please sign in to comment.