How to Use GitHub Copilot With Jest and Vitest Test Generation
🔍 WiseChecker

How to Use GitHub Copilot With Jest and Vitest Test Generation

You want GitHub Copilot to generate unit tests for your JavaScript or TypeScript code using Jest or Vitest. Writing tests manually takes time and often leads to incomplete coverage. Copilot can suggest test cases, mocks, and assertions based on your existing code and test framework. This article shows you how to set up Copilot for test generation, write effective prompts, and avoid common pitfalls with Jest and Vitest.

Key Takeaways: Generating Tests with Copilot

  • Inline suggestion via comment: Write a comment like // test the sum function and Copilot generates the test block.
  • Context-aware test generation: Open a test file and Copilot reads the source file to produce matching test cases.
  • Framework-specific output: Copilot adapts syntax for Jest describe/it blocks and Vitest describe/it blocks automatically.

ADVERTISEMENT

How Copilot Generates Tests for Jest and Vitest

GitHub Copilot uses the context of your open files to infer the test framework. When you open a file named test.js, spec.ts, or a file inside a __tests__ folder, Copilot assumes you are writing tests. It reads the corresponding source file if it is open in the editor or in the same directory. Copilot then generates test code that matches the test framework conventions found in your project—Jest or Vitest. No special configuration is required beyond having Copilot installed and enabled in your editor, typically Visual Studio Code or JetBrains IDEs.

What Copilot Looks For to Detect the Framework

Copilot scans your project for jest.config.js, vitest.config.ts, or package.json entries for Jest or Vitest. If it finds a configuration file, it uses the structure and options defined there. If no config is found, Copilot defaults to Jest syntax because Jest is more widely used. You can override this by adding a comment at the top of your test file, such as // @vitest-environment jsdom for Vitest. Copilot also respects existing test patterns in your codebase—if you already have tests using vi.fn() (Vitest) or jest.fn() (Jest), Copilot continues with that style.

Steps to Generate Tests with Copilot in Visual Studio Code

Follow these steps to make Copilot generate test cases for your functions. The steps assume you have the GitHub Copilot extension installed and active in Visual Studio Code. The same approach works in JetBrains IDEs with the Copilot plugin.

  1. Open the source file and the test file side by side
    In Visual Studio Code, open your source file—for example math.js—and create a new test file named math.test.js or math.spec.ts. Place the files in the same directory or in a __tests__ folder. Copilot uses the source file to understand function signatures and imports.
  2. Write an import statement for the function under test
    In the test file, type the import line manually or let Copilot auto-complete it. For example: import { sum } from './math';. This gives Copilot the exact reference to the function.
  3. Start a describe block with a meaningful name
    Type describe('sum', () => { and press Enter. Copilot often suggests the it block automatically. If not, type it('should return the sum of two numbers', () => { and press Enter. Copilot fills the test body.
  4. Accept or modify the suggested test
    Press Tab to accept the suggestion. Review the generated test—Copilot may include edge cases like zero, negative numbers, or type errors. Add additional it blocks by typing it('should handle and let Copilot complete the edge case.
  5. Generate mocks for external dependencies
    If your function calls an API or a database, Copilot can generate mock functions. Type jest.mock('./api'); for Jest or vi.mock('./api'); for Vitest. Then write it('should call the API once', () => { and Copilot produces the mock setup and assertion.
  6. Run the tests and iterate
    After generating the test file, run npm test or npx vitest to execute the tests. If a test fails, open the test file and place your cursor on the failing line. Copilot may suggest a fix based on the error message in the terminal.

ADVERTISEMENT

Common Issues When Copilot Generates Tests

Copilot Generates Jest Syntax for a Vitest Project

Copilot sometimes outputs jest.fn() or jest.mock() even when your project uses Vitest. This happens when Copilot cannot find a Vitest configuration file or when the test file lacks a Vitest-specific comment. To fix this, add // @vitest-environment node or // @vitest-environment jsdom at the top of your test file. Copilot then switches to Vitest syntax for all subsequent suggestions. Alternatively, rename your test file to use the .spec.ts extension and ensure vitest.config.ts exists in the project root.

Copilot Does Not Suggest Tests at All

If Copilot remains silent when you type describe or it, the editor may not recognize the test file context. Verify that your file extension is .test.js, .test.ts, .spec.js, or .spec.ts. Also confirm that Copilot is enabled in the status bar—click the Copilot icon and ensure it shows a check mark. If the issue persists, close and reopen the file to refresh the context.

Copilot Generates Incomplete or Incorrect Assertions

Copilot may produce test bodies that lack expect statements or use wrong matchers. This often occurs when the source function has complex logic or multiple return paths. To improve suggestions, write a detailed comment before the it block. For example: // test that sum returns 5 when inputs are 2 and 3. Copilot uses the comment as a prompt and generates a more accurate assertion.

Copilot Repeats the Same Test Case

Sometimes Copilot suggests the same test body for multiple it blocks. This happens when the function signature is simple and Copilot has limited context. To get varied tests, add comments that describe different scenarios. For example: // test with negative numbers and // test with string input. Each comment leads to a distinct test suggestion.

Copilot with Jest vs Copilot with Vitest: Key Differences

Item Jest Vitest
Default syntax jest.fn(), jest.mock(), jest.spyOn() vi.fn(), vi.mock(), vi.spyOn()
Configuration detection jest.config.js or jest in package.json vitest.config.ts or vitest in package.json
Environment comments Not required—Jest uses testEnvironment in config Optional but recommended: // @vitest-environment jsdom
Mock reset behavior jest.clearAllMocks() in beforeEach vi.clearAllMocks() in beforeEach
Test runner speed Slower for large projects due to Jest’s architecture Faster because Vitest uses Vite’s transform pipeline

You can now generate unit tests with GitHub Copilot using both Jest and Vitest. Start by opening your source file and test file together. Write clear comments for edge cases to get better suggestions from Copilot. For advanced usage, try writing a comment that describes a failing test scenario—Copilot may suggest the fix before you write the code.

ADVERTISEMENT