switch to bun

This commit is contained in:
2025-09-22 21:39:45 +00:00
parent 049ed4b956
commit 91c14f750e
17 changed files with 208 additions and 276 deletions

View File

@@ -1,4 +1,4 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect } from "bun:test";
import { AccountEntity } from "../src/domain/account/entity/AccountEntity";
import {
InvalidEmailFormatError,

View File

@@ -1,20 +1,24 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { describe, it, expect, jest, beforeEach } from "bun:test";
import AccountRepository from "../src/domain/account/repository/AccoutRepository";
import { DatabaseInterface } from "../src/database/DatabaseInterface";
import { AccountEntity } from "../src/domain/account/entity/AccountEntity";
describe("AccountRepository", () => {
let accountRepository: AccountRepository;
let mockDatabase: DatabaseInterface;
let mockDatabase: {
ping: ReturnType<typeof jest.fn>;
fetchAll: ReturnType<typeof jest.fn>;
fetchOne: ReturnType<typeof jest.fn>;
execute: ReturnType<typeof jest.fn>;
};
beforeEach(() => {
mockDatabase = {
ping: vi.fn(),
fetchAll: vi.fn(),
fetchOne: vi.fn(),
execute: vi.fn(),
ping: jest.fn(() => Promise.resolve()),
fetchAll: jest.fn(() => Promise.resolve([])),
fetchOne: jest.fn(() => Promise.resolve(undefined)),
execute: jest.fn(() => Promise.resolve()),
};
accountRepository = new AccountRepository(mockDatabase);
accountRepository = new AccountRepository(mockDatabase as any);
});
describe("findByEmail", () => {
@@ -29,7 +33,9 @@ describe("AccountRepository", () => {
updated_at: new Date(),
};
vi.mocked(mockDatabase.fetchOne).mockResolvedValue(mockResult);
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve(mockResult),
);
const result = await accountRepository.findByEmail(email);
@@ -46,7 +52,9 @@ describe("AccountRepository", () => {
it("should return null when account not found", async () => {
const email = "nonexistent@example.com";
vi.mocked(mockDatabase.fetchOne).mockResolvedValue(undefined);
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve(undefined),
);
const result = await accountRepository.findByEmail(email);
@@ -66,7 +74,9 @@ describe("AccountRepository", () => {
updated_at: new Date(),
};
vi.mocked(mockDatabase.fetchOne).mockResolvedValue(mockResult);
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve(mockResult),
);
const result = await accountRepository.findById(id);
@@ -83,7 +93,9 @@ describe("AccountRepository", () => {
it("should return null when account not found", async () => {
const id = "nonexistent";
vi.mocked(mockDatabase.fetchOne).mockResolvedValue(undefined);
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve(undefined),
);
const result = await accountRepository.findById(id);
@@ -95,7 +107,9 @@ describe("AccountRepository", () => {
it("should save account successfully", async () => {
const account = AccountEntity.create("test@example.com", "password123");
vi.mocked(mockDatabase.fetchOne).mockResolvedValue({ id: account.id });
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve({ id: account.id }),
);
const result = await accountRepository.save(account);

View File

@@ -1,6 +1,5 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { describe, it, expect, jest, beforeEach } from "bun:test";
import AccountService from "../src/domain/account/service/AccountService";
import { AccountRepositoryInterface } from "../src/domain/account/repository/AccountRepositoryInterface";
import { AccountEntity } from "../src/domain/account/entity/AccountEntity";
import {
AccountNotFoundError,
@@ -10,15 +9,19 @@ import {
describe("AccountService", () => {
let accountService: AccountService;
let mockAccountRepository: AccountRepositoryInterface;
let mockAccountRepository: {
findByEmail: ReturnType<typeof jest.fn>;
save: ReturnType<typeof jest.fn>;
findById: ReturnType<typeof jest.fn>;
};
beforeEach(() => {
mockAccountRepository = {
findByEmail: vi.fn(),
save: vi.fn(),
findById: vi.fn(),
findByEmail: jest.fn(() => Promise.resolve(null)),
save: jest.fn(() => Promise.resolve("123")),
findById: jest.fn(() => Promise.resolve(null)),
};
accountService = new AccountService(mockAccountRepository);
accountService = new AccountService(mockAccountRepository as any);
});
describe("createAccount", () => {
@@ -26,8 +29,12 @@ describe("AccountService", () => {
const email = "test@example.com";
const password = "password123";
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(null);
vi.mocked(mockAccountRepository.save).mockResolvedValue("123");
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(null),
);
mockAccountRepository.save.mockImplementation(() =>
Promise.resolve("123"),
);
const result = await accountService.register(email, password);
@@ -44,8 +51,8 @@ describe("AccountService", () => {
const password = "password123";
const existingAccount = AccountEntity.create(email, password);
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(
existingAccount,
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(existingAccount),
);
await expect(accountService.register(email, password)).rejects.toThrow(
@@ -60,7 +67,9 @@ describe("AccountService", () => {
const password = "password123";
const account = AccountEntity.create(email, password);
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(account);
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(account),
);
const result = await accountService.login(email, password);
@@ -72,7 +81,9 @@ describe("AccountService", () => {
const email = "test@example.com";
const password = "password123";
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(null);
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(null),
);
await expect(accountService.login(email, password)).rejects.toThrow(
AccountNotFoundError,
@@ -85,7 +96,9 @@ describe("AccountService", () => {
const wrongPassword = "wrongpassword";
const account = AccountEntity.create(email, password);
vi.mocked(mockAccountRepository.findByEmail).mockResolvedValue(account);
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(account),
);
await expect(accountService.login(email, wrongPassword)).rejects.toThrow(
BadPasswordError,

View File

@@ -1,4 +1,4 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect } from "bun:test";
import {
emailSchema,
passwordSchema,