✨ SwitchIt: The Smart Workflow Extension for Code & Tests
SwitchIt is a Visual Studio Code extension built to keep your implementation and test files perfectly in sync, ensuring high code quality through enforced coverage standards.
🚀 Key Features
| Feature |
What It Does |
Why It Matters |
| Instant File Switch |
Quickly jumps between an implementation file (e.g., file.ts) and its related test file (file.spec.ts) with a single command. |
Eliminates manual file searching and streamlines your unit testing workflow. |
| Commit Coverage Gate |
Installs a Git hook that automatically runs tests on staged changes and blocks the commit if the code coverage threshold is not met. |
Enforces code quality. Prevents accidental drops in test coverage before a PR is even opened. |
| Missing File Prompt |
If the related test or implementation file is missing, SwitchIt offers to create it for you instantly. |
Helps maintain a consistent file structure and encourages immediate testing. |
| Customizable Setup |
Easily adjust file naming conventions, coverage percentage, and test runners to fit any project. |
Works with virtually any testing setup (Jest, Karma, etc.). |
⚙️ Getting Started (File Switching)
1. Installation
Install SwitchIt from the Visual Studio Code Marketplace.
2. Usage
- Open any implementation or test file.
- Open the Command Palette (
Ctrl+Shift+P or Cmd+Shift+P).
- Search for and run the command:
SwitchIt: Switch File
- The related file will open immediately, or you will be prompted to create it.
3. Basic Configuration (Optional)
SwitchIt uses the fileNamingConvention configuration scope for file switching.
- Open Settings: Go to
File > Preferences > Settings (or Code > Settings on macOS).
- Find Settings: Search for
fileNamingConvention.
- Set Your Naming Conventions: Adjust these values to match your project's structure:
| Setting Path |
Description |
Default Value (used in your code) |
fileNamingConvention.testFile |
The pattern/suffix for your test files. |
.spec.ts |
fileNamingConvention.implementationFile |
The pattern/suffix for your source code files. |
.ts |
🛡️ Advanced Feature: Setup Coverage Gate
This feature uses the switchit configuration scope to manage code quality checks.
Go to VS Code Settings and search for switchit:
| Setting Path |
Description |
Default Value |
switchit.coverageGateThreshold |
The minimum required line/branch coverage percentage that staged files must meet to allow a commit. |
80 |
switchit.testRunner |
Select the test runner used in your project (options: jest, karma or custom). |
karma |
switchit.customRunnerCommand |
If testRunner is set to custom, define the full terminal command to run your tests and generate a coverage report. |
(Empty) |
2. Run Setup Command
- Open the Command Palette (
Ctrl+Shift+P or Cmd+Shift+P).
- Run the command:
SwitchIt: Setup Coverage Gate
This command automatically configures your repository by:
- Installing the required pre-commit hook script (
precommit-check.js).
- Adding the necessary
coverage:gate:run script to your package.json.
- Writing temporary runner configuration files based on your selection.
3. Usage
Once set up, the coverage gate runs automatically on git commit. If staged files have insufficient coverage, your commit will be BLOCKED, and a detailed failure report will be printed to your terminal, guiding you on what needs to be fixed.
🛠️ Requirements
- Visual Studio Code version 1.60.0 or higher.
- A Git repository initialized in your project folder.
- Node.js and npm accessible in your path.
🤝 Contribution & License
Contributions are welcome! Feel free to submit issues or pull requests to improve this extension.
This extension is licensed under the [MIT License].
Happy Coding! Now go forth and let your code and tests stay in sync like never before! 🚀