3.0.0 • Published 6 months ago
@jterrazz/test v3.0.0
Package Typescript Test
This package provides Vitest configuration and testing utilities for TypeScript projects, including MSW (Mock Service Worker) setup for API mocking.
Installation
Install the package using npm:
npm install -D @jterrazz/test
Usage
- Set up MSW for API mocking:
// handlers.ts
import { http } from 'msw';
import { setupServer } from '@jterrazz/test';
// Define your API handlers
const handlers = [
http.get('/api/example', () => {
return new Response(JSON.stringify({ data: 'example' }));
}),
];
// Setup MSW server
export const server = setupServer(...handlers);
- You can now write your tests using Vitest and MSW!
import { describe, test, expect } from '@jterrazz/test';
import { server } from './handlers';
describe('API Tests', () => {
test('should handle API requests', async () => {
const response = await fetch('/api/example');
const data = await response.json();
expect(data).toEqual({ data: 'example' });
});
});
- Using Date Mocking:
import { mockOfDate } from '@jterrazz/test';
describe('Date Tests', () => {
test('should mock dates', () => {
const fixedDate = new Date('2024-01-01');
mockOfDate.set(fixedDate);
expect(new Date()).toEqual(fixedDate);
// Reset the mock after your test
mockOfDate.reset();
});
});
- Using Extended Mocking:
import { mockOf } from '@jterrazz/test';
interface UserService {
getUser: (id: string) => Promise<{ id: string; name: string }>;
}
describe('Mock Tests', () => {
test('should use extended mocks', async () => {
const mockUserService = mockOf<UserService>();
// Setup mock behavior
mockUserService.getUser.mockResolvedValue({ id: '1', name: 'John' });
const user = await mockUserService.getUser('1');
expect(user).toEqual({ id: '1', name: 'John' });
});
});
Features
- Vitest configuration with TypeScript support
- MSW integration for API mocking
- Mock date utilities for time-based testing
- Extended mocking capabilities with vitest-mock-extended
Happy testing! 🚀