@codepunter-labs/mock-cloudinary
v0.2.0
Published
TODO
Readme
@codepunter-labs/mock-cloudinary
Mock SDK for Cloudinary Node.js SDK. Provides fully-typed mock implementations for testing Cloudinary image and video upload, transformation, and management without making real API calls.
Installation
npm install @codepunter-labs/mock-cloudinaryFeatures
- Upload: Upload files with options
- Admin: List resources, get resource details, delete, rename
- URL Generation: Generate transformation URLs
- 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-cloudinary
- Source Code: src/
- Tests: tests/
Usage
Basic Setup
import { vi } from 'vitest';
import { createMockCloudinary } from '@codepunter-labs/mock-cloudinary';
const cloudinary = createMockCloudinary({ spy: vi.fn });Using with Jest
import { createMockCloudinary } from '@codepunter-labs/mock-cloudinary';
const cloudinary = createMockCloudinary({ spy: jest.fn });Example: Upload Image
// Default behavior - returns realistic mock data
const result = await cloudinary.upload('image.jpg', {
public_id: 'my_image',
resource_type: 'image'
});Example: List Resources
const result = await cloudinary.listResources({
resource_type: 'image',
max_results: 10
});Example: Generate URL
const url = cloudinary.url('my_image', {
width: 300,
height: 200,
crop: 'fill'
});Example: Override Mock Response
import { makeUploadResponse } from '@codepunter-labs/mock-cloudinary';
cloudinary.upload.mockResolvedValue(
makeUploadResponse({ public_id: 'custom_image', width: 1920 })
);Example: Error Simulation
import { MockErrors } from '@codepunter-labs/mock-core';
cloudinary.upload.mockRejectedValue(
MockErrors.badRequest('Invalid file format')
);API Reference
MockCloudinary
upload(file, options?)- Upload filelistResources(options?)- List resourcesgetResource(publicId)- Get resource detailsdestroy(publicId)- Delete resourcerename(from, to, options?)- Rename resourceurl(publicId, options?)- Generate transformation URL
Fixture Factories
makeUploadResponse(options?)- Create upload responsemakeResource(options?)- Create resourcemakeDeleteResponse(options?)- Create delete responsemakeRenameResponse(options?)- Create rename response
Testing Patterns
Test Success Path
test('should upload image successfully', async () => {
const cloudinary = createMockCloudinary({ spy: vi.fn });
const result = await cloudinary.upload('image.jpg');
expect(result.public_id).toBeDefined();
expect(result.secure_url).toContain('cloudinary.com');
});Test Error Handling
test('should handle invalid file format error', async () => {
const cloudinary = createMockCloudinary({ spy: vi.fn });
cloudinary.upload.mockRejectedValue(
CloudinaryErrors.invalidFileFormat()
);
await expect(cloudinary.upload('invalid.xyz')).rejects.toMatchObject({ message: /Invalid file format/ });
});Test with Custom Data
test('should use custom public ID', async () => {
const cloudinary = createMockCloudinary({ spy: vi.fn });
cloudinary.upload.mockResolvedValue(
makeUploadResponse({ public_id: 'custom_image' })
);
const result = await cloudinary.upload('image.jpg');
expect(result.public_id).toBe('custom_image');
});License
MIT
