Skip to content

Commit

Permalink
Merge pull request #107 from line/test/add-test-case-for-parsing-arg
Browse files Browse the repository at this point in the history
test: add test case for parsing args correctly
  • Loading branch information
kazushisan authored Dec 16, 2024
2 parents efb37f5 + b917842 commit 50b9261
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/fixtures/parse_args/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const a = 'a';

export const a2 = 'a2';
3 changes: 3 additions & 0 deletions test/fixtures/parse_args/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { a } from '@/a.js';

console.log(a);
8 changes: 8 additions & 0 deletions test/fixtures/parse_args/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"paths": {
// this can be anything, it's just specified to check if we're reading the tsconfig correctly
"@/*": ["./src/*"]
}
}
}
3 changes: 3 additions & 0 deletions test/fixtures/parse_args/tsconfig.main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["src/main.ts"]
}
98 changes: 98 additions & 0 deletions test/parse_args.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import assert from 'node:assert/strict';
import * as child_process from 'node:child_process';
import { promisify } from 'node:util';
import { dirname, resolve } from 'node:path';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import stripAnsi from 'strip-ansi';

const exec = promisify(child_process.exec);

const projectRoot = resolve(
dirname(fileURLToPath(import.meta.url)),
'fixtures/parse_args',
);

const bin = resolve(dirname(fileURLToPath(import.meta.url)), '../dist/cli.js');

await exec('npm run build', { cwd: projectRoot });

describe('parse_args', () => {
it('should show help', async () => {
const { stdout } = await exec(`node ${bin} --help`);

const output = stripAnsi(stdout);

assert.equal(
output,
`
Usage:
tsr [options] [...entrypoints]
Options:
-p, --project <file> Path to your tsconfig.json
-w, --write Write changes in place
-r, --recursive Recursively look into files until the project is clean
--include-d-ts Check for unused code in .d.ts files
-h, --help Display this message
-v, --version Display version number
Examples:
# Check unused code for a project with an entrypoint of src/main.ts
tsr 'src/main\\.ts$'
# Write changes in place
tsr --write 'src/main\\.ts$'
# Check unused code for a project with a custom tsconfig.json
tsr --project tsconfig.app.json 'src/main\\.ts$'
# Check unused code for a project with multiple entrypoints in src/pages
tsr 'src/pages/.*\\.ts$'
`,
);
});

it('should use default tsconfig.json', async () => {
const { stdout, code } = await exec(`node ${bin} 'src/main\\.ts$'`, {
cwd: projectRoot,
})
.then((res) => ({ ...res, code: 0 }))
.catch((e) => e as { stdout: string; code: number });

const output = stripAnsi(stdout.toString());

assert.equal(
output,
`tsconfig tsconfig.json
Project has 2 files. Found 1 entrypoint file
export src/a.ts:2:0 'a2'
✖ remove 1 export
`,
);
assert.equal(code, 1);
});

it('should work with custom tsconfig.json', async () => {
const { stdout, code } = await exec(
`node ${bin} --project tsconfig.main.json 'src/main\\.ts$'`,
{
cwd: projectRoot,
},
)
.then((res) => ({ ...res, code: 0 }))
.catch((e) => e as { stdout: string; code: number });

const output = stripAnsi(stdout.toString());

assert.equal(
output,
`tsconfig tsconfig.main.json
Project has 1 file. Found 1 entrypoint file
✔ all good!
`,
);
assert.equal(code, 0);
});
});

0 comments on commit 50b9261

Please sign in to comment.