1

I've got a library that's giving me some trouble in my jest tests.

This library is included throughout my project, and it has an annoyingFunction that has a console.error in it. So, whenever I run a test, I naturally get unwanted console.error messages all over the place.

I don't want to mock out the whole library, just the annoyingFunction, so I put this in my setup file:

jest.mock('myLibrary', () => ({
   ...jest.requireActual('myLibrary'),
   annoyingFunction: jest.fn(),
}));

This is being run, however, the original annoyingFunction is still being called, polluting my tests with console.error calls.

If I console log my mock, I clearly see annoyingFunction: [Function: mockConstructor], so the mock is working, but for some reason, the original function from the library is still being called.

What am I missing here? Is there something wrong with my initial setup of the mock?

Max Millington
  • 4,378
  • 4
  • 27
  • 34

1 Answers1

1

There could be a couple of things wrong, but my guess is that annoyingFunction is called internally within the library. Consider the following example, which doesn't do what you might expect it to:

foo.js

function add(a, b) {
  return a + b;
}

function subtract(a, b) {
  return a - b;
}

function multiply(a, b) {
  let total = 0;
  for (let i = 0; i < b; i++) {
    total = add(total, a);
  }
  return total;
}

export {
  add,
  subtract,
  multiply
};

foo_test.js

import * as Operations from "./foo.js";

jest.mock("./foo.js", () => ({
  ...jest.requireActual("./foo.js"),
  add: () => -999,
}));

describe("all the things", () => {
  // Here, the mock does what you would expect, because you're calling the
  // exported function "add."
  it("can add", () => {
    expect(Operations.add(1, 2)).toEqual(-999);
  });
  it("can subtract", () => {
    expect(Operations.subtract(1, 2)).toEqual(-1);
  });
  // Here, the mock doesn't do what you would expect. because unbeknownst to
  // you, `multiply` calls `add` _within_ the module code.  The mock has no
  // effect in this case.
  it("can multiply", () => {
    expect(Operations.multiply(1, 2)).toEqual(2);
  });
});

I'm not really sure what you can do about this, except to mock exported methods of the library until you can control the result.

Or...you could jest.spyOn console.error for whichever test is giving you a problem, and reset the spy afterward.

const consoleErrorSpy = jest.spyOn(console, "error");
//...do your test...
consoleErrorSpy.mockRestore();

Hope that helps!

Matt Morgan
  • 4,900
  • 4
  • 21
  • 30
  • You're right, this is exactly what is happening. I guess I erroneously thought that when I mocked out the function, it would replace all calls of the function, including the ones internal to the library itself. – Max Millington May 17 '22 at 15:47
  • The issue with mocking out the console error is that `annoyingFunction` is actually run in virtually every test in a suite of thousands. So I wouldn't want to have to mock it out for each individual test. I considered mocking it globally, since I'm not sure I ever want console.error in my test suite, but was hesitant to do so since I'm unsure what the unintended side effects would be – Max Millington May 17 '22 at 15:49
  • 1
    Yes, I think it might be hard to prove you haven't clobbered a legit call to `console.error`. Maybe time to fork the library! – Matt Morgan May 17 '22 at 16:12