Vitekube Automates Namespace and Cleanup Management in Kubernetes Testing
Vitekube automates namespace and cleanup management during testing on real Kubernetes clusters, creating isolated namespaces for each test and deleting them after completion to maintain cluster hygiene.
Namespace and Cleanup Management
Vitekube automates namespace and cleanup management during testing on real Kubernetes clusters. When a test runs, vitekube creates a new namespace specifically for that test, ensuring isolation and preventing interference between tests. After the test completes, the namespace is automatically deleted, maintaining cluster hygiene. Cleanup functions are executed in reverse order of setup, allowing for precise resource teardown. These functions can be customized with time limits and failure handling, providing greater control over test execution. Additionally, vitekube offers detailed failure output and options to define what data is considered sensitive, enhancing test visibility and security. This approach ensures that each test runs in a clean, isolated environment, mirroring real-world deployment scenarios without leaving residual resources on the cluster.
Integration with Vitest and Kubernetes
Vitekube enables seamless integration between Vitest and Kubernetes by allowing developers to extend test fixtures with Kubernetes-specific functionality. Using .extend, custom fixtures can be created, such as web, which provides access to Kubernetes resources like port forwarding and logs. Any test can request these fixtures by name, enabling consistent and reusable testing logic across different environments.
Configuration is streamlined through k8s.setup.ts, where developers can define cluster details, such as the provider and name, ensuring tests run on the correct cluster. Integration with Vitest is achieved by adding K8sReporter to the reporters array in vitest.config.ts, which enhances output readability and includes features like improved timeout handling and filtering behavior.
Custom Fixtures and Test Execution
Vitekube allows developers to create custom fixtures using the .extend method, enabling tailored test environments for Kubernetes clusters. For example, a fixture can be defined in fixtures.ts to set up a specific test scenario, such as establishing a port forward to a pod and providing utility functions for fetching data or retrieving logs. These fixtures can be requested by name in any test, ensuring consistency and reusability across test cases.
Fixtures offer several features, including a cluster client that is canceled on test timeout, a temporary namespace that is deleted after the test, and cleanup functions that execute in reverse order with individual time limits. Developers can also control what is considered sensitive data and restrict output when necessary, as shown in evidence.restrict(). Custom fixtures can be added with .extend, and file-scoped fixtures are available via fileK8s for shared resources.
Cluster Configuration and Commands
Vitekube allows developers to configure and manage Kubernetes clusters directly within their test setup. In k8s.setup.ts, users can define cluster-specific settings, such as the provider (k3d in this case), the cluster name (my-app-tests), and whether to reuse an existing cluster (reuse: true). This configuration ensures consistency across test runs and reduces overhead by avoiding unnecessary cluster creation.
Vitekube also provides a set of commands to control test execution. For example, npx vitest run web runs tests matching the web pattern, while VITEKUBE_REBUILD=1 npx vitest run forces a rebuild of all images and stages. Additional options, like VITEKUBE_CLUSTER=other, allow users to specify different clusters for different worktrees, and VITEKUBE_KEEP_ON_FAILURE=1 keeps the namespace of a failed test for inspection.
According to the tool's documentation, even when no changes occur, Vitekube may take a few seconds to execute due to Docker's layer cache being the only reliable method to determine if changes have occurred.
Testing and Development Workflow
Vitekube enables developers to run Vitest tests directly on real Kubernetes clusters, bridging the gap between local development and production environments. By setting VITEKUBE_KEEP_ON_FAILURE=1, developers can retain the failed test's namespace for inspection, making debugging more efficient. To integrate Vitekube into a project, the server block in vite.config.ts should include deps: { inline: ["vitekube"] }, ensuring Vitest remains installed. The tool supports a variety of commands, such as vp install, vp pack, and vp test, which streamline the development workflow without requiring Docker. End-to-end tests can be executed using pnpm run e2e:all, which runs contract tests and examples on real k3d clusters. Git hooks are also configured to run checks and tests automatically on commit and push, enhancing code quality and consistency.
Sources and reporting notes
Based on published source material. No independent hands-on testing is claimed.