t3-code-android-nightly/oxlint-plugin-t3code/rules/no-wall-clock-in-tests.test.ts

47 lines
1.2 KiB
TypeScript

import { describe } from "@effect/vitest";
import { createOxlintRuleHarness } from "../test/utils.ts";
const rule = createOxlintRuleHarness("t3code/no-wall-clock-in-tests", {
filename: "fixture.test.ts",
});
describe("t3code/no-wall-clock-in-tests", () => {
rule.valid(
"allows fixed dates and readings that seed mocks",
`
import { vi } from "vitest";
const epoch = new Date("2026-01-01T00:00:00.000Z");
export const later = new Date(epoch.getTime() + 1_000);
vi.setSystemTime(new Date());
vi.spyOn(Date, "now").mockReturnValue(Date.now() + 5_000);
const clock = vi.fn(() => performance.now());
clock.mockImplementation(() => Number(process.hrtime.bigint()));
vi.mock("./clock", () => ({ now: () => Date.now() }));
`,
);
rule.invalid(
"reports Date.now fixtures",
`
export const session = { expiresAt: Date.now() + 60_000 };
`,
);
rule.invalid(
"reports new Date() with no arguments",
`
export const createdAt = new Date().toISOString();
`,
);
});
const productionRule = createOxlintRuleHarness("t3code/no-wall-clock-in-tests");
productionRule.valid(
"ignores non-test files",
`
export const now = Date.now();
`,
);