Skip to content

Commit fadb70d

Browse files
committed
feat: allow for a sub command to be the default
test: test default sub commands
1 parent 2255c7d commit fadb70d

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

.changeset/dry-months-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'nest-commander': minor
3+
---
4+
5+
Allow for a sub command to be set as the default sub command.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { TestingModule } from '@nestjs/testing';
2+
import { Stub, stubMethod } from 'hanbi';
3+
import { CommandTestFactory } from 'nest-commander-testing';
4+
import { suite } from 'uvu';
5+
import { equal } from 'uvu/assert';
6+
import { LogService } from '../../common/log.service';
7+
import { NestedModule } from '../src/nested.module';
8+
9+
export const DefaultSubCommandSuite = suite<{
10+
logMock: Stub<typeof console.log>;
11+
exitMock: Stub<typeof process.exit>;
12+
commandInstance: TestingModule;
13+
}>('Default Sub Command Suite');
14+
DefaultSubCommandSuite.before(async (context) => {
15+
context.exitMock = stubMethod(process, 'exit');
16+
context.logMock = stubMethod(console, 'log');
17+
context.commandInstance = await CommandTestFactory.createTestingCommand({
18+
imports: [NestedModule],
19+
})
20+
.overrideProvider(LogService)
21+
.useValue({
22+
log: context.logMock.handler,
23+
})
24+
.compile();
25+
});
26+
DefaultSubCommandSuite.after.each(({ logMock, exitMock }) => {
27+
logMock.reset();
28+
exitMock.reset();
29+
});
30+
DefaultSubCommandSuite.after(({ exitMock }) => {
31+
exitMock.restore();
32+
});
33+
DefaultSubCommandSuite('top should call top mid1 bottom', async ({ commandInstance, logMock }) => {
34+
await CommandTestFactory.run(commandInstance, ['top']);
35+
equal(logMock.firstCall?.args[0], `top mid-1 bottom command`);
36+
});
37+
DefaultSubCommandSuite(
38+
'top mid-2 should call top mid2 command',
39+
async ({ commandInstance, logMock }) => {
40+
await CommandTestFactory.run(commandInstance, ['top', 'mid-2']);
41+
equal(logMock.firstCall?.args[0], `top mid-2 command`);
42+
},
43+
);

packages/nest-commander/src/command-runner.service.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ ${cliPluginError(this.options.cliName ?? 'nest-commander', this.options.pluginsA
4545
if (this.options.errorHandler) {
4646
this.commander.exitOverride(this.options.errorHandler);
4747
}
48-
if (this.options.enablePositionalOptions) {
49-
this.commander.enablePositionalOptions();
50-
}
5148
}
5249

5350
private async populateCommandMapInstances(

0 commit comments

Comments
 (0)