|
| 1 | +import { describe, expect, it } from 'bun:test' |
| 2 | +import { z } from 'zod/v4' |
| 3 | +import { parseArgs } from './parse' |
| 4 | + |
| 5 | +describe('parseArgs', () => { |
| 6 | + it('should parse long-form flags with values', () => { |
| 7 | + const schema = z.object({ |
| 8 | + name: z.string() |
| 9 | + }) |
| 10 | + const argv = ['--name', 'test'] |
| 11 | + const result = parseArgs({ argv, schema }) |
| 12 | + expect(result).toEqual({ name: 'test' }) |
| 13 | + }) |
| 14 | + |
| 15 | + it('should parse long-form boolean flags', () => { |
| 16 | + const schema = z.object({ |
| 17 | + bool: z.boolean().optional() |
| 18 | + }) |
| 19 | + const argv = ['--bool'] |
| 20 | + const result = parseArgs({ argv, schema }) |
| 21 | + expect(result).toEqual({ bool: true }) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should parse long-form number flags', () => { |
| 25 | + const schema = z.object({ |
| 26 | + number: z.number() |
| 27 | + }) |
| 28 | + const argv = ['--number', '123'] |
| 29 | + const result = parseArgs({ argv, schema }) |
| 30 | + expect(result).toEqual({ number: 123 }) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should convert kebab-case flags to camelCase', () => { |
| 34 | + const schema = z.object({ |
| 35 | + kebabCase: z.boolean().optional() |
| 36 | + }) |
| 37 | + const argv = ['--kebab-case'] |
| 38 | + const result = parseArgs({ argv, schema }) |
| 39 | + expect(result).toEqual({ kebabCase: true }) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should parse short-form boolean flags', () => { |
| 43 | + const schema = z.object({ |
| 44 | + bool: z.boolean().optional() |
| 45 | + }) |
| 46 | + const argv = ['-b'] |
| 47 | + const result = parseArgs({ argv, schema }) |
| 48 | + expect(result).toEqual({ bool: true }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should parse short-form flags with values', () => { |
| 52 | + const schema = z.object({ |
| 53 | + name: z.string() |
| 54 | + }) |
| 55 | + const argv = ['-n', 'test'] |
| 56 | + const result = parseArgs({ argv, schema }) |
| 57 | + expect(result).toEqual({ name: 'test' }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should throw an error for a missing value for a long-form flag', () => { |
| 61 | + const schema = z.object({ |
| 62 | + name: z.string() |
| 63 | + }) |
| 64 | + const argv = ['--name'] |
| 65 | + expect(() => { |
| 66 | + try { |
| 67 | + parseArgs({ argv, schema }) |
| 68 | + } catch (error) { |
| 69 | + throw error.message |
| 70 | + } |
| 71 | + }).toThrow('Missing value for flag --name') |
| 72 | + }) |
| 73 | + |
| 74 | + it.skip('should handle combined short-form boolean flags', () => { |
| 75 | + const schema = z.object({ |
| 76 | + bool1: z.boolean().optional(), |
| 77 | + bool2: z.boolean().optional() |
| 78 | + }) |
| 79 | + const argv = ['-vf'] |
| 80 | + const result = parseArgs({ argv, schema }) |
| 81 | + expect(result).toEqual({ bool1: true, bool2: true }) |
| 82 | + }) |
| 83 | + |
| 84 | + it.skip('should handle combined short-form flags with a value', () => { |
| 85 | + const schema = z.object({ |
| 86 | + name: z.string(), |
| 87 | + bool: z.boolean().optional() |
| 88 | + }) |
| 89 | + const argv = ['-nf', 'test'] |
| 90 | + const result = parseArgs({ argv, schema }) |
| 91 | + expect(result).toEqual({ name: 'test', bool: true }) |
| 92 | + }) |
| 93 | +}) |
0 commit comments