diff --git a/packages/gatsby-cli/src/reporter/__tests__/patch-console.ts b/packages/gatsby-cli/src/reporter/__tests__/patch-console.ts new file mode 100644 index 0000000000000..6b9b90384b60f --- /dev/null +++ b/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`) + }) +}) diff --git a/packages/gatsby-cli/src/reporter/patch-console.ts b/packages/gatsby-cli/src/reporter/patch-console.ts index 7fe5909007821..acdc2ecbccafe 100644 --- a/packages/gatsby-cli/src/reporter/patch-console.ts +++ b/packages/gatsby-cli/src/reporter/patch-console.ts @@ -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)) diff --git a/packages/gatsby-cli/src/reporter/reporter.ts b/packages/gatsby-cli/src/reporter/reporter.ts index e11c1259de8e9..87be5a5b65f35 100644 --- a/packages/gatsby-cli/src/reporter/reporter.ts +++ b/packages/gatsby-cli/src/reporter/reporter.ts @@ -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