Skip to content

Commit 42dc014

Browse files
committed
add tests
1 parent 8e54fc2 commit 42dc014

4 files changed

Lines changed: 141 additions & 2 deletions

File tree

knip.jsonc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "./node_modules/knip/schema-jsonc.json",
3+
"entry": [
4+
"src/index.ts",
5+
"src/test-utils/*.ts"
6+
],
7+
"ignoreDependencies": [
8+
"@tanstack/intent"
9+
]
10+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`lint command > runKnip > detects unused exports and unused files 1`] = `
4+
[
5+
{
6+
"code": "unused-file",
7+
"column": undefined,
8+
"file": "src/unused-file.ts",
9+
"level": "warning",
10+
"line": undefined,
11+
"message": "Unused file",
12+
"tool": "knip",
13+
},
14+
{
15+
"code": "unused-export",
16+
"column": 20,
17+
"file": "src/used.ts",
18+
"level": "warning",
19+
"line": 3,
20+
"message": "Unused export 'unusedExport'",
21+
"tool": "knip",
22+
},
23+
]
24+
`;
25+
26+
exports[`lint command > runOxlint > detects violations in bad code 1`] = `
27+
[
28+
{
29+
"code": "eslint(no-unused-vars)",
30+
"column": 5,
31+
"file": "src/index.ts",
32+
"level": "error",
33+
"line": 1,
34+
"message": "Variable 'x' is declared but never used. Unused variables should start with a '_'.",
35+
"tool": "oxlint",
36+
},
37+
{
38+
"code": "eslint(no-var)",
39+
"column": 1,
40+
"file": "src/index.ts",
41+
"level": "error",
42+
"line": 1,
43+
"message": "Unexpected var, use let or const instead.",
44+
"tool": "oxlint",
45+
},
46+
]
47+
`;

src/commands/lint.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
});

src/commands/lint.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface Violation {
2222

2323
// -- Tool Runners --
2424

25-
async function runOxlint(targets: string[], fix?: boolean): Promise<Violation[]> {
25+
export async function runOxlint(targets: string[], fix?: boolean): Promise<Violation[]> {
2626
const args = ["-c", oxlintConfig, "--format=json", ...targets];
2727
if (fix) args.push("--fix");
2828
const result = await x(local("oxlint"), args, { throwOnError: false });
@@ -74,7 +74,7 @@ interface KnipJsonReport {
7474
issues: KnipJsonIssue[];
7575
}
7676

77-
async function runKnip(): Promise<Violation[]> {
77+
export async function runKnip(): Promise<Violation[]> {
7878
const args = ["--no-progress", "--reporter", "json"];
7979
const result = await x(local("knip"), args, { throwOnError: false });
8080
if (!result.stdout.trim()) return [];

0 commit comments

Comments
 (0)