-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These integration tests cover basic commands with multiple options calling `this` for the command runner and the param handler and having multiple commands inside the application. All passing with great coverage.
- Loading branch information
Showing
23 changed files
with
340 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"private": "true", | ||
"dependencies": { | ||
"nest-commander": "workspace:*", | ||
"nest-commander-testing": "workspace:*", | ||
"commander": "^6.2.1", | ||
"@nestjs/common": "^7.6.5", | ||
"@nestjs/core": "^7.6.5", | ||
"@nestjs/testing": "^7.6.5", | ||
"jest": "^26.6.3", | ||
"typescript": "^4.1.3", | ||
"ts-jest": "^26.4.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Command, CommandRunner } from 'nest-commander'; | ||
import { LogService } from './../../common/log.service'; | ||
|
||
@Command({ name: 'bar', options: { isDefault: false } }) | ||
export class BarCommand implements CommandRunner { | ||
constructor(private readonly logService: LogService) {} | ||
|
||
async run(): Promise<void> { | ||
this.logService.log('bar'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Command, CommandRunner } from 'nest-commander'; | ||
import { LogService } from './../../common/log.service'; | ||
|
||
@Command({ name: 'foo', options: { isDefault: true } }) | ||
export class FooCommand implements CommandRunner { | ||
constructor(private readonly logService: LogService) {} | ||
|
||
async run(): Promise<void> { | ||
this.logService.log('foo'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { BarCommand } from './bar.command'; | ||
import { FooCommand } from './foo.command'; | ||
import { LogService } from './../../common/log.service'; | ||
|
||
@Module({ | ||
providers: [BarCommand, FooCommand, LogService], | ||
}) | ||
export class MultipleCommandModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { TestingModule } from '@nestjs/testing'; | ||
import { CommandTestFactory } from 'nest-commander-testing'; | ||
import { LogService } from '../../common/log.service'; | ||
import { MultipleCommandModule } from '../src/root.module'; | ||
|
||
describe('Multiple Commands', () => { | ||
const logSpy = jest.fn(); | ||
let commandInstance: TestingModule; | ||
|
||
beforeAll(async () => { | ||
commandInstance = await CommandTestFactory.createTestingCommand({ | ||
imports: [MultipleCommandModule], | ||
}) | ||
.overrideProvider(LogService) | ||
.useValue({ log: logSpy }) | ||
.compile(); | ||
}); | ||
|
||
afterEach(() => { | ||
logSpy.mockClear(); | ||
}); | ||
|
||
it.each` | ||
command | expected | ||
${'foo'} | ${'foo'} | ||
${'bar'} | ${'bar'} | ||
${undefined} | ${'foo'} | ||
`( | ||
'call $command expect $expected', | ||
async ({ | ||
command, | ||
expected, | ||
}: { | ||
command: 'bar' | 'foo' | undefined; | ||
expected: 'bar' | 'foo'; | ||
}) => { | ||
await CommandTestFactory.run(commandInstance, command ? [command] : undefined); | ||
expect(logSpy).toBeCalledWith(expected); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"private": "true", | ||
"dependencies": { | ||
"nest-commander": "workspace:*", | ||
"nest-commander-testing": "workspace:*", | ||
"commander": "^6.2.1", | ||
"@nestjs/common": "^7.6.5", | ||
"@nestjs/core": "^7.6.5", | ||
"@nestjs/testing": "^7.6.5", | ||
"jest": "^26.6.3", | ||
"typescript": "^4.1.3", | ||
"ts-jest": "^26.4.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Command, CommandRunner } from 'nest-commander'; | ||
import { LogService } from '../../common/log.service'; | ||
|
||
@Command({ name: 'this-command', arguments: '<with-value>' }) | ||
export class ThisCommandCommand implements CommandRunner { | ||
constructor(private readonly log: LogService) {} | ||
|
||
async run(params: string[]) { | ||
this.logHandler(params); | ||
} | ||
|
||
logHandler(...args: any[]): void { | ||
this.log.log(...args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common/decorators'; | ||
import { LogService } from '../../common/log.service'; | ||
import { ThisCommandCommand } from './this-command.command'; | ||
|
||
@Module({ | ||
providers: [LogService, ThisCommandCommand], | ||
}) | ||
export class ThisCommandModule {} |
25 changes: 25 additions & 0 deletions
25
integration/this-command/test/this-command.command.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { TestingModule } from '@nestjs/testing'; | ||
import { CommandTestFactory } from 'nest-commander-testing'; | ||
import { LogService } from '../../common/log.service'; | ||
import { ThisCommandModule } from '../src/this-command.module'; | ||
|
||
describe('This Handler', () => { | ||
const logMock = jest.fn(); | ||
let commandModule: TestingModule; | ||
|
||
beforeAll(async () => { | ||
commandModule = await CommandTestFactory.createTestingCommand({ imports: [ThisCommandModule] }) | ||
.overrideProvider(LogService) | ||
.useValue({ log: logMock }) | ||
.compile(); | ||
}); | ||
|
||
afterEach(() => { | ||
logMock.mockClear(); | ||
}); | ||
|
||
it('should call this-handler with arg hello', async () => { | ||
await CommandTestFactory.run(commandModule, ['this-command', 'hello']); | ||
expect(logMock).toBeCalledWith(['hello']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"private": "true", | ||
"dependencies": { | ||
"nest-commander": "workspace:*", | ||
"nest-commander-testing": "workspace:*", | ||
"commander": "^6.2.1", | ||
"@nestjs/common": "^7.6.5", | ||
"@nestjs/core": "^7.6.5", | ||
"@nestjs/testing": "^7.6.5", | ||
"jest": "^26.6.3", | ||
"typescript": "^4.1.3", | ||
"ts-jest": "^26.4.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Command, CommandRunner, Option } from 'nest-commander'; | ||
import { LogService } from '../../common/log.service'; | ||
|
||
@Command({ | ||
name: 'this-handler', | ||
description: 'Just adding a description for coverage', | ||
options: { isDefault: true }, | ||
}) | ||
export class ThisHandlerCommand implements CommandRunner { | ||
constructor(private readonly log: LogService) {} | ||
|
||
async run(params: string[], options: Record<string, string>) { | ||
this.logHandler(options); | ||
} | ||
|
||
logHandler(...args: any[]): void { | ||
this.log.log(...args); | ||
} | ||
|
||
@Option({ | ||
flags: '-b,--basic [basic]', | ||
description: 'A value to pass for the this-handler method', | ||
defaultValue: 'oh HAI', | ||
}) | ||
parseBasicOption(val: string): string { | ||
return this.handleParsingBasic(val); | ||
} | ||
|
||
handleParsingBasic(val: string): string { | ||
return val; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common/decorators'; | ||
import { LogService } from '../../common/log.service'; | ||
import { ThisHandlerCommand } from './this-handler.command'; | ||
|
||
@Module({ | ||
providers: [LogService, ThisHandlerCommand], | ||
}) | ||
export class ThisHandlerModule {} |
32 changes: 32 additions & 0 deletions
32
integration/this-handler/test/this-handler.command.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { TestingModule } from '@nestjs/testing'; | ||
import { CommandTestFactory } from 'nest-commander-testing'; | ||
import { LogService } from '../../common/log.service'; | ||
import { ThisHandlerModule } from '../src/this-handler.module'; | ||
|
||
describe('This Handler', () => { | ||
const logMock = jest.fn(); | ||
let commandModule: TestingModule; | ||
|
||
beforeAll(async () => { | ||
commandModule = await CommandTestFactory.createTestingCommand({ imports: [ThisHandlerModule] }) | ||
.overrideProvider(LogService) | ||
.useValue({ log: logMock }) | ||
.compile(); | ||
}); | ||
|
||
afterEach(() => { | ||
logMock.mockClear(); | ||
}); | ||
|
||
it.each` | ||
flagVal | expected | ||
${['-b']} | ${'oh HAI'} | ||
${['-b', 'hello']} | ${'hello'} | ||
`( | ||
'should call this-handler with arg $flagVal and log $expected', | ||
async ({ flagVal, expected }: { flagVal: string[]; expected: string }) => { | ||
await CommandTestFactory.run(commandModule, ['this-handler'].concat(flagVal)); | ||
expect(logMock).toBeCalledWith({ basic: expected }); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
module.exports = { | ||
moduleFileExtensions: ['js', 'json', 'ts'], | ||
rootDir: '.', | ||
testRegex: '.spec.ts$', | ||
testMatch: ['*.spec.ts'], | ||
transform: { | ||
'^.+\\.ts$': 'ts-jest', | ||
}, | ||
testEnvironment: 'node', | ||
collectCoverageFrom: ['src/*'], | ||
collectCoverage: true | ||
}; | ||
collectCoverageFrom: ['packages/**/src/*.ts'], | ||
collectCoverage: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
const baseConfig = require('./jest.config'); | ||
module.exports = { | ||
...baseConfig, | ||
testRegex: 'integration/.*.spec.ts$', | ||
collectCoverage: false, | ||
testMatch: ['<rootDir>/integration/**/*.spec.ts'], | ||
collectCoverage: true, | ||
verbose: true, | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: 'tsconfig.test.json', | ||
}, | ||
}, | ||
moduleNameMapper: { | ||
'^nest-commander$': ['<rootDir>/packages/nest-commander/src'], | ||
'^nest-commander-testing$': ['<rootDir>/packages/nest-commander-testing/src'], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.