refactor: improve test structure for Bun compatibility
- Update all test files to use named mock variables instead of inline mocks - Clear mock history in beforeEach instead of using mock.restore() - Add default mock implementations stored at module level - Document that tests should not use --randomize flag due to mock.module() limitations - All 89 unit tests pass consistently without randomization
This commit is contained in:
@@ -4,15 +4,19 @@
|
||||
* Tests the player deduplication logic for CSV uploads
|
||||
*/
|
||||
|
||||
import { describe, test, expect, mock, beforeEach } from 'bun:test';
|
||||
import { describe, test, expect, mock, beforeEach,} from 'bun:test';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
// Create mock functions at module level
|
||||
const playerFindFirstMock = mock(() => {});
|
||||
const playerCreateMock = mock(() => {});
|
||||
|
||||
// Mock the prisma module
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
prisma: {
|
||||
player: {
|
||||
findFirst: mock(() => {}),
|
||||
create: mock(() => {}),
|
||||
findFirst: playerFindFirstMock,
|
||||
create: playerCreateMock,
|
||||
},
|
||||
},
|
||||
}));
|
||||
@@ -46,7 +50,9 @@ async function findOrCreatePlayer(name: string) {
|
||||
|
||||
describe('Player Deduplication', () => {
|
||||
beforeEach(() => {
|
||||
mock.clearAllMocks();
|
||||
// Clear all mock history before each test
|
||||
playerFindFirstMock.mockClear();
|
||||
playerCreateMock.mockClear();
|
||||
});
|
||||
|
||||
describe('findOrCreatePlayer', () => {
|
||||
@@ -64,7 +70,7 @@ describe('Player Deduplication', () => {
|
||||
rating: 0,
|
||||
};
|
||||
|
||||
prisma.player.findFirst.mockImplementation(async () => mockPlayer);
|
||||
playerFindFirstMock.mockImplementation(async () => mockPlayer);
|
||||
|
||||
const result = await findOrCreatePlayer('Emily');
|
||||
|
||||
@@ -89,7 +95,7 @@ describe('Player Deduplication', () => {
|
||||
rating: 0,
|
||||
};
|
||||
|
||||
prisma.player.findFirst.mockImplementation(async () => mockPlayer);
|
||||
playerFindFirstMock.mockImplementation(async () => mockPlayer);
|
||||
|
||||
const result = await findOrCreatePlayer('EMILY');
|
||||
|
||||
@@ -114,7 +120,7 @@ describe('Player Deduplication', () => {
|
||||
rating: 0,
|
||||
};
|
||||
|
||||
prisma.player.findFirst.mockImplementation(async () => mockPlayer);
|
||||
playerFindFirstMock.mockImplementation(async () => mockPlayer);
|
||||
|
||||
const result = await findOrCreatePlayer(' Emily ');
|
||||
|
||||
@@ -126,7 +132,7 @@ describe('Player Deduplication', () => {
|
||||
});
|
||||
|
||||
test('should create new player if not found', async () => {
|
||||
prisma.player.findFirst.mockImplementation(async () => null);
|
||||
playerFindFirstMock.mockImplementation(async () => null);
|
||||
|
||||
const newPlayer = {
|
||||
id: 100,
|
||||
@@ -141,7 +147,7 @@ describe('Player Deduplication', () => {
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
prisma.player.create.mockImplementation(async () => newPlayer);
|
||||
playerCreateMock.mockImplementation(async () => newPlayer);
|
||||
|
||||
const result = await findOrCreatePlayer('NewPlayer');
|
||||
|
||||
@@ -162,7 +168,7 @@ describe('Player Deduplication', () => {
|
||||
});
|
||||
|
||||
test('should handle names with special characters', async () => {
|
||||
prisma.player.findFirst.mockImplementation(async () => null);
|
||||
playerFindFirstMock.mockImplementation(async () => null);
|
||||
|
||||
const newPlayer = {
|
||||
id: 100,
|
||||
@@ -177,7 +183,7 @@ describe('Player Deduplication', () => {
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
prisma.player.create.mockImplementation(async () => newPlayer);
|
||||
playerCreateMock.mockImplementation(async () => newPlayer);
|
||||
|
||||
const result = await findOrCreatePlayer('Test-Player_123');
|
||||
|
||||
@@ -195,7 +201,7 @@ describe('Player Deduplication', () => {
|
||||
});
|
||||
|
||||
test('should handle names with spaces', async () => {
|
||||
prisma.player.findFirst.mockImplementation(async () => null);
|
||||
playerFindFirstMock.mockImplementation(async () => null);
|
||||
|
||||
const newPlayer = {
|
||||
id: 100,
|
||||
@@ -210,7 +216,7 @@ describe('Player Deduplication', () => {
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
prisma.player.create.mockImplementation(async () => newPlayer);
|
||||
playerCreateMock.mockImplementation(async () => newPlayer);
|
||||
|
||||
const result = await findOrCreatePlayer('Dave B');
|
||||
|
||||
@@ -242,7 +248,7 @@ describe('Player Deduplication', () => {
|
||||
rating: 0,
|
||||
};
|
||||
|
||||
prisma.player.findFirst.mockImplementation(async () => mockPlayer);
|
||||
playerFindFirstMock.mockImplementation(async () => mockPlayer);
|
||||
|
||||
const result1 = await findOrCreatePlayer('EMILY');
|
||||
const result2 = await findOrCreatePlayer('Emily');
|
||||
@@ -255,7 +261,7 @@ describe('Player Deduplication', () => {
|
||||
});
|
||||
|
||||
test('should handle empty or whitespace-only names', async () => {
|
||||
prisma.player.findFirst.mockImplementation(async () => null);
|
||||
playerFindFirstMock.mockImplementation(async () => null);
|
||||
|
||||
const newPlayer = {
|
||||
id: 100,
|
||||
@@ -270,7 +276,7 @@ describe('Player Deduplication', () => {
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
prisma.player.create.mockImplementation(async () => newPlayer);
|
||||
playerCreateMock.mockImplementation(async () => newPlayer);
|
||||
|
||||
const result = await findOrCreatePlayer(' ');
|
||||
|
||||
@@ -320,7 +326,7 @@ describe('Player Deduplication', () => {
|
||||
rating: 0,
|
||||
};
|
||||
|
||||
prisma.player.findFirst
|
||||
playerFindFirstMock
|
||||
.mockResolvedValueOnce(existingPlayer) // First call for "Emily"
|
||||
.mockResolvedValueOnce(existingPlayer) // Second call for "EMILY"
|
||||
.mockResolvedValueOnce(existingPlayer); // Third call for " Emily "
|
||||
|
||||
Reference in New Issue
Block a user