Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions .changeset/notificationkit_71368.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,36 @@

## Summary

First official release: Added Dependabot automation and SonarQube MCP integration instructions
Comprehensive testing implementation with 133+ tests, improved code quality, and complete documentation.

## Changes

### Testing

- Added comprehensive test suite with 133+ tests across 10 test suites
- Created shared test utilities in `test/test-utils.ts` to reduce code duplication
- Implemented integration tests for end-to-end notification workflows
- Added controller tests for REST API endpoints
- Added module tests for NestJS dependency injection
- Included mock implementations: `MockRepository`, `MockSender`, `MockTemplateEngine`, etc.
- Created helper functions for easier test setup: `createNotificationServiceWithDeps()`, `createFailingNotificationServiceWithDeps()`

### Code Quality

- Reduced code duplication from 4.3% to 2.66% (passing SonarQube quality gate ≤ 3%)
- Improved code organization with centralized test utilities
- Fixed ESLint and TypeScript strict mode issues in test files

### Documentation

- Created comprehensive README.md with full project documentation
- Updated CONTRIBUTING.md with detailed testing guidelines
- Added CHANGELOG.md to track version history
- Enhanced infrastructure documentation with testing examples
- Added support and contribution links

### Automation

- Updated package configuration and workflows
- Enhanced code quality and automation tooling
- Improved CI/CD integration and monitoring capabilities
- Enhanced CI/CD integration with Dependabot
- Integrated SonarQube quality gate checks
192 changes: 192 additions & 0 deletions .github/instructions/testing.instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,195 @@ it("test", async () => {
- [ ] Mocks cleaned up in afterEach
- [ ] Async operations properly awaited
- [ ] Error cases tested

---

## 🧰 Shared Test Utilities

This package provides shared test utilities in `test/test-utils.ts` to reduce code duplication and make testing easier.

### Mock Implementations

```typescript
import {
MockRepository,
MockSender,
MockTemplateEngine,
MockEventEmitter,
MockFailingSender,
} from "../test/test-utils";

// In-memory notification repository
const repository = new MockRepository();
await repository.create(notification);

// Mock notification sender (always succeeds)
const sender = new MockSender(NotificationChannel.EMAIL);
await sender.send(recipient, content);

// Mock sender that simulates failures
const failingSender = new MockFailingSender();
failingSender.setShouldFail(true);

// Mock template engine
const templateEngine = new MockTemplateEngine();
await templateEngine.render("welcome", { name: "John" });

// Mock event emitter
const eventEmitter = new MockEventEmitter();
eventEmitter.on("notification.sent", handler);
```

### Factory Functions

```typescript
import {
createNotificationServiceWithDeps,
createFailingNotificationServiceWithDeps,
createModuleTestOptions,
} from "../test/test-utils";

// Create service with all mocked dependencies
const { service, repository, sender, idGenerator, dateTimeProvider } =
createNotificationServiceWithDeps();

// Create service with failing sender for error testing
const { service: failingService, repository: failingRepo } =
createFailingNotificationServiceWithDeps();

// Create module configuration for NestJS testing
const options = createModuleTestOptions({
senders: [new MockSender()],
repository: new MockRepository(),
});
```

### Default Test Data

```typescript
import { defaultNotificationDto, createMockNotification } from "../test/test-utils";

// Standard notification DTO for tests
const notification = await service.send(defaultNotificationDto);

// Create mock notification with custom overrides
const mockNotification = createMockNotification({
status: NotificationStatus.SENT,
priority: NotificationPriority.HIGH,
});
```

### Usage Example

```typescript
import { createNotificationServiceWithDeps, defaultNotificationDto } from "../test/test-utils";

describe("MyFeature", () => {
let service: NotificationService;
let repository: MockRepository;

beforeEach(() => {
const ctx = createNotificationServiceWithDeps();
service = ctx.service;
repository = ctx.repository;
});

it("should create notification", async () => {
const notification = await service.create(defaultNotificationDto);

expect(notification.id).toBeDefined();
expect(notification.status).toBe(NotificationStatus.QUEUED);
});

it("should send notification", async () => {
const result = await service.send(defaultNotificationDto);

expect(result.success).toBe(true);

// Repository is shared, can verify persistence
const notifications = await repository.find({});
expect(notifications).toHaveLength(1);
});
});
```

### Benefits

- ✅ **Reduced duplication**: Centralized mock implementations
- ✅ **Consistent behavior**: All tests use the same mocks
- ✅ **Easy setup**: Factory functions handle complex initialization
- ✅ **Type safety**: Full TypeScript support
- ✅ **Maintainable**: Changes to mocks update all tests automatically

---

## 📈 Current Test Coverage

The package maintains comprehensive test coverage:

- **Total Tests**: 133+
- **Test Suites**: 10
- **Code Duplication**: 2.66% (well below 3% threshold)
- **Coverage Target**: 80%+ (achieved)

### Test Distribution

- ✅ Core domain tests (notification.service.test.ts)
- ✅ DTO validation tests (dtos.test.ts)
- ✅ Error handling tests (errors.test.ts)
- ✅ Provider tests (providers.test.ts)
- ✅ Controller tests (notification.controller.test.ts, webhook.controller.test.ts)
- ✅ Module tests (module.test.ts)
- ✅ Decorator tests (decorators.test.ts)
- ✅ Integration tests (integration.test.ts)
- ✅ Smoke tests (smoke.test.ts)

---

## 🚀 Running Tests

```bash
# Run all tests
npm test

# Run with coverage
npm run test:cov

# Watch mode for development
npm run test:watch

# Run specific test file
npm test -- notification.service.test.ts

# Run tests matching pattern
npm test -- --testNamePattern="should send notification"
```

---

## 📝 Writing New Tests

When adding new tests:

1. **Use shared utilities** from `test/test-utils.ts` to avoid duplication
2. **Follow naming conventions**: `[feature].test.ts` or `[feature].spec.ts`
3. **Test behavior**, not implementation details
4. **Include error cases** and edge conditions
5. **Keep tests independent** - no shared state between tests
6. **Use descriptive names**: `it('should [expected behavior] when [condition]')`
7. **Clean up mocks** in `afterEach()` hooks

---

## 🔍 Quality Checks

Before committing:

```bash
npm run lint # Check code style
npm run typecheck # Check TypeScript types
npm test # Run all tests
npm run test:cov # Verify coverage
```

All checks must pass before merging to main branch.
49 changes: 49 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Comprehensive test suite with 133+ tests across 10 test suites
- Shared test utilities in `test/test-utils.ts` for easier testing
- Integration tests for end-to-end notification workflows
- Controller tests for REST API endpoints
- Module tests for NestJS dependency injection
- Mock implementations for testing: `MockRepository`, `MockSender`, `MockTemplateEngine`
- Test helper functions: `createNotificationServiceWithDeps()`, `createFailingNotificationServiceWithDeps()`
- Default test data: `defaultNotificationDto`

### Changed

- Reduced code duplication from 4.3% to 2.66% (passing SonarQube quality gate)
- Improved test organization with centralized test utilities
- Enhanced documentation with comprehensive README and testing guidelines

### Fixed

- ESLint configuration for test files
- TypeScript strict mode compatibility across all test files

## [0.0.0] - Initial Release

### Added

- Core notification service with support for Email, SMS, and Push notifications
- Multi-provider support (Twilio, AWS SNS, Firebase, Nodemailer, etc.)
- NestJS module integration with dependency injection
- Pluggable repository pattern for flexible data storage
- Event system for notification lifecycle tracking
- Template engine support (Handlebars and simple templates)
- Retry logic and notification state management
- REST API controllers (optional)
- Webhook handling (optional)
- Clean architecture with framework-agnostic core
- Full TypeScript support with type definitions

[Unreleased]: https://github.com/CISCODE-MA/NotificationKit/compare/v0.0.0...HEAD
[0.0.0]: https://github.com/CISCODE-MA/NotificationKit/releases/tag/v0.0.0
52 changes: 46 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing to <PACKAGE_NAME>
# Contributing to @ciscode/notification-kit

Thank you for your interest in contributing to **<PACKAGE_NAME>** 💙
Thank you for your interest in contributing to **@ciscode/notification-kit** 💙
Contributions of all kinds are welcome: bug fixes, improvements, documentation, and discussions.

---
Expand Down Expand Up @@ -67,10 +67,49 @@ npm test
npm run build
```

If you add or modify logic:
### Testing Guidelines

• Add unit tests for behaviour changes.
• Avoid live external API calls in tests.
This project maintains high test coverage (133+ tests). When contributing:

**For bug fixes:**

- Add a test that reproduces the bug
- Verify the fix resolves the issue
- Ensure existing tests still pass

**For new features:**

- Add unit tests for core business logic
- Add integration tests for end-to-end workflows
- Test error cases and edge cases
- Use shared test utilities from `test/test-utils.ts`

**Testing best practices:**

- Keep tests independent and isolated
- Use descriptive test names: `it('should [expected behavior]')`
- Avoid live external API calls - use mocks
- Test both success and failure scenarios
- Aim for at least 80% code coverage

**Available test utilities:**

```typescript
import {
createNotificationServiceWithDeps,
MockRepository,
MockSender,
defaultNotificationDto,
} from "./test/test-utils";
```

**Running specific test suites:**

```bash
npm test -- notification.service.test.ts # Run specific file
npm run test:watch # Watch mode
npm run test:cov # With coverage
```

---

Expand All @@ -81,7 +120,8 @@ When opening a PR:
• Clearly describe what was changed and why
• Keep PRs focused on a single concern
• Reference related issues if applicable
• Update docummentation if APIs or behaviour change
• Update documentation if APIs or behaviour change
• Ensure all tests pass and coverage is maintained

A maintainer may ask for changes or clarification before merging.

Expand Down
Loading