// ✅ Good: Using in-memory database
describe('User Registration', () => {
beforeEach(async () => {
// Setup in-memory database
Object.assign(process.env, {
DB_ADAPTER: 'mock',
DB_DATABASE: `test-${v4()}`
})
})
it('should create a new user with hashed password', async () => {
const userData = {
email: 'test@example.com',
password: 'password123',
name: 'Test User'
}
const user = await UserService.register(userData)
// Test the actual database state
expect(user.email).toBe('test@example.com')
expect(user.password).not.toBe('password123') // Should be hashed
// Verify user was actually saved
const savedUser = await User.findBy('email', 'test@example.com')
expect(savedUser).toBeTruthy()
})
})
// ❌ Avoid: Mocking the repository layer
describe('User Registration (with mocks)', () => {
it('should create a new user', async () => {
const mockUser = { id: '1', email: 'test@example.com' }
jest.spyOn(User, 'create').mockResolvedValue(mockUser)
const user = await UserService.register(userData)
// This only tests that the mock was called correctly,
// not that your actual business logic works
expect(User.create).toHaveBeenCalledWith(userData)
})
})