Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 10, 2020
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
30 changes: 30 additions & 0 deletions packages/gatsby-cli/src/reporter/__tests__/patch-console.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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