Vitest and Rollup
Dipend integrates cleanly with modern development tools like Vitest for testing and Rollup for bundling. This guide walks you through how to configure each.
Using Vitest for Testing
Section titled “Using Vitest for Testing”Vitest is a fast unit test framework built on top of Vite, with first-class TypeScript support.
Install Vitest and Related Tools
Section titled “Install Vitest and Related Tools”npm install --save-dev vitest @vitest/ui @rollup/plugin-typescript ts-patch
Initialize Dipend
Section titled “Initialize Dipend”If not already done:
npx dipend init
Example Vitest Configuration
Section titled “Example Vitest Configuration”Create or edit a file called vitest.config.ts
:
import { defineConfig } from "vitest/config";import tsPlugin from "@rollup/plugin-typescript";const tspCompiler = require("ts-patch/compiler");
export default defineConfig({ test: { coverage: { enabled: true, exclude: ["**/node_modules/**", "**/vitest.config.ts", "**/dist/**"], }, }, server: { host: "127.0.0.1", }, plugins: [tsPlugin({ typescript: tspCompiler })],});
Writing a Simple Test
Section titled “Writing a Simple Test”Create a test like this:
import { describe, it, expect, vi } from "vitest";import { DependencyContainer } from "dipend";import { Greeter } from "./greeter";import { ILogger } from "./logger";
describe("Greeter", () => { const dependencyContainer = new DependencyContainer();
afterEach(() => { vi.restoreAllMocks(); dependencyContainer.reset(); // clean the container });
it("greets with a name", () => { const logger: ILogger = { info: vi.fn() };
dependencyContainer.addSingletonInstance<ILogger>({ instance: logger }); dependencyContainer.addSingleton<Greeter>();
const greeter = dependencyContainer.getDependency<Greeter>();
expect(greeter.greet("Alice")).toBe("Hello, Alice!"); expect(logger.info).toHaveBeenCalledWith("Hello, Alice!"); });});
Run Tests
Section titled “Run Tests”npx vitest run
Or with UI:
npx vitest --ui
Using Rollup for Bundling
Section titled “Using Rollup for Bundling”Rollup is a modern bundler ideal for building TypeScript libraries and applications.
Install Rollup
Section titled “Install Rollup”npm install --save-dev rollup @rollup/plugin-typescript ts-patch
Initialize Dipend
Section titled “Initialize Dipend”If not already done:
npx dipend init
Basic Rollup Configuration
Section titled “Basic Rollup Configuration”Create a rollup.config.js
file:
import typescript from "@rollup/plugin-typescript";const tspCompiler = require("ts-patch/compiler");
export default { input: "./index.ts", output: { dir: "dist", format: "esm", sourcemap: true, }, plugins: [typescript({ typescript: tspCompiler })],};
Build with Rollup
Section titled “Build with Rollup”npx rollup -c
This will bundle your application or library into the dist
folder.
You’re now set up for a full development workflow using Dipend, Vitest, and Rollup!