ESLint plugin to enforce Domain-Driven Design principles
npm install eslint-plugin-dddESLint plugin to enforce Domain-Driven Design (DDD) principles in your JavaScript and TypeScript projects.
``bash`
pnpm add -D eslint-plugin-ddd
Add ddd to the plugins section of your .eslintrc configuration file:
`json`
{
"plugins": ["ddd"]
}
Then configure the rules you want to use:
`json`
{
"rules": {
"ddd/require-spec-file": "error"
}
}
Alternatively, you can use the recommended configuration:
`json`
{
"extends": ["plugin:ddd/recommended"]
}
Or the strict configuration:
`json`
{
"extends": ["plugin:ddd/strict"]
}
Enforces that every .js or .ts file containing logic has a corresponding .spec.js or .spec.ts file in the same directory.
Rule Details
This rule ensures test coverage by requiring spec files alongside implementation files that contain logic, promoting test-driven development and maintaining test organization.
What counts as "logic":
- Functions with implementations (not just type signatures)
- Classes with method implementations
- Executable code
Files automatically excluded (no logic):
- Type-only files (interfaces, types, enums)
- Constant-only files
- Simple re-export files
The rule automatically matches the file extension:
- .js files require .spec.js files.ts
- files require .spec.ts files
Examples of incorrect code:
`javascript`
// src/user-service.js exists
// but src/user-service.spec.js does NOT exist
// ❌ ESLint will report an error
`typescript`
// src/product-repository.ts exists
// but src/product-repository.spec.ts does NOT exist
// ❌ ESLint will report an error
Examples of correct code:
`javascript`
// src/user-service.js exists with logic
// AND src/user-service.spec.js exists
// ✅ No error
`typescript`
// src/product-repository.ts exists with logic
// AND src/product-repository.spec.ts exists
// ✅ No error
`javascript`
// src/constants.js exists with ONLY constants (no logic)
// ✅ No spec file required
export const API_URL = 'https://api.example.com';
export const MAX_RETRIES = 3;
`typescript`
// src/types.ts exists with ONLY types (no logic)
// ✅ No spec file required
export interface User {
id: string;
name: string;
}
Options
This rule accepts an options object with the following properties:
- excludePatterns (array of strings): Glob patterns to exclude from the spec file requirement
Default exclude patterns:
- */.spec.js - JavaScript spec files*/.spec.ts
- - TypeScript spec files*/.test.js
- - JavaScript test files*/.test.ts
- - TypeScript test files**/index.js
- - JavaScript index files**/index.ts
- - TypeScript index files*/.d.ts
- - TypeScript declaration files
Example configuration:
`json`
{
"rules": {
"ddd/require-spec-file": ["error", {
"excludePatterns": [
"*/.spec.js",
"*/.spec.ts",
"*/.test.js",
"*/.test.ts",
"/config/",
"*/.config.js",
"*/.config.ts"
]
}]
}
}
`bashInstall dependencies
pnpm install
Releasing
This project uses release-it for automated releases.
$3
1. Copy the
.env.example file to .env:
`bash
cp .env.example .env
`2. Add your tokens to the
.env file:
- GITHUB_TOKEN: GitHub Personal Access Token (generate here)
- NPM_TOKEN: NPM Access Token (generate here)$3
`bash
pnpm release
``This will:
- Run linting and tests
- Bump the version
- Create a git commit and tag
- Push to GitHub
- Create a GitHub release
- Publish to npm
MIT