|
| 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 | +); |
0 commit comments