Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

test: add test case for parsing args correctly #107

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading