bun:test/no-restricted-jest-methods
Disallow the jest and vi methods that you list
Some teams agree not to use parts of the jest object, like module mocks, fake timers or spies. This rule reports the methods you list, with your own message if you give one, so the agreement doesn't depend on review.
The rule does nothing until you give it a list.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"bun:test/no-restricted-jest-methods" = "error" # or "warn"Examples#
These examples restrict advanceTimersByTime and spyOn.
example.tswith
{ "advanceTimersByTime": null, "spyOn": "Pass the dependency in instead of spying on it" }2 problemsimport { expect, jest, test } from "bun:test";
test("retries after a second", () => {
const send = jest.spyOnPass the dependency in instead of spying on it(mailer, "send");
retry(send);
jest.advanceTimersByTimeUse of advanceTimersByTime is not allowed(1000);
expect(send).toHaveBeenCalledTimes(2);
});example.tswith
{ "advanceTimersByTime": null, "spyOn": "Pass the dependency in instead of spying on it" }No problemsimport { expect, jest, test } from "bun:test";
test("retries once", () => {
const send = jest.fn();
retry(send);
expect(send).toHaveBeenCalledTimes(2);
});vi is checked the same way as jest:
example.tswith
{ "spyOn": "Pass the dependency in instead of spying on it" }1 problemimport { vi } from "bun:test";
const send = vi.spyOnPass the dependency in instead of spying on it(mailer, "send");Only methods of jest and vi are checked. The spyOn and mock functions that "bun:test" exports aren't:
example.tswith
{ "spyOn": "Pass the dependency in instead of spying on it" }No problemsimport { spyOn } from "bun:test";
const send = spyOn(mailer, "send");Options#
The rule takes one object. Each key is a method name, and its value is the message to report.
bunfig.toml
[lint.rules]
"bun:test/no-restricted-jest-methods" = ["error", {
spyOn = "Pass the dependency in instead of spying on it",
}]In a JSON or JavaScript config, use null in place of the message to get the default message: Use of `spyOn` is not allowed.