Skip to content

Testing examples using @kjanat/dreamcli/testkit.

Testing examples using @kjanat/dreamcli/testkit.

  • Source: examples/standalone/testing.ts

  • Demonstrates: runCommand(), prompt answers, env/config injection, output assertions, middleware context, and activity assertions.

Usage

bash
bun test examples/testing.ts

Source

ts
/**
 * Testing examples using @kjanat/dreamcli/testkit.
 *
 * Demonstrates: `runCommand()`, prompt answers, env/config injection, output
 * assertions, middleware context, and activity assertions.
 *
 * This file is structured as a Vitest test suite — run with
 * `bun test examples/testing.ts`.
 *
 * Usage:
 *   bun test examples/testing.ts
 */

import { , , ,  } from '@kjanat/dreamcli';
import { , ,  } from '@kjanat/dreamcli/testkit';
import { , ,  } from 'vitest';

// --- Command under test ---

const  = ('deploy')
	.('Deploy to an environment')
	.('target', .().('Deploy target'))
	.(
		'region',
		
			.(['us', 'eu', 'ap'])
			.('DEPLOY_REGION')
			.('deploy.region')
			.({ : 'select', : 'Which region?' })
			.('Target region'),
	)
	.('force', .().('f'))
	.(({ , ,  }) => {
		if (!.) {
			.('Use --force to skip confirmation');
		}
		.(`Deploying ${.} to ${.}`);
	});

// --- Tests ---

('deploy command', () => {
	('deploys with explicit flags', async () => {
		const  = await (, ['production', '--region', 'eu', '--force']);

		(.).(0);
		(.).(['Deploying production to eu\n']);
		(.).([]);
	});

	('resolves region from environment variable', async () => {
		const  = await (, ['staging', '--force'], {
			: { : 'ap' },
		});

		(.).(0);
		(.).(['Deploying staging to ap\n']);
	});

	('resolves region from config file', async () => {
		const  = await (, ['staging', '--force'], {
			: { : { : 'eu' } },
		});

		(.).(0);
		(.).(['Deploying staging to eu\n']);
	});

	('prompts for region when not provided', async () => {
		const  = await (, ['staging', '--force'], {
			: ['us'],
		});

		(.).(0);
		(.).(['Deploying staging to us\n']);
	});

	('handles prompt cancellation', async () => {
		const  = await (, ['staging'], {
			: ([]),
		});

		(.)..(0);
	});

	('shows warning when --force is not set', async () => {
		const  = await (, ['production', '--region', 'us']);

		(.).(0);
		(.).(['Use --force to skip confirmation\n']);
		(.).(['Deploying production to us\n']);
	});

	('renders help text', async () => {
		const  = await (, ['--help']);

		(.).(0);
		(..('')).('Deploy to an environment');
		(..('')).('--region');
	});
});

// --- Middleware testing ---

const  = <{ : string }>(async ({  }) => {
	return ({ : 'alice' });
});

const  = ('secret')
	.('Protected command')
	.()
	.(({ ,  }) => {
		.(`Hello, ${.}`);
	});

('middleware context', () => {
	('passes context from middleware to action', async () => {
		const  = await (, []);

		(.).(0);
		(.).(['Hello, alice\n']);
	});
});

// --- Spinner/progress activity events ---

const  = ('build')
	.('Build something')
	.(({  }) => {
		const  = .('Building...');
		.('Done');
	});

('activity events', () => {
	('captures spinner lifecycle in activity array', async () => {
		const  = await (, []);

		(.).(0);
		(.).(
			.({ : 'spinner:start', : 'Building...' }),
		);
		(.).(
			.({ : 'spinner:succeed', : 'Done' }),
		);
	});
});

Released under the MIT License.