Skip to content

Commit

Permalink
nx-update-ts-references: Implement test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLey committed Dec 7, 2024
1 parent 7bb3633 commit 5d0eaa7
Show file tree
Hide file tree
Showing 11 changed files with 819 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-gorillas-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nx-update-ts-references": patch
---

Implement test suite
46 changes: 46 additions & 0 deletions apps/nx-update-ts-references/.c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"reporter": [
"html",
"text-summary"
],
"exclude": [
"coverage/**",
"eslint.config.js",
"src/tests/**"
],
"exclude-after-remap": true,
"extension": [
".js",
".cjs",
".mjs",
".ts",
".cts",
".mts",
".tsx",
".jsx"
],

"lines": 100,
"statements": 100,
"functions": 100,
"branches": 100,

"watermarks": {
"lines": [
100,
95
],
"functions": [
100,
95
],
"branches": [
100,
95
],
"statements": [
100,
95
]
}
}
4 changes: 4 additions & 0 deletions apps/nx-update-ts-references/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"npm-update-ts-references": {},
"tsc": {},
"npm-populate-files": {},
"coverage-reset": {},
"mocha-unit-test": {},
"mocha-integration-test": {},
"coverage-report": {},
"analyze:_": {},
"analyze:format": {},
"analyze:lint": {},
Expand Down
4 changes: 4 additions & 0 deletions apps/nx-update-ts-references/src/tests/chai-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { use } from 'chai';
import chaiAsPromised from 'chai-as-promised';

export const { expect } = use(chaiAsPromised);
64 changes: 64 additions & 0 deletions apps/nx-update-ts-references/src/tests/integration/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { exec } from 'node:child_process';
import Path from 'node:path';
import { promisify } from 'node:util';
import { suite, test } from 'mocha-chain';
import { expect } from '../chai-hooks.js';

const execAsync = promisify(exec);
const projectRoot = Path.join(import.meta.dirname, '../../..');

suite('cli', () => {
test('--help', async () => {
const result = await execAsync('./bin.mjs --help');

expect(result.stdout).to.contain(
"Write tsconfig.json's references field based on Nx detected dependencies"
);
expect(result.stderr).to.equal('');
});

test('--version', async () => {
const result = await execAsync('./bin.mjs --version');

expect(result.stdout).to.match(/\d+.\d+.\d+/u);
expect(result.stderr).to.equal('');
});

suite('commands', () => {
suite('default/update-ts-references', () => {
test('success', async () => {
const result = await execAsync(`./bin.mjs --project-root ${projectRoot} --ci`);

expect(result.stdout).to.contain('');
// https://github.com/nrwl/nx/issues/29244
expect(result.stderr).to.contain(
'The inspector is disabled, coverage could not be collected'
);
const stdErrWithoutWarning = result.stderr
.replace(
/\(node:\d+\) Warning: The inspector is disabled, coverage could not be collected\n/u,
''
)
.replace(
/\(Use `pnpm --trace-warnings ...` to show where the warning was created\)\n/u,
''
);
expect(stdErrWithoutWarning).to.equal('');
});

test('unknown options', async () => {
await expect(execAsync('./bin.mjs --unknown --option'))
.to.eventually.be.rejectedWith(Error)
.that.has.property('stderr')
.that.contain('Unknown arguments: unknown, option');
});

test('failure', async () => {
const fakeRoot = Path.join(projectRoot, 'does-not-exist');
await expect(
execAsync(`./bin.mjs --project-root ${fakeRoot} --ci`)
).to.eventually.be.rejectedWith(Error, `Directory not found: ${fakeRoot}`);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { suite, test } from 'mocha-chain';

suite('types', () => {
test('coverage', async () => {
await import('../../../../commands/lib/types.js');
});
});
Loading

0 comments on commit 5d0eaa7

Please sign in to comment.