jest/no-export
Disallow exports from a file that has tests
When another file imports from a test file, the whole test file runs, including its test() calls, so its tests run a second time as part of the importing file. Put shared helpers in a separate file.
The rule only checks files named like tests. That means the name ends in test or spec before the extension, such as cart.test.ts, or the file is under a __tests__ directory. A file with no test or it call can export anything.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"jest/no-export" = "error" # or "warn"Examples#
src/cart.test.ts1 problem
import { expect, test } from "@jest/globals";
export function makeCartDo not export from a test file.() {
return { items: [] };
}
test("starts empty", () => {
expect(makeCart().items).toEqual([]);
});Not reported, with the helper in a separate file:
helpers.ts
export function makeCart() {
return { items: [] };
}cart.test.tsNo problems
import { expect, test } from "@jest/globals";
import { makeCart } from "./helpers";
test("starts empty", () => {
expect(makeCart().items).toEqual([]);
});Exported types are reported too:
cart.test.ts1 problem
import { expect, test } from "@jest/globals";
import { makeCart } from "./helpers";
export type CartDo not export from a test file. = ReturnType<typeof makeCart>;
test("starts empty", () => {
expect(makeCart().items).toEqual([]);
});