@codepunter-labs/mock-brevo
v0.2.0
Published
Mock SDK for Brevo API
Maintainers
Readme
@codepunter-labs/mock-brevo
Mock SDK for Brevo API (formerly Sendinblue). Provides fully-typed mock implementations for testing transactional email sending, templates, contacts, and webhooks without making real API calls.
Installation
npm install @codepunter-labs/mock-brevoFeatures
- Email Sending: Send transactional emails with HTML, text, or templates
- Templates: Create, update, list, and delete email templates
- Contacts: Manage contacts with attributes and lists
- Webhooks: Create and manage webhooks for email events
- Type-safe: Full TypeScript support with accurate API types
- Test Framework Agnostic: Works with Jest, Vitest, and other spy-based frameworks
Repository
- GitHub: https://github.com/DERRICK-TORKORNOO/mock-sdks/tree/main/packages/mock-brevo
- Source Code: src/
- Tests: tests/
Usage
Basic Setup
import { vi } from 'vitest';
import { createMockBrevo } from '@codepunter-labs/mock-brevo';
const brevo = createMockBrevo({ spy: vi.fn });Using with Jest
import { createMockBrevo } from '@codepunter-labs/mock-brevo';
const brevo = createMockBrevo({ spy: jest.fn });Example: Send Email
// Default behavior - returns realistic mock data
const result = await brevo.sendEmail({
sender: { email: '[email protected]', name: 'Sender' },
to: [{ email: '[email protected]', name: 'Recipient' }],
subject: 'Test Email',
htmlContent: '<p>Hello World</p>'
});Example: Send Email with Template
const result = await brevo.sendEmail({
sender: { email: '[email protected]' },
to: [{ email: '[email protected]' }],
templateId: 1,
params: { name: 'John', order: 12345 }
});Example: Create Template
const result = await brevo.createTemplate({
name: 'Welcome Email',
subject: 'Welcome',
htmlContent: '<h1>Welcome {{name}}</h1>'
});Example: Create Contact
const result = await brevo.createContact({
email: '[email protected]',
attributes: { FIRSTNAME: 'John', LASTNAME: 'Doe' },
listIds: [1]
});Example: Override Mock Response
import { makeSendEmailResponse } from '@codepunter-labs/mock-brevo';
brevo.sendEmail.mockResolvedValue({
code: 200,
message: 'success',
data: makeSendEmailResponse({ messageId: '<[email protected]>' })
});Example: Error Simulation
import { MockErrors } from '@codepunter-labs/mock-core';
brevo.sendEmail.mockRejectedValue(
MockErrors.badRequest('Invalid email address')
);API Reference
MockBrevo
sendEmail(request)- Send transactional emailcreateTemplate(request)- Create email templateupdateTemplate(request)- Update email templategetTemplate(templateId)- Get template detailslistTemplates()- List all templatesdeleteTemplate(templateId)- Delete templatecreateContact(request)- Create contactupdateContact(request)- Update contactgetContact(email)- Get contact detailslistContacts()- List all contactsdeleteContact(email)- Delete contactcreateWebhook(request)- Create webhookupdateWebhook(request)- Update webhookgetWebhook(id)- Get webhook detailslistWebhooks()- List all webhooksdeleteWebhook(id)- Delete webhook
Fixture Factories
makeEmailAddress(email, name?)- Create email addressmakeSendEmailResponse(options?)- Create send email responsemakeTemplate(options?)- Create templatemakeContact(options?)- Create contactmakeWebhook(options?)- Create webhook
Testing Patterns
Test Success Path
test('should send email successfully', async () => {
const brevo = createMockBrevo({ spy: vi.fn });
const result = await brevo.sendEmail(request);
expect(result.code).toBe(200);
expect(result.data.messageId).toBeDefined();
});Test Error Handling
test('should handle invalid email address error', async () => {
const brevo = createMockBrevo({ spy: vi.fn });
brevo.sendEmail.mockRejectedValue(
BrevoErrors.invalidEmail()
);
await expect(brevo.sendEmail(request)).rejects.toMatchObject({ message: /Invalid email/ });
});Test with Custom Data
test('should use custom message ID', async () => {
const brevo = createMockBrevo({ spy: vi.fn });
brevo.sendEmail.mockResolvedValue({
code: 200,
message: 'success',
data: makeSendEmailResponse({ messageId: '<[email protected]>' })
});
const result = await brevo.sendEmail(request);
expect(result.data.messageId).toBe('<[email protected]>');
});License
MIT
