Skip to content

Commit f1baf97

Browse files
committed
test: add test case for parsing arg
1 parent baef6c9 commit f1baf97

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

test/fixtures/parse_args/src/a.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const a = 'a';
2+
3+
export const a2 = 'a2';

test/fixtures/parse_args/src/main.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { a } from '@/a.js';
2+
3+
console.log(a);
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"paths": {
4+
// this can be anything, it's just specified to check if we're reading the tsconfig correctly
5+
"@/*": ["./src/*"]
6+
}
7+
}
8+
}

test/parse_args.test.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import assert from 'node:assert/strict';
2+
import * as child_process from 'node:child_process';
3+
import { promisify } from 'node:util';
4+
import { dirname, resolve } from 'node:path';
5+
import { describe, it } from 'node:test';
6+
import { fileURLToPath } from 'node:url';
7+
import stripAnsi from 'strip-ansi';
8+
9+
const exec = promisify(child_process.exec);
10+
11+
const projectRoot = resolve(
12+
dirname(fileURLToPath(import.meta.url)),
13+
'fixtures/parse_args',
14+
);
15+
16+
const bin = resolve(dirname(fileURLToPath(import.meta.url)), '../dist/cli.js');
17+
18+
await exec('npm run build', { cwd: projectRoot });
19+
20+
describe('parse_args', () => {
21+
it('should show help', async () => {
22+
const { stdout } = await exec(`node ${bin} --help`);
23+
24+
const output = stripAnsi(stdout);
25+
26+
assert.equal(
27+
output,
28+
`
29+
Usage:
30+
tsr [options] [...entrypoints]
31+
32+
Options:
33+
-p, --project <file> Path to your tsconfig.json
34+
-w, --write Write changes in place
35+
-r, --recursive Recursively look into files until the project is clean
36+
--include-d-ts Check for unused code in .d.ts files
37+
-h, --help Display this message
38+
-v, --version Display version number
39+
40+
Examples:
41+
# Check unused code for a project with an entrypoint of src/main.ts
42+
tsr 'src/main\\.ts$'
43+
44+
# Write changes in place
45+
tsr --write 'src/main\\.ts$'
46+
47+
# Check unused code for a project with a custom tsconfig.json
48+
tsr --project tsconfig.app.json 'src/main\\.ts$'
49+
50+
# Check unused code for a project with multiple entrypoints in src/pages
51+
tsr 'src/pages/.*\\.ts$'
52+
53+
`,
54+
);
55+
});
56+
57+
it('should use default tsconfig.json', async () => {
58+
const { stdout, code } = await exec(`node ${bin} 'src/main\\.ts$'`, {
59+
cwd: projectRoot,
60+
})
61+
.then((res) => ({ ...res, code: 0 }))
62+
.catch((e) => e as { stdout: string; code: number });
63+
64+
const output = stripAnsi(stdout.toString());
65+
66+
assert.equal(
67+
output,
68+
`tsconfig tsconfig.json
69+
Project has 2 files. Found 1 entrypoint file
70+
export src/a.ts:2:0 'a2'
71+
✖ remove 1 export
72+
`,
73+
);
74+
assert.equal(code, 1);
75+
});
76+
});

0 commit comments

Comments
 (0)