|
1 | 1 | import { expect, test } from "bun:test"; |
2 | 2 | import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
3 | 4 | import { join } from "node:path"; |
4 | 5 |
|
5 | 6 | const repoRoot = join(import.meta.dirname, "../.."); |
@@ -34,40 +35,49 @@ function findingsForRule(stdout: string): unknown[] { |
34 | 35 | return (parsed.diagnostics ?? []).filter((item) => item.code === ruleCode); |
35 | 36 | } |
36 | 37 |
|
37 | | -test("oxlint reports bare mock.module in a *.test.ts file", async () => { |
38 | | - const dir = await mkdtemp( |
39 | | - join(import.meta.dirname, "oxlint-mock-module-banned-"), |
40 | | - ); |
41 | | - const file = join(dir, "banned.test.ts"); |
| 38 | +// bun test ./tests collects leftover *.test.ts under tests/; keep fixtures off that glob. |
| 39 | +async function withFixture( |
| 40 | + prefix: string, |
| 41 | + name: string, |
| 42 | + source: string, |
| 43 | + run: (file: string) => Promise<void>, |
| 44 | +): Promise<void> { |
| 45 | + const dir = await mkdtemp(join(tmpdir(), prefix)); |
| 46 | + const file = join(dir, name); |
42 | 47 | try { |
43 | | - await writeFile( |
44 | | - file, |
45 | | - `import { mock } from "bun:test"; |
46 | | -mock.module("./example.js", () => ({})); |
47 | | -`, |
48 | | - ); |
49 | | - const { stdout, stderr } = await runOxlint(file); |
50 | | - expect(findingsForRule(stdout).length, stderr || stdout).toBeGreaterThan(0); |
| 48 | + await writeFile(file, source); |
| 49 | + await run(file); |
51 | 50 | } finally { |
52 | 51 | await rm(dir, { recursive: true, force: true }); |
53 | 52 | } |
| 53 | +} |
| 54 | + |
| 55 | +test("oxlint reports bare mock.module in a *.test.ts file", async () => { |
| 56 | + await withFixture( |
| 57 | + "oxlint-mock-module-banned-", |
| 58 | + "banned.test.ts", |
| 59 | + `import { mock } from "bun:test"; |
| 60 | +mock.module("./example.js", () => ({})); |
| 61 | +`, |
| 62 | + async (file) => { |
| 63 | + const { stdout, stderr } = await runOxlint(file); |
| 64 | + expect(findingsForRule(stdout).length, stderr || stdout).toBeGreaterThan( |
| 65 | + 0, |
| 66 | + ); |
| 67 | + }, |
| 68 | + ); |
54 | 69 | }); |
55 | 70 |
|
56 | 71 | test("oxlint is clean when a *.test.ts file only uses withMockedModule", async () => { |
57 | | - const dir = await mkdtemp( |
58 | | - join(import.meta.dirname, "oxlint-mock-module-clean-"), |
59 | | - ); |
60 | | - const file = join(dir, "clean.test.ts"); |
61 | | - try { |
62 | | - await writeFile( |
63 | | - file, |
64 | | - `import { withMockedModule } from "../helpers/mock-module.ts"; |
| 72 | + await withFixture( |
| 73 | + "oxlint-mock-module-clean-", |
| 74 | + "clean.test.ts", |
| 75 | + `import { withMockedModule } from "../helpers/mock-module.ts"; |
65 | 76 | await withMockedModule("./example.js", () => ({})); |
66 | 77 | `, |
67 | | - ); |
68 | | - const { stdout, stderr } = await runOxlint(file); |
69 | | - expect(findingsForRule(stdout), stderr || stdout).toEqual([]); |
70 | | - } finally { |
71 | | - await rm(dir, { recursive: true, force: true }); |
72 | | - } |
| 78 | + async (file) => { |
| 79 | + const { stdout, stderr } = await runOxlint(file); |
| 80 | + expect(findingsForRule(stdout), stderr || stdout).toEqual([]); |
| 81 | + }, |
| 82 | + ); |
73 | 83 | }); |
0 commit comments