|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import { createFixture } from "../test-utils/index.ts"; |
| 3 | +import { runOxlint, runKnip } from "./lint.ts"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | + |
| 6 | +describe("lint command", () => { |
| 7 | + let originalCwd: string; |
| 8 | + let fixture: Awaited<ReturnType<typeof createFixture>>; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + originalCwd = process.cwd(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(async () => { |
| 15 | + process.chdir(originalCwd); |
| 16 | + if (fixture) await fixture.cleanup(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe("runOxlint", () => { |
| 20 | + it("detects violations in bad code", async () => { |
| 21 | + fixture = await createFixture({ |
| 22 | + src: { |
| 23 | + "index.ts": "var x = 1;", |
| 24 | + }, |
| 25 | + }); |
| 26 | + process.chdir(fileURLToPath(fixture.root)); |
| 27 | + |
| 28 | + const violations = await runOxlint(["./src"]); |
| 29 | + |
| 30 | + expect(violations).not.toEqual([]); |
| 31 | + expect(violations).toMatchSnapshot(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("returns empty array for clean code", async () => { |
| 35 | + fixture = await createFixture({ |
| 36 | + src: { |
| 37 | + "index.ts": ` |
| 38 | + export const x = 1; |
| 39 | + `, |
| 40 | + }, |
| 41 | + }); |
| 42 | + process.chdir(fileURLToPath(fixture.root)); |
| 43 | + |
| 44 | + const violations = await runOxlint(["./src"]); |
| 45 | + |
| 46 | + expect(violations).toEqual([]); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe("runKnip", () => { |
| 51 | + it("detects unused exports and unused files", async () => { |
| 52 | + fixture = await createFixture({ |
| 53 | + "package.json": { |
| 54 | + name: "test-pkg", |
| 55 | + version: "1.0.0", |
| 56 | + type: "module", |
| 57 | + exports: "./src/index.ts", |
| 58 | + }, |
| 59 | + src: { |
| 60 | + "index.ts": ` |
| 61 | + import { used } from "./used"; |
| 62 | + console.log(used); |
| 63 | + export const value = 42; |
| 64 | + `, |
| 65 | + "used.ts": ` |
| 66 | + export const used = "used"; |
| 67 | + export const unusedExport = "unusedExport"; |
| 68 | + `, |
| 69 | + "unused-file.ts": ` |
| 70 | + export const unused = "unused"; |
| 71 | + `, |
| 72 | + }, |
| 73 | + }); |
| 74 | + process.chdir(fileURLToPath(fixture.root)); |
| 75 | + |
| 76 | + const violations = await runKnip(); |
| 77 | + |
| 78 | + expect(violations).not.toEqual([]); |
| 79 | + expect(violations).toMatchSnapshot(); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments