main-process-unit-tests

Write Vitest unit tests for Electron main process services and IPC handlers.

Updated May 20, 2025
One-click install
npx skills add https://github.com/trae-op/electron-password-generation --skill main-process-unit-tests
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: main-process-unit-tests
Source: https://github.com/trae-op/electron-password-generation/tree/main/.github/skills/main-process-unit-tests
Command: npx skills add https://github.com/trae-op/electron-password-generation --skill main-process-unit-tests

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Unit testing for Electron main process logic (services, IPC handlers, and DI-based code) can be tedious and brittle. This skill provides guidelines, patterns, and practical examples using Vitest to create reliable tests and diagnose failures.

Core Features & Use Cases

  • Co-located unit tests for main-process modules
  • Mock Electron APIs, network calls, and file system interactions
  • Validate IPC handlers, provider factories, and DI-injected services in isolation
  • Establish consistent testing conventions and quick-start examples

Quick Start

Install Vitest and set up a basic test scaffold for a service like src/main/feature/service.ts and its test at src/main/feature/service.test.ts. Then run npm run test:unit:main or your project’s test script to execute changes and confirm success.

Frequently Asked Questions about main-process-unit-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I unit test Electron main process code with Vitest?

Unit testing Electron main process code with Vitest involves co-locating tests with your modules and mocking Electron APIs to validate IPC handlers and DI-based services in isolation. This approach establishes reliable testing conventions for your application.

What is the best way to mock Electron APIs for main process unit tests?

The best way to mock Electron APIs for main process unit tests is to use Vitest mocking patterns to isolate network calls and file system interactions. This allows you to validate DI-injected services and IPC handlers without triggering actual Electron operations.

Can I test Electron IPC handlers in isolation using Vitest?

Yes, you can test Electron IPC handlers in isolation using Vitest. By mocking Electron APIs and applying dependency injection, you can validate provider factories and IPC handlers independently of the renderer process or full Electron runtime environment.

How do I set up Vitest for testing DI-based components in an Electron app?

To set up Vitest for testing DI-based components in an Electron app, create a basic test scaffold co-located with your service files, such as placing service.test.ts next to service.ts. This structure helps validate injected dependencies and main-process modules quickly.

Why are my Electron main process unit tests failing?

Electron main process unit tests often fail due to unmocked Electron APIs or actual network and file system interactions. By enforcing Vitest-based mocking patterns and isolating DI-injected services, you can diagnose failures and build more reliable test suites.