Search code examples
node.jsunit-testingjestjschalk

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:

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


Solution

  • // newTest.js
    
    const chalk = require('chalk');
    
    function functionUnderTest(delta, apiPath) {
      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.