Skip to content

jest/no-untyped-mock-factory Style

🛠️ An auto-fix is available for this rule for some violations.

What it does

This rule triggers a warning if mock() or doMock() is used without a generic type parameter or return type.

Why is this bad?

By default, jest.mock and jest.doMock allow any type to be returned by a mock factory. A generic type parameter can be used to enforce that the factory returns an object with the same shape as the original module, or some other strict type. Requiring a type makes it easier to use TypeScript to catch changes needed in test mocks when the source module changes.

Example

// invalid

typescript
jest.mock("../moduleName", () => {
  return jest.fn(() => 42);
});

jest.mock("./module", () => ({
  ...jest.requireActual("./module"),
  foo: jest.fn(),
}));

jest.mock("random-num", () => {
  return jest.fn(() => 42);
});

// valid

typescript
// Uses typeof import()
jest.mock<typeof import("../moduleName")>("../moduleName", () => {
  return jest.fn(() => 42);
});

jest.mock<typeof import("./module")>("./module", () => ({
  ...jest.requireActual("./module"),
  foo: jest.fn(),
}));

// Uses custom type
jest.mock<() => number>("random-num", () => {
  return jest.fn(() => 42);
});

// No factory
jest.mock("random-num");

// Virtual mock
jest.mock(
  "../moduleName",
  () => {
    return jest.fn(() => 42);
  },
  { virtual: true },
);

References

Released under the MIT License.