close

unbound-method

Added in v0.9.4

Configuration

rslint.config.ts
import { defineConfig, jestPlugin } from '@rslint/core';

export default defineConfig([
  jestPlugin.configs.recommended,
  {
    rules: {
      'jest/unbound-method': 'error',
    },
  },
]);

Rule Details

Enforce unbound methods are called with their expected scope. This rule extends @typescript-eslint/unbound-method with an understanding of Jest: a method may be passed unbound to expect or jest.mocked, which never call it.

Every other check of the base rule is kept. Methods passed to expect are still reported when the matcher calls them (toThrow, toThrowError, toThrowErrorMatchingSnapshot, and toThrowErrorMatchingInlineSnapshot), or when the expect chain has no called matcher.

Use this rule in place of @typescript-eslint/unbound-method for test files; enabling both reports the same references twice. The rule requires type information.

Examples of incorrect code for this rule:

class Service {
  method() {
    return this;
  }
}
const service = new Service();

expect(service.method).toThrow();
Promise.resolve().then(service.method);
const method = service.method;

Examples of correct code for this rule:

class Service {
  method() {
    return this;
  }
}
const service = new Service();

expect(service.method).toHaveBeenCalledTimes(1);
expect(service.method).not.toHaveBeenCalled();
jest.mocked(service.method).mockImplementation(() => service);

Options

This rule accepts the same options as @typescript-eslint/unbound-method, such as ignoreStatic.

{ "jest/unbound-method": ["error", { "ignoreStatic": true }] }

Original Documentation