Repository: directus/directus
File: api/src/cli/index.test.ts (TypeScript)

Code before:
	});

	describe('CLI initialization', () => {
		test('Should create a Command instance', async () => {
			expect(program).toBeInstanceOf(Command);
		});

		test('Should load extensions during initialization', async () => {
			const { loadExtensions } = await import('./load-extensions.js');
			expect(loadExtensions).toHaveBeenCalledTimes(1);
		});

		test('Should emit cli.before and cli.after events', async () => {
			const emitter = (await import('../emitter.js')).default;
			expect(emitter.emitInit).toHaveBeenCalledWith('cli.before', { program: expect.any(Command) });
			expect(emitter.emitInit).toHaveBeenCalledWith('cli.after', { program: expect.any(Command) });
		});

		test('Should allow excess arguments', () => {
			expect((program as any)._allowExcessArguments).toBe(true);
		});
	});

	describe('Command actions', () => {
		test('Should call startServer when start command is invoked', async () => {
			const { startServer } = await import('../server.js');
			vi.mocked(startServer).mockClear();

			await program.parseAsync(['node', 'directus', 'start']);

			expect(startServer).toHaveBeenCalledTimes(1);
		});

		test('Should call init when init command is invoked', async () => {
			const init = (await import('./commands/init/index.js')).default;
			vi.mocked(init).mockClear();

			await program.parseAsync(['node', 'directus', 'init']);

			expect(init).toHaveBeenCalledTimes(1);
		});

		test('Should call bootstrap when bootstrap command is invoked', async () => {
			const bootstrap = (await import('./commands/bootstrap/index.js')).default;
			vi.mocked(bootstrap).mockClear();

			await program.parseAsync(['node', 'directus', 'bootstrap']);

			expect(bootstrap).toHaveBeenCalledTimes(1);
		});


Review comment (suggestion):
Are the `await import(..)` statements necessary for the tests to work? Ideally a regular import at the top would be used

Code after:
}));

describe('createCli', () => {
	let program: Command;

	beforeEach(async () => {
		vi.clearAllMocks();
		program = await createCli();
	});

	describe('CLI initialization', () => {
		test('Should create a Command instance', async () => {
			expect(program).toBeInstanceOf(Command);
		});

		test('Should load extensions during initialization', async () => {
			expect(loadExtensions).toHaveBeenCalledTimes(1);
		});

		test('Should emit cli.before and cli.after events', async () => {
			expect(emitter.emitInit).toHaveBeenCalledWith('cli.before', { program: expect.any(Command) });
			expect(emitter.emitInit).toHaveBeenCalledWith('cli.after', { program: expect.any(Command) });
		});

		test('Should allow excess arguments', async () => {
			await program.parseAsync(['node', 'directus', 'start', 'test']);

			expect(program.args).toEqual(['start', 'test']);
		});
	});

	describe('Command actions', () => {
		test('Should call startServer when start command is invoked', async () => {
			await program.parseAsync(['node', 'directus', 'start']);

			expect(startServer).toHaveBeenCalledTimes(1);
		});

		test('Should call init when init command is invoked', async () => {
			await program.parseAsync(['node', 'directus', 'init']);

			expect(init).toHaveBeenCalledTimes(1);
		});

		test('Should call bootstrap when bootstrap command is invoked', async () => {
			await program.parseAsync(['node', 'directus', 'bootstrap']);

			expect(bootstrap).toHaveBeenCalledTimes(1);
		});

		test.each([