|
| 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