{"meta":{"title":"Creating mock objects to abstract layers","intro":"Copilot Chat can help with creating mock objects that you can use for unit tests.","product":"GitHub Copilot","breadcrumbs":[{"href":"/en/copilot","title":"GitHub Copilot"},{"href":"/en/copilot/tutorials","title":"Tutorials"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat Cookbook"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/testing-code","title":"Testing code"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/testing-code/create-mock-objects","title":"Create mock objects"}],"documentType":"article"},"body":"# Creating mock objects to abstract layers\n\nCopilot Chat can help with creating mock objects that you can use for unit tests.\n\nWhen creating unit tests, it's important to ensure they're isolated and not depending on external services. One way to achieve this is to create mock objects that abstract layers of your application. Copilot Chat can help you generate the code you need to create these mock objects.\n\n## Example scenario\n\nImagine a website built with TypeScript which displays a list of runners. You have a service which fetches the runners from a database, and the server-side code which uses this service. You want to test the server-side code, but you don't want to make a call to the database. You can ask Copilot Chat to generate a mock object for the service.\n\n```typescript id=mock-service\n// SvelteKit example at +page.server.ts\nimport service from './service';\n\nexport async function load({ params }) {\n  const runner = await service.getRunner(params.id);\n\n  return {\n    runner\n  };\n}\n```\n\n## Example prompt\n\nThis example assumes you are using [vitest](https://vitest.dev/) for your tests, but will work with other test runners.\n\n```copilot copy prompt ref=mock-service\n/tests Create a unit test to ensure the service is called correctly. Mock the service object.\n```\n\nIf you have an existing test you wish Copilot to use as a model, you can use `#file: path/to/test-file.js` to provide the path to the file, and include this information in the prompt.\n\n## Example response\n\n> \\[!NOTE] The following response is an example. Copilot Chat responses are non-deterministic, so you may get a different response from the one shown here.\n\nCopilot provides the plan it will implement and the code text which includes the mocks.\n\n```typescript\nimport { describe, it, expect, vi, beforeEach } from 'vitest';\nimport { load } from './+page.server.ts';\n\n// Mock the entire service module\nvi.mock('./service', () => ({\n  default: {\n    getRunner: vi.fn()\n  }\n}));\n\n// Import the mocked service\nimport service from './service';\n\ndescribe('load function', () => {\n  const mockRunner = {\n    id: '123',\n    name: 'Test Runner',\n    status: 'active'\n  };\n\n  beforeEach(() => {\n    // Reset mock before each test\n    vi.resetAllMocks();\n\n    // Setup default mock implementation\n    (service.getRunner as any).mockResolvedValue(mockRunner);\n  });\n\n  it('should fetch runner with correct id', async () => {\n    const params = { id: '123' };\n    const result = await load({ params });\n\n    expect(service.getRunner).toHaveBeenCalledWith('123');\n    expect(service.getRunner).toHaveBeenCalledTimes(1);\n    expect(result).toEqual({ runner: mockRunner });\n  });\n});\n```\n\n## Further reading\n\n* [Prompt engineering for GitHub Copilot Chat](/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [Best practices for using GitHub Copilot](/en/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}