@rajzik/danger-configuration
v2.4.3
Published
Reusable Danger.js scripts
Readme
@rajzik/danger-configuration
A comprehensive DangerJS configuration package providing utilities for automated code review checks in pull requests.
Installation
npm install --save-dev @rajzik/danger-configuration danger
pnpm install --save-dev @rajzik/danger-configuration danger
yarn add --dev @rajzik/danger-configuration dangerUsage
Create a dangerfile.js in your repository root:
import {
checkForADR,
checkForAnyTests,
checkForConventionalPrefix,
checkForConventionalSquashCommit,
checkForInvalidLocks,
checkSourceFilesHaveTests,
disableComponentSnapshots,
disableNewJavaScript,
} from '@rajzik/danger-configuration';
// Check for conventional commit prefix in PR title
checkForConventionalPrefix();
// Check for squash commit format
checkForConventionalSquashCommit();
// Validate lock file changes
checkForInvalidLocks();
// Check for ADR documentation on large PRs
checkForADR('docs/adr', {
changeThreshold: 200,
docsUrl: 'https://example.com/docs/adr',
fail: true,
});
// Check for test files
checkForAnyTests({ root: 'src', fail: true });
// Ensure all source files have tests
checkSourceFilesHaveTests({
root: 'src',
ignorePattern: /Icon[A-Z][A-Za-z]+\.tsx$/,
fail: true,
});
// Disable component snapshots
disableComponentSnapshots({
docsUrl: 'https://example.com/docs/testing',
});
// Disable new JavaScript files
disableNewJavaScript();API Reference
checkForConventionalPrefix()
Verifies that the PR title contains a conventional changelog prefix according to the conventional-changelog format.
Example:
checkForConventionalPrefix();Behavior:
- Fails the PR if the title doesn't match the conventional commit format
- Valid formats include:
fix:,new(scope):,update(Button):, etc.
checkForConventionalSquashCommit()
Ensures that when a PR has only 1 commit and will be squash-merged, the commit message matches the PR title. This prevents losing the semver prefix during automatic releases.
Example:
checkForConventionalSquashCommit();Behavior:
- Fails if PR has exactly 1 commit and the commit message doesn't include the PR title
- Prevents semver prefix loss during squash merges
checkForInvalidLocks()
Validates that lock file changes are accompanied by corresponding package.json
changes.
Example:
checkForInvalidLocks();Behavior:
- Fails if
package-lock.jsonis changed withoutpackage.json - Fails if
npm-shrinkwrap.jsonis changed withoutpackage.json - Fails if
pnpm-lock.yamlis changed withoutpackage.jsonorpnpm-workspace.yaml
Supported lock files:
package-lock.json(npm)npm-shrinkwrap.json(npm)pnpm-lock.yaml(pnpm)
checkForADR(docsPath, options?)
Checks that large PRs have an associated Architecture Decision Record (ADR) file documenting the changes.
Parameters:
docsPath(string): Path to the ADR documentation directory (e.g.,'docs/adr')options(object, optional):changeThreshold(number, default:200): Number of additions/deletions that trigger the checkdocsUrl(string, optional): URL to ADR documentationexclusions(string[], optional): Additional file patterns to exclude from change countfail(boolean, default:false): Whether to fail the PR or just warn
Example:
checkForADR('docs/adr', {
changeThreshold: 200,
docsUrl: 'https://example.com/docs/adr',
exclusions: ['*.md'],
fail: true,
});Behavior:
- Automatically excludes lock files, test files, and snapshots from change count
- Shows a thank you message if ADR files are found
- Warns or fails if PR exceeds threshold without ADR documentation
- Skips check for revert PRs
checkForAnyTests(options?)
Checks that test files exist when source files are updated.
Parameters:
options(object, optional):root(string, optional): Root directory to filter source files (e.g.,'src')fail(boolean, default:false): Whether to fail the PR or just warn
Example:
checkForAnyTests({ root: 'src', fail: true });Behavior:
- Checks if any test files (
.test.ts,.test.tsx,.test.js,.test.jsx) are present - Warns or fails if source files changed but no test files found
- Skips check for revert PRs
checkSourceFilesHaveTests(options?)
Ensures that all touched source files have an accompanying test file change.
Parameters:
options(object, optional):root(string, optional): Root directory to filter source filesignorePattern(RegExp, optional): Pattern to ignore specific source filesfail(boolean, default:false): Whether to fail the PR or just warn
Example:
checkSourceFilesHaveTests({
root: 'src',
ignorePattern: /Icon[A-Z][A-Za-z]+\.tsx$/,
fail: true,
});Behavior:
- Maps source files to expected test file names:
src/components/Button.tsx→tests/components/Button.test.tsxsrc/components/index.tsx→tests/components/index.test.tsxortests/components/Button.test.tsx
- Lists all source files missing test files
- Skips check for revert PRs
Test file mapping rules:
src/→tests/ortest/Foo/index.tsx→Foo.test.tsxorFoo/index.test.tsxFoo.tsx→Foo.test.tsx
disableComponentSnapshots(options?)
Prevents creation or updates of component snapshot files (.jsx.snap,
.tsx.snap).
Parameters:
options(object, optional):docsUrl(string, optional): URL to migration documentation
Example:
disableComponentSnapshots({
docsUrl: 'https://example.com/docs/testing',
});Behavior:
- Fails if new snapshot files are created
- Warns if existing snapshot files are updated
- Skips check for revert PRs
disableNewJavaScript()
Prevents creation of new JavaScript files, enforcing TypeScript usage.
Example:
disableNewJavaScript();Behavior:
- Fails if any new
.jsor.jsxfiles are created insrc/ortests?/directories - Only checks newly created files, not modified files
Types
CommonOptions
interface CommonOptions {
fail?: boolean; // Whether to fail the PR (true) or warn (false)
}CheckAdrOptions
interface CheckAdrOptions extends CommonOptions {
changeThreshold?: number; // Default: 200
docsUrl?: string;
exclusions?: string[];
}TestOptions
interface TestOptions extends CommonOptions {
ignorePattern?: RegExp;
root?: string;
}SnapshotOptions
interface SnapshotOptions {
docsUrl?: string;
}Helper Functions
The following helper functions are exported but primarily used internally:
debug(msg, ...args): Debug logging (only whendangerfile.jsis modified)isRevert(): Checks if PR is a revertcountChangesInFile(file): Counts additions/deletions in a filetouchedFiles: Array of all created, deleted, and modified filesupdatedFiles: Array of all created and modified files
Constants
IS_SRC: Regex matchingsrc/directoryIS_TEST: Regex matchingtests?/directoryJS_EXT: Regex matching.jsor.jsxfilesSRC_EXT: Regex matching.ts,.tsx,.js,.jsxfilesTEST_EXT: Regex matching.test.ts,.test.tsx,.test.js,.test.jsxfilesSNAP_EXT: Regex matching.snapfilesGLOBAL_IGNORE: Regex for globally ignored files (e.g., icon components)
