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

Code before:
		});

		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);
		});

		test('Should call dbMigrate with correct arguments', async () => {
			const dbMigrate = (await import('./commands/database/migrate.js')).default;

			vi.mocked(dbMigrate).mockClear();
			await program.parseAsync(['node', 'directus', 'database', 'migrate:latest']);
			expect(dbMigrate).toHaveBeenCalledWith('latest');

			vi.mocked(dbMigrate).mockClear();
			await program.parseAsync(['node', 'directus', 'database', 'migrate:up']);
			expect(dbMigrate).toHaveBeenCalledWith('up');

Review comment (suggestion):
We can utilize the `afterEach` hook and auto clear all mocks after every test instead of doing so manually in each

Code after:
	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([
			['latest', 'migrate:latest'],
			['up', 'migrate:up'],
			['down', 'migrate:down'],
		])('Should call dbMigrate with "%s" argument', async (expectedArg, command) => {
			await program.parseAsync(['node', 'directus', 'database', command]);

			expect(dbMigrate).toHaveBeenCalledWith(expectedArg);
		});
	});