|
| 1 | +import { defineCommand } from 'citty' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +import { runCommandDef } from '../../src/run-command' |
| 5 | + |
| 6 | +let clear: boolean | undefined |
| 7 | + |
| 8 | +const devCommand = defineCommand({ |
| 9 | + meta: { name: 'dev' }, |
| 10 | + args: { |
| 11 | + clear: { |
| 12 | + type: 'boolean', |
| 13 | + description: 'Clear console on restart', |
| 14 | + default: false, |
| 15 | + }, |
| 16 | + }, |
| 17 | + run(ctx) { |
| 18 | + clear = ctx.args.clear |
| 19 | + }, |
| 20 | +}) |
| 21 | + |
| 22 | +vi.mock('../../src/commands', () => ({ |
| 23 | + commands: { |
| 24 | + dev: () => Promise.resolve(devCommand), |
| 25 | + }, |
| 26 | +})) |
| 27 | + |
| 28 | +describe('runCommand', () => { |
| 29 | + it('should not clear the console unless asked to', async () => { |
| 30 | + const { runCommand } = await import('../../src/run') |
| 31 | + |
| 32 | + await runCommand('dev', []) |
| 33 | + expect(clear).toBe(false) |
| 34 | + |
| 35 | + await runCommandDef(devCommand, []) |
| 36 | + expect(clear).toBe(false) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should respect an explicit `--clear`', async () => { |
| 40 | + const { runCommand } = await import('../../src/run') |
| 41 | + |
| 42 | + await runCommand('dev', ['--clear']) |
| 43 | + expect(clear).toBe(true) |
| 44 | + |
| 45 | + await runCommandDef(devCommand, ['--clear']) |
| 46 | + expect(clear).toBe(true) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should not modify the arguments it is given', async () => { |
| 50 | + const { runCommand } = await import('../../src/run') |
| 51 | + const argv = ['--clear'] |
| 52 | + |
| 53 | + await runCommand('dev', argv) |
| 54 | + await runCommandDef(devCommand, argv) |
| 55 | + |
| 56 | + expect(argv).toEqual(['--clear']) |
| 57 | + }) |
| 58 | +}) |
0 commit comments