import { Command } from 'commander'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { AbstractAction } from '../../actions/abstract.action.js'; import { BuildCommand } from '../../commands/build.command.js'; class FakeAction extends AbstractAction { public handle = vi.fn().mockResolvedValue(undefined); } const ProcessExitError = class extends Error { constructor(public code: number | undefined) { super(`process.exit(${code})`); } }; const buildProgram = (action: FakeAction) => { const program = new Command(); program.exitOverride(); new BuildCommand(action).load(program); return program; }; describe('BuildCommand', () => { let action: FakeAction; let exitSpy: ReturnType; let consoleErrorSpy: ReturnType; beforeEach(() => { action = new FakeAction(); exitSpy = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => { throw new ProcessExitError(code); }) as never); consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); }); afterEach(() => { exitSpy.mockRestore(); consoleErrorSpy.mockRestore(); }); describe('--builder validation', () => { it('exits with code 1 when --builder receives an unknown value', async () => { const program = buildProgram(action); await expect( program.parseAsync(['node', 'nest', 'build', '--builder', 'foo']), ).rejects.toBeInstanceOf(ProcessExitError); expect(exitSpy).toHaveBeenCalledWith(1); expect(consoleErrorSpy).toHaveBeenCalledWith( expect.stringContaining('Invalid builder option: foo'), ); expect(action.handle).not.toHaveBeenCalled(); }); it('exits with code 1 when --builder receives an empty string', async () => { const program = buildProgram(action); await expect( program.parseAsync(['node', 'nest', 'build', '--builder', '']), ).rejects.toBeInstanceOf(ProcessExitError); expect(exitSpy).toHaveBeenCalledWith(1); expect(consoleErrorSpy).toHaveBeenCalledWith( expect.stringContaining('Invalid builder option: '), ); expect(action.handle).not.toHaveBeenCalled(); }); it.each([['tsc'], ['webpack'], ['swc'], ['rspack']])( 'accepts %s as a valid --builder value', async (builder) => { const program = buildProgram(action); await program.parseAsync([ 'node', 'nest', 'build', '--builder', builder, ]); expect(exitSpy).not.toHaveBeenCalled(); expect(action.handle).toHaveBeenCalledTimes(1); expect(action.handle).toHaveBeenCalledWith( expect.objectContaining({ builder }), ); }, ); it('does not validate the builder when the option is omitted', async () => { const program = buildProgram(action); await program.parseAsync(['node', 'nest', 'build']); expect(exitSpy).not.toHaveBeenCalled(); expect(action.handle).toHaveBeenCalledTimes(1); }); it('selects the tsc builder when --tsc is passed', async () => { const program = buildProgram(action); // Without this, a "builder" set in nest-cli.json (e.g. "rspack", the // monorepo default) would still be used. await program.parseAsync(['node', 'nest', 'build', '--tsc']); expect(action.handle).toHaveBeenCalledWith( expect.objectContaining({ builder: 'tsc', webpack: false }), ); }); }); describe('--parallel validation', () => { it('exits with code 1 when --parallel receives a non-positive integer', async () => { const program = buildProgram(action); await expect( program.parseAsync(['node', 'nest', 'build', '--parallel', '0']), ).rejects.toBeInstanceOf(ProcessExitError); expect(exitSpy).toHaveBeenCalledWith(1); expect(consoleErrorSpy).toHaveBeenCalledWith( expect.stringContaining('Invalid --parallel value'), ); expect(action.handle).not.toHaveBeenCalled(); }); it('exits with code 1 when --parallel receives a non-numeric string', async () => { const program = buildProgram(action); await expect( program.parseAsync(['node', 'nest', 'build', '--parallel', 'abc']), ).rejects.toBeInstanceOf(ProcessExitError); expect(exitSpy).toHaveBeenCalledWith(1); expect(action.handle).not.toHaveBeenCalled(); }); it('forwards a positive integer to the action context', async () => { const program = buildProgram(action); await program.parseAsync(['node', 'nest', 'build', '--parallel', '4']); expect(exitSpy).not.toHaveBeenCalled(); expect(action.handle).toHaveBeenCalledWith( expect.objectContaining({ parallel: 4 }), ); }); }); describe('--emit-declarations', () => { it('leaves emitDeclarations undefined when the flag is absent', async () => { // A materialized `false` wins over `compilerOptions.emitDeclarations` in // getValueOrDefault, permanently shadowing the nest-cli.json setting. const program = buildProgram(action); await program.parseAsync(['node', 'nest', 'build']); expect(action.handle).toHaveBeenCalledWith( expect.objectContaining({ emitDeclarations: undefined }), ); }); it('forwards true when the flag is passed', async () => { const program = buildProgram(action); await program.parseAsync([ 'node', 'nest', 'build', '--emit-declarations', ]); expect(action.handle).toHaveBeenCalledWith( expect.objectContaining({ emitDeclarations: true }), ); }); }); describe('--bundle-dependencies', () => { it.each([ [[], undefined], [['--bundle-dependencies'], true], [['--no-bundle-dependencies'], false], ])('forwards %j as %s', async (flags, expected) => { const program = buildProgram(action); await program.parseAsync(['node', 'nest', 'build', ...flags]); expect(action.handle).toHaveBeenCalledWith( expect.objectContaining({ bundleDependencies: expected }), ); }); }); describe('--preserveWatchOutput', () => { it('stays undefined in tsc watch mode when the flag is absent, so the tsconfig value can apply', async () => { const program = buildProgram(action); await program.parseAsync(['node', 'nest', 'build', '--watch', '--tsc']); expect(action.handle.mock.calls[0][0].preserveWatchOutput).toBe( undefined, ); }); it('forwards true when the flag is passed in tsc watch mode', async () => { const program = buildProgram(action); await program.parseAsync([ 'node', 'nest', 'build', '--watch', '--tsc', '--preserveWatchOutput', ]); expect(action.handle).toHaveBeenCalledWith( expect.objectContaining({ preserveWatchOutput: true }), ); }); it('forwards false outside of tsc watch mode', async () => { const program = buildProgram(action); await program.parseAsync([ 'node', 'nest', 'build', '--tsc', '--preserveWatchOutput', ]); expect(action.handle).toHaveBeenCalledWith( expect.objectContaining({ preserveWatchOutput: false }), ); }); }); });