Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

391
Views
How to use Jest to test a console.log that uses chalk?

Writing a Node CLI in my module I have a console.log with chalk, example:

console.log(
  chalk.green(`${delta}:`),
  chalk.white(`\nAPI: ${apiPath}`),
)

when I run a Jest code coverage --coverage I'm reminded that I didn't have a test so I wrote:

test(`Test console log`, async () => {
  await mod(params)
  await expect(console.log).toBe(`${delta}:\nAPI: ${apiPath}`)
})

but I get an error of:

Expected: "string"
Received: [Function log]

Second attempt from research I tried:

test(`Test console log`, async () => {
  await mod(params)
  await expect(console.log).toHaveBeenCalledWith(`${delta}:\nAPI: ${apiPath}`)
})

but I get an error of:

Received has type:  function
Received has value: [Function log]

Research:

  • How do I test a jest console.log
  • Console.log statements output nothing at all in Jest
  • How to test console.log output using Jest or other Javascript Testing Framework?

With Jest how can I test a console.log that uses chalk?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

// newTest.js

const chalk = require('chalk');

function functionUnderTest() {
  console.log(chalk.green(`${delta}:`), chalk.white(`\nAPI: ${apiPath}`));
}

module.exports = functionUnderTest;


// newTest.test.js

const functionUnderTest = require('./newTest');
const chalk = require('chalk');

jest.mock('chalk', () => ({
  green: jest.fn(),
  white: jest.fn(),
}));

it('calls console.log and chalk.blue with correct arguments', () => {
  const spy = jest.spyOn(global.console, 'log');
  chalk.green.mockReturnValueOnce('test-blue');
  chalk.white.mockReturnValueOnce('test-white');

  functionUnderTest(5, 'my-path');

  expect(chalk.green).toHaveBeenCalledWith('5:');
  expect(chalk.white).toHaveBeenCalledWith('\nAPI: my-path');
  expect(global.console.log).toHaveBeenCalledWith('test-blue', 'test-white');

  spy.mockRestore();
});

To access global objects you have to use global context (Jest: how to mock console when it is used by a third-party-library?). You do that by spying log method of the console global object.

The important part regarding the test itself is that there are 2 dependencies that need to be mocked, the console.log (which is done in the spy) and the chalk (which i'm doing with the jest.mock) where i say that it has a property named green which is a mocked function (and white). Here what should be tested is that the console.log prints what is returned from the chalk.green call. So assigned a dummy string to be the result of the chalk.green call (test-result) and assert that console.log is called with that same string. The same for the white mock function.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!