Skip to content

Commit

Permalink
Add tests for --reporter and --preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Aug 30, 2023
1 parent b7a138a commit 85effd1
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 5 deletions.
4 changes: 4 additions & 0 deletions fixtures/cli-preprocessor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function (options) {
console.log('hi from js preprocessor');
return options;
}
4 changes: 4 additions & 0 deletions fixtures/cli-preprocessor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function (options) {
console.log('hi from ts preprocessor');
return options;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fixtures/cli-preprocessor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "@fixtures/cli-preprocessor"
}
3 changes: 3 additions & 0 deletions fixtures/cli-reporter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function () {
console.log('hi from js reporter');
}
3 changes: 3 additions & 0 deletions fixtures/cli-reporter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function () {
console.log('hi from ts reporter');
}
3 changes: 3 additions & 0 deletions fixtures/cli-reporter/node_modules/@org/reporter/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions fixtures/cli-reporter/node_modules/@org/reporter/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fixtures/cli-reporter/node_modules/knip-reporter/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fixtures/cli-reporter/node_modules/knip-reporter/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fixtures/cli-reporter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "@fixtures/cli-reporter"
}
3 changes: 3 additions & 0 deletions fixtures/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "@fixtures/cli"
}
27 changes: 27 additions & 0 deletions tests/cli-preprocessor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import assert from 'node:assert/strict';
import { execSync } from 'node:child_process';
import test from 'node:test';
import { resolve } from '../src/util/path.js';

const cwd = resolve('fixtures/cli-preprocessor');

const exec = (command: string) => {
const output = execSync(command.replace(/^knip/, 'node ../../dist/cli.js'), { cwd });
return output.toString().trim();
};

test('knip --preprocessor ./index.js', () => {
assert.equal(exec('knip --preprocessor ./index.js'), 'hi from js preprocessor');
});

test('knip --preprocessor ./index.ts', () => {
assert.equal(exec('knip --preprocessor ./index.ts'), 'hi from ts preprocessor');
});

test('knip --preprocessor knip-preprocessor', () => {
assert.equal(exec('knip --preprocessor knip-preprocessor'), 'hi from pkg preprocessor');
});

test('knip --preprocessor @org/preprocessor', () => {
assert.equal(exec('knip --preprocessor @org/preprocessor'), 'hi from scoped preprocessor');
});
27 changes: 27 additions & 0 deletions tests/cli-reporter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import assert from 'node:assert/strict';
import { execSync } from 'node:child_process';
import test from 'node:test';
import { resolve } from '../src/util/path.js';

const cwd = resolve('fixtures/cli-reporter');

const exec = (command: string) => {
const output = execSync(command.replace(/^knip/, 'node ../../dist/cli.js'), { cwd });
return output.toString().trim();
};

test('knip --reporter ./index.js', () => {
assert.equal(exec('knip --reporter ./index.js'), 'hi from js reporter');
});

test('knip --reporter ./index.ts', () => {
assert.equal(exec('knip --reporter ./index.ts'), 'hi from ts reporter');
});

test('knip --reporter knip-reporter', () => {
assert.equal(exec('knip --reporter knip-reporter'), 'hi from pkg reporter');
});

test('knip --reporter @org/reporter', () => {
assert.equal(exec('knip --reporter @org/reporter'), 'hi from scoped reporter');
});
9 changes: 4 additions & 5 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import assert from 'node:assert/strict';
import { execSync } from 'node:child_process';
import test from 'node:test';
import { helpText } from '../src/util/cli-arguments.js';
import { resolve } from '../src/util/path.js';
import { version } from '../src/version.js';

const cwd = resolve('fixtures/cli');

const exec = (command: string) => {
const output = execSync(command.replace(/^knip/, 'node --no-warnings --loader tsx src/cli.ts'));
const output = execSync(command.replace(/^knip/, 'node ../../dist/cli.js'), { cwd });
return output.toString().trim();
};

Expand All @@ -16,7 +19,3 @@ test('knip --version', () => {
test('knip --help', () => {
assert.equal(exec('knip --help'), helpText);
});

test('knip', { skip: true }, () => {
assert.equal(exec('knip'), '✂️ Excellent, Knip found no issues.');
});

0 comments on commit 85effd1

Please sign in to comment.