๋จ์ ํ ์คํธ๋ฅผ ๋ง๋ค ๋ ์ธ๋ถ ์๋น์ค์ ๋ฐ๋ผ ๊ฒฉ๋ฆฌ๋์ง ์๋๋ก ํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. ์ด๋ฅผ ๋ฌ์ฑํ๋ ํ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ณ์ธต์ ์ถ์ํํ๋ ๋ชจ์ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๊ฒ์ ๋๋ค. Copilot Chat์ ์ด๋ฌํ ๋ชจ์ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐ ํ์ํ ์ฝ๋๋ฅผ ์์ฑํ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
์์ ์๋๋ฆฌ์ค
์คํ๊ธฐ ๋ชฉ๋ก์ ํ์ํ๋ TypeScript๋ก ๋น๋๋ ์น ์ฌ์ดํธ๋ฅผ ์์ํด ๋ณด์ธ์. ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์คํ๊ธฐ๋ฅผ ๊ฐ์ ธ์ค๋ ์๋น์ค์ ์ด ์๋น์ค๋ฅผ ์ฌ์ฉํ๋ ์๋ฒ ์ชฝ ์ฝ๋๊ฐ ์์ต๋๋ค. ์๋ฒ ์ชฝ ์ฝ๋๋ฅผ ํ ์คํธํ๋ ค๊ณ ํ์ง๋ง ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ํธ์ถํ๊ณ ์ถ์ง๋ ์์ต๋๋ค. Copilot Chat์๊ฒ ์๋น์ค์ ๋ํ ๋ชจ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋๋ก ์์ฒญํ ์ ์์ต๋๋ค.
// SvelteKit example at +page.server.ts
import service from './service';
export async function load({ params }) {
const runner = await service.getRunner(params.id);
return {
runner
};
}
์์ ํ๋กฌํํธ
์ด ์์ ์์๋ ํ ์คํธ์ vitest๋ฅผ ์ฌ์ฉํ์ง๋ง ๋ค๋ฅธ ํ ์คํธ ์คํ๊ธฐ์์ ์๋ํฉ๋๋ค.
/tests Create a unit test to ensure the service is called correctly. Mock the service object.
Copilot๋ฅผ ๋ชจ๋ธ๋ก ์ฌ์ฉํ๋ ค๋ ๊ธฐ์กด ํ
์คํธ๊ฐ ์๋ ๊ฒฝ์ฐ #file: path/to/test-file.js
๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ ๊ณตํ๊ณ ํ๋กฌํํธ์ ์ด ์ ๋ณด๋ฅผ ํฌํจํ ์ ์์ต๋๋ค.
์์ ์๋ต
์ฐธ๊ณ ํญ๋ชฉ
๋ค์ ์๋ต์ด ์์ ์ ๋๋ค. Copilot Chat ์๋ต์ ๋น๊ฒฐ์ ์ ์ด๋ฏ๋ก ์ฌ๊ธฐ์ ํ์๋ ์๋ต๊ณผ ๋ค๋ฅธ ์๋ต์ ์ป์ ์ ์์ต๋๋ค.
Copilot๋ ๊ตฌํํ ๊ณํ๊ณผ ๋ชจ์ ํญ๋ชฉ์ด ํฌํจ๋ ์ฝ๋ ํ ์คํธ๋ฅผ ์ ๊ณตํฉ๋๋ค.
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { load } from './+page.server.ts';
// Mock the entire service module
vi.mock('./service', () => ({
default: {
getRunner: vi.fn()
}
}));
// Import the mocked service
import service from './service';
describe('load function', () => {
const mockRunner = {
id: '123',
name: 'Test Runner',
status: 'active'
};
beforeEach(() => {
// Reset mock before each test
vi.resetAllMocks();
// Setup default mock implementation
(service.getRunner as any).mockResolvedValue(mockRunner);
});
it('should fetch runner with correct id', async () => {
const params = { id: '123' };
const result = await load({ params });
expect(service.getRunner).toHaveBeenCalledWith('123');
expect(service.getRunner).toHaveBeenCalledTimes(1);
expect(result).toEqual({ runner: mockRunner });
});
});