diff --git a/packages/gatsby-cli/src/reporter/__tests__/patch-console.ts b/packages/gatsby-cli/src/reporter/__tests__/patch-console.ts index 6b9b90384b60f..72d1cb43127e9 100644 --- a/packages/gatsby-cli/src/reporter/__tests__/patch-console.ts +++ b/packages/gatsby-cli/src/reporter/__tests__/patch-console.ts @@ -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(``) + }) + }) }) }) diff --git a/packages/gatsby-cli/src/reporter/patch-console.ts b/packages/gatsby-cli/src/reporter/patch-console.ts index acdc2ecbccafe..f10d27e0e96c6 100644 --- a/packages/gatsby-cli/src/reporter/patch-console.ts +++ b/packages/gatsby-cli/src/reporter/patch-console.ts @@ -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))