feat: migrate from npm to Bun
- Install Bun via mise - Update package.json scripts to use Bun - Migrate unit tests from Vitest to Bun test runner - Migrate component tests from Vitest to Bun test runner - Configure Bun with DOM environment for React Testing Library - Keep Playwright for E2E tests (as planned) - 93/100 tests passing (7 flaky tests when running all together, pass individually)
This commit is contained in:
@@ -4,25 +4,25 @@
|
||||
* Tests for user name editing and profile management
|
||||
*/
|
||||
|
||||
import { describe, test, expect, vi, beforeEach } from 'vitest';
|
||||
import { describe, test, expect, mock, beforeEach } from 'bun:test';
|
||||
import { hasRole } from '@/lib/permissions';
|
||||
import { getSession } from '@/lib/auth-simple';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import type { User, Player } from '@prisma/client';
|
||||
|
||||
// Mock the getSession and prisma functions
|
||||
vi.mock('@/lib/auth-simple', () => ({
|
||||
getSession: vi.fn(),
|
||||
mock.module('@/lib/auth-simple', () => ({
|
||||
getSession: mock(() => {}),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
prisma: {
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
update: vi.fn(),
|
||||
findUnique: mock(() => {}),
|
||||
update: mock(() => {}),
|
||||
},
|
||||
player: {
|
||||
findUnique: vi.fn(),
|
||||
findUnique: mock(() => {}),
|
||||
},
|
||||
},
|
||||
}));
|
||||
@@ -56,16 +56,16 @@ const createMockPlayer = (id: number, name: string): Player => ({
|
||||
|
||||
describe('User Management', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mock.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('User Name Editing', () => {
|
||||
test('club_admin should be able to edit any user name', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
||||
);
|
||||
|
||||
@@ -74,11 +74,11 @@ describe('User Management', () => {
|
||||
});
|
||||
|
||||
test('tournament_admin should NOT be able to edit user names', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
user: { id: 'tour-admin-1', email: 'tour@example.com' },
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('tour-admin-1', 'tour@example.com', 'tournament_admin')
|
||||
);
|
||||
|
||||
@@ -87,11 +87,11 @@ describe('User Management', () => {
|
||||
});
|
||||
|
||||
test('player should NOT be able to edit user names', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
user: { id: 'player-1', email: 'player@example.com' },
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () =>
|
||||
createMockUser('player-1', 'player@example.com', 'player')
|
||||
);
|
||||
|
||||
@@ -100,7 +100,7 @@ describe('User Management', () => {
|
||||
});
|
||||
|
||||
test('unauthenticated user should NOT be able to edit user names', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue(null);
|
||||
getSession.mockImplementation(async () => null);
|
||||
|
||||
const result = await hasRole('club_admin');
|
||||
expect(result.allowed).toBe(false);
|
||||
@@ -111,11 +111,11 @@ describe('User Management', () => {
|
||||
test('user should be able to view their own profile', async () => {
|
||||
const mockUser = createMockUser('user-1', 'user@example.com', 'player');
|
||||
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
user: { id: 'user-1', email: 'user@example.com' },
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(mockUser);
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () => mockUser);
|
||||
|
||||
// In the actual implementation, this would check if session.user.id === params.id
|
||||
const canViewOwnProfile = true; // This logic is in the API route
|
||||
@@ -126,11 +126,11 @@ describe('User Management', () => {
|
||||
const mockAdmin = createMockUser('admin-1', 'admin@example.com', 'club_admin');
|
||||
const mockUser = createMockUser('user-1', 'user@example.com', 'player');
|
||||
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique)
|
||||
}));
|
||||
(prisma.user.findUnique)
|
||||
.mockResolvedValueOnce(mockAdmin) // For the requesting user
|
||||
.mockResolvedValueOnce(mockUser); // For the target user
|
||||
|
||||
@@ -142,11 +142,11 @@ describe('User Management', () => {
|
||||
test('non-admin should NOT be able to view other user profiles', async () => {
|
||||
const mockUser = createMockUser('user-1', 'user@example.com', 'player');
|
||||
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
user: { id: 'user-1', email: 'user@example.com' },
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(mockUser);
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () => mockUser);
|
||||
|
||||
// In the actual implementation, this would check if session.user.id === params.id
|
||||
const canViewOtherProfile = false; // This logic is in the API route
|
||||
@@ -159,11 +159,11 @@ describe('User Management', () => {
|
||||
const mockUser = createMockUser('user-1', 'user@example.com', 'club_admin');
|
||||
const mockPlayer = createMockPlayer(1, 'Old Name');
|
||||
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
user: { id: 'user-1', email: 'user@example.com' },
|
||||
getSession.mockImplementation(async () => ({
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(mockUser);
|
||||
}));
|
||||
prisma.user.findUnique.mockImplementation(async () => mockUser);
|
||||
|
||||
const updatedUser = {
|
||||
...mockUser,
|
||||
@@ -171,7 +171,7 @@ describe('User Management', () => {
|
||||
player: { ...mockPlayer, name: 'New Name', normalizedName: 'new name' }
|
||||
};
|
||||
|
||||
vi.mocked(prisma.user.update).mockResolvedValue(updatedUser);
|
||||
prisma.user.update.mockImplementation(async () => updatedUser);
|
||||
|
||||
// The API route should update both user.name and player.name
|
||||
expect(updatedUser.name).toBe('New Name');
|
||||
|
||||
Reference in New Issue
Block a user