feat(features): add test factories to all 5 features
Adds src/__factories__/<entity>.factory.ts to auth, blog, marketing-pages, navigation, media. Each factory uses defineFactory from @repo/core-testing with stable date defaults (2026-01-01) so snapshot diffs reflect SUT behavior only. Refactors mechanical inline-fixture tests to use factories. Also adds vitest.config.ts and tsconfig path alias to @repo/media (lacked both), and adds @repo/core-testing devDependency to @repo/media. Spec: §5.1, §6.3
This commit is contained in:
2
packages/auth/src/__factories__/index.ts
Normal file
2
packages/auth/src/__factories__/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export { userFactory } from "./user.factory.js";
|
||||||
|
export { sessionFactory } from "./session.factory.js";
|
||||||
26
packages/auth/src/__factories__/session.factory.test.ts
Normal file
26
packages/auth/src/__factories__/session.factory.test.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from "vitest";
|
||||||
|
import { sessionFactory } from "@/__factories__/session.factory";
|
||||||
|
|
||||||
|
describe("sessionFactory", () => {
|
||||||
|
beforeEach(() => sessionFactory.reset());
|
||||||
|
|
||||||
|
it("returns a Session with stable defaults", () => {
|
||||||
|
const s = sessionFactory.build();
|
||||||
|
expect(s.id).toBe("session-1");
|
||||||
|
expect(s.userId).toBe("user-1");
|
||||||
|
expect(s.expiresAt).toBeInstanceOf(Date);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies overrides", () => {
|
||||||
|
const s = sessionFactory.build({ userId: "user-42" });
|
||||||
|
expect(s.userId).toBe("user-42");
|
||||||
|
expect(s.id).toBe("session-1");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("increments sequence per build", () => {
|
||||||
|
const a = sessionFactory.build();
|
||||||
|
const b = sessionFactory.build();
|
||||||
|
expect(a.id).toBe("session-1");
|
||||||
|
expect(b.id).toBe("session-2");
|
||||||
|
});
|
||||||
|
});
|
||||||
8
packages/auth/src/__factories__/session.factory.ts
Normal file
8
packages/auth/src/__factories__/session.factory.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { defineFactory } from "@repo/core-testing/factory";
|
||||||
|
import type { Session } from "../entities/session.js";
|
||||||
|
|
||||||
|
export const sessionFactory = defineFactory<Session>(({ sequence }) => ({
|
||||||
|
id: `session-${sequence}`,
|
||||||
|
userId: "user-1",
|
||||||
|
expiresAt: new Date("2026-12-31T23:59:59Z"),
|
||||||
|
}));
|
||||||
26
packages/auth/src/__factories__/user.factory.test.ts
Normal file
26
packages/auth/src/__factories__/user.factory.test.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from "vitest";
|
||||||
|
import { userFactory } from "@/__factories__/user.factory";
|
||||||
|
|
||||||
|
describe("userFactory", () => {
|
||||||
|
beforeEach(() => userFactory.reset());
|
||||||
|
|
||||||
|
it("returns a User with stable defaults", () => {
|
||||||
|
const u = userFactory.build();
|
||||||
|
expect(u.id).toBe("user-1");
|
||||||
|
expect(u.username).toBe("user1");
|
||||||
|
expect(u.passwordHash).toHaveLength(60);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies overrides", () => {
|
||||||
|
const u = userFactory.build({ username: "alice" });
|
||||||
|
expect(u.username).toBe("alice");
|
||||||
|
expect(u.id).toBe("user-1");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("increments sequence per build", () => {
|
||||||
|
const a = userFactory.build();
|
||||||
|
const b = userFactory.build();
|
||||||
|
expect(a.id).toBe("user-1");
|
||||||
|
expect(b.id).toBe("user-2");
|
||||||
|
});
|
||||||
|
});
|
||||||
8
packages/auth/src/__factories__/user.factory.ts
Normal file
8
packages/auth/src/__factories__/user.factory.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { defineFactory } from "@repo/core-testing/factory";
|
||||||
|
import type { User } from "../entities/user.js";
|
||||||
|
|
||||||
|
export const userFactory = defineFactory<User>(({ sequence }) => ({
|
||||||
|
id: `user-${sequence}`,
|
||||||
|
username: `user${sequence}`,
|
||||||
|
passwordHash: `$2b$10$stablehashfortest${sequence}`.padEnd(60, "x"),
|
||||||
|
}));
|
||||||
27
packages/blog/src/__factories__/article.factory.test.ts
Normal file
27
packages/blog/src/__factories__/article.factory.test.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from "vitest";
|
||||||
|
import { articleFactory } from "@/__factories__/article.factory";
|
||||||
|
|
||||||
|
describe("articleFactory", () => {
|
||||||
|
beforeEach(() => articleFactory.reset());
|
||||||
|
|
||||||
|
it("returns an Article with stable defaults", () => {
|
||||||
|
const a = articleFactory.build();
|
||||||
|
expect(a.title).toBe("Article 1");
|
||||||
|
expect(a.slug).toBe("article-1");
|
||||||
|
expect(a.status).toBe("draft");
|
||||||
|
expect(a.createdAt).toEqual(new Date("2026-01-01T00:00:00Z"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies overrides", () => {
|
||||||
|
const a = articleFactory.build({ status: "published", title: "X" });
|
||||||
|
expect(a.status).toBe("published");
|
||||||
|
expect(a.title).toBe("X");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("increments sequence per build", () => {
|
||||||
|
const a = articleFactory.build();
|
||||||
|
const b = articleFactory.build();
|
||||||
|
expect(a.id).toBe("article-1");
|
||||||
|
expect(b.id).toBe("article-2");
|
||||||
|
});
|
||||||
|
});
|
||||||
13
packages/blog/src/__factories__/article.factory.ts
Normal file
13
packages/blog/src/__factories__/article.factory.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { defineFactory } from "@repo/core-testing/factory";
|
||||||
|
import type { Article } from "../entities/article.js";
|
||||||
|
|
||||||
|
export const articleFactory = defineFactory<Article>(({ sequence }) => ({
|
||||||
|
id: `article-${sequence}`,
|
||||||
|
title: `Article ${sequence}`,
|
||||||
|
slug: `article-${sequence}`,
|
||||||
|
content: null,
|
||||||
|
status: "draft",
|
||||||
|
authorId: "user-1",
|
||||||
|
createdAt: new Date("2026-01-01T00:00:00Z"),
|
||||||
|
updatedAt: new Date("2026-01-01T00:00:00Z"),
|
||||||
|
}));
|
||||||
1
packages/blog/src/__factories__/index.ts
Normal file
1
packages/blog/src/__factories__/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { articleFactory } from "./article.factory.js";
|
||||||
@@ -3,6 +3,7 @@ import { blogContainer } from "../../di/container";
|
|||||||
import { BLOG_SYMBOLS } from "../../di/symbols";
|
import { BLOG_SYMBOLS } from "../../di/symbols";
|
||||||
import type { IArticlesRepository } from "../../application/repositories/articles-repository.interface";
|
import type { IArticlesRepository } from "../../application/repositories/articles-repository.interface";
|
||||||
import { MockArticlesRepository } from "../../infrastructure/repositories/mock-articles.repository";
|
import { MockArticlesRepository } from "../../infrastructure/repositories/mock-articles.repository";
|
||||||
|
import { articleFactory } from "../../__factories__/article.factory";
|
||||||
import { getArticlesUseCase } from "./get-articles.use-case";
|
import { getArticlesUseCase } from "./get-articles.use-case";
|
||||||
|
|
||||||
describe("getArticlesUseCase", () => {
|
describe("getArticlesUseCase", () => {
|
||||||
@@ -16,47 +17,19 @@ describe("getArticlesUseCase", () => {
|
|||||||
blogContainer
|
blogContainer
|
||||||
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
|
.bind<IArticlesRepository>(BLOG_SYMBOLS.IArticlesRepository)
|
||||||
.toConstantValue(repo);
|
.toConstantValue(repo);
|
||||||
|
articleFactory.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns all articles with no filters", async () => {
|
it("returns all articles with no filters", async () => {
|
||||||
const now = new Date();
|
await repo.createArticle(articleFactory.build({ id: "1", title: "A", slug: "a" }));
|
||||||
await repo.createArticle({
|
|
||||||
id: "1",
|
|
||||||
title: "A",
|
|
||||||
slug: "a",
|
|
||||||
content: null,
|
|
||||||
status: "draft",
|
|
||||||
authorId: "u1",
|
|
||||||
createdAt: now,
|
|
||||||
updatedAt: now,
|
|
||||||
});
|
|
||||||
const result = await getArticlesUseCase();
|
const result = await getArticlesUseCase();
|
||||||
expect(result).toHaveLength(1);
|
expect(result).toHaveLength(1);
|
||||||
expect(result[0]?.id).toBe("1");
|
expect(result[0]?.id).toBe("1");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("filters by status", async () => {
|
it("filters by status", async () => {
|
||||||
const now = new Date();
|
await repo.createArticle(articleFactory.build({ id: "1", title: "A", slug: "a", status: "draft" }));
|
||||||
await repo.createArticle({
|
await repo.createArticle(articleFactory.build({ id: "2", title: "B", slug: "b", status: "published" }));
|
||||||
id: "1",
|
|
||||||
title: "A",
|
|
||||||
slug: "a",
|
|
||||||
content: null,
|
|
||||||
status: "draft",
|
|
||||||
authorId: "u1",
|
|
||||||
createdAt: now,
|
|
||||||
updatedAt: now,
|
|
||||||
});
|
|
||||||
await repo.createArticle({
|
|
||||||
id: "2",
|
|
||||||
title: "B",
|
|
||||||
slug: "b",
|
|
||||||
content: null,
|
|
||||||
status: "published",
|
|
||||||
authorId: "u1",
|
|
||||||
createdAt: now,
|
|
||||||
updatedAt: now,
|
|
||||||
});
|
|
||||||
const result = await getArticlesUseCase({ status: "published" });
|
const result = await getArticlesUseCase({ status: "published" });
|
||||||
expect(result).toHaveLength(1);
|
expect(result).toHaveLength(1);
|
||||||
expect(result[0]?.id).toBe("2");
|
expect(result[0]?.id).toBe("2");
|
||||||
|
|||||||
2
packages/marketing-pages/src/__factories__/index.ts
Normal file
2
packages/marketing-pages/src/__factories__/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export { pageFactory } from "./page.factory.js";
|
||||||
|
export { siteSettingsFactory } from "./site-settings.factory.js";
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from "vitest";
|
||||||
|
import { pageFactory } from "@/__factories__/page.factory";
|
||||||
|
|
||||||
|
describe("pageFactory", () => {
|
||||||
|
beforeEach(() => pageFactory.reset());
|
||||||
|
|
||||||
|
it("returns a Page with stable defaults", () => {
|
||||||
|
const p = pageFactory.build();
|
||||||
|
expect(p.id).toBe("page-1");
|
||||||
|
expect(p.title).toBe("Page 1");
|
||||||
|
expect(p.slug).toBe("page-1");
|
||||||
|
expect(p.status).toBe("draft");
|
||||||
|
expect(p.createdAt).toEqual(new Date("2026-01-01T00:00:00Z"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies overrides", () => {
|
||||||
|
const p = pageFactory.build({ status: "published", title: "About" });
|
||||||
|
expect(p.status).toBe("published");
|
||||||
|
expect(p.title).toBe("About");
|
||||||
|
expect(p.id).toBe("page-1");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("increments sequence per build", () => {
|
||||||
|
const a = pageFactory.build();
|
||||||
|
const b = pageFactory.build();
|
||||||
|
expect(a.id).toBe("page-1");
|
||||||
|
expect(b.id).toBe("page-2");
|
||||||
|
});
|
||||||
|
});
|
||||||
19
packages/marketing-pages/src/__factories__/page.factory.ts
Normal file
19
packages/marketing-pages/src/__factories__/page.factory.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { defineFactory } from "@repo/core-testing/factory";
|
||||||
|
import type { Page } from "../entities/page.js";
|
||||||
|
|
||||||
|
export const pageFactory = defineFactory<Page>(({ sequence }) => ({
|
||||||
|
id: `page-${sequence}`,
|
||||||
|
title: `Page ${sequence}`,
|
||||||
|
slug: `page-${sequence}`,
|
||||||
|
hero: {
|
||||||
|
heading: `Hero ${sequence}`,
|
||||||
|
},
|
||||||
|
layout: [],
|
||||||
|
status: "draft",
|
||||||
|
publishedAt: null,
|
||||||
|
seo: {
|
||||||
|
title: `Page ${sequence} | Site`,
|
||||||
|
},
|
||||||
|
createdAt: new Date("2026-01-01T00:00:00Z"),
|
||||||
|
updatedAt: new Date("2026-01-01T00:00:00Z"),
|
||||||
|
}));
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from "vitest";
|
||||||
|
import { siteSettingsFactory } from "@/__factories__/site-settings.factory";
|
||||||
|
|
||||||
|
describe("siteSettingsFactory", () => {
|
||||||
|
beforeEach(() => siteSettingsFactory.reset());
|
||||||
|
|
||||||
|
it("returns SiteSettings with stable defaults", () => {
|
||||||
|
const s = siteSettingsFactory.build();
|
||||||
|
expect(s.siteName).toBe("Site 1");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies overrides", () => {
|
||||||
|
const s = siteSettingsFactory.build({ siteName: "My Brand" });
|
||||||
|
expect(s.siteName).toBe("My Brand");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("increments sequence per build (siteName reflects it)", () => {
|
||||||
|
const a = siteSettingsFactory.build();
|
||||||
|
const b = siteSettingsFactory.build();
|
||||||
|
expect(a.siteName).toBe("Site 1");
|
||||||
|
expect(b.siteName).toBe("Site 2");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import { defineFactory } from "@repo/core-testing/factory";
|
||||||
|
import type { SiteSettings } from "../entities/site-settings.js";
|
||||||
|
|
||||||
|
export const siteSettingsFactory = defineFactory<SiteSettings>(({ sequence }) => ({
|
||||||
|
siteName: `Site ${sequence}`,
|
||||||
|
}));
|
||||||
@@ -18,6 +18,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@repo/core-eslint": "workspace:*",
|
"@repo/core-eslint": "workspace:*",
|
||||||
|
"@repo/core-testing": "workspace:*",
|
||||||
"@repo/core-typescript": "workspace:*",
|
"@repo/core-typescript": "workspace:*",
|
||||||
"vitest": "^3.1.0"
|
"vitest": "^3.1.0"
|
||||||
}
|
}
|
||||||
|
|||||||
1
packages/media/src/__factories__/index.ts
Normal file
1
packages/media/src/__factories__/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { mediaFactory, type Media } from "./media.factory.js";
|
||||||
28
packages/media/src/__factories__/media.factory.test.ts
Normal file
28
packages/media/src/__factories__/media.factory.test.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from "vitest";
|
||||||
|
import { mediaFactory } from "@/__factories__/media.factory";
|
||||||
|
|
||||||
|
describe("mediaFactory", () => {
|
||||||
|
beforeEach(() => mediaFactory.reset());
|
||||||
|
|
||||||
|
it("returns a Media object with stable defaults", () => {
|
||||||
|
const m = mediaFactory.build();
|
||||||
|
expect(m.id).toBe("media-1");
|
||||||
|
expect(m.alt).toBe("Media 1");
|
||||||
|
expect(m.mimeType).toBe("image/png");
|
||||||
|
expect(m.filesize).toBe(1024);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies overrides", () => {
|
||||||
|
const m = mediaFactory.build({ alt: "Custom alt", mimeType: "image/jpeg" });
|
||||||
|
expect(m.alt).toBe("Custom alt");
|
||||||
|
expect(m.mimeType).toBe("image/jpeg");
|
||||||
|
expect(m.id).toBe("media-1");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("increments sequence per build", () => {
|
||||||
|
const a = mediaFactory.build();
|
||||||
|
const b = mediaFactory.build();
|
||||||
|
expect(a.id).toBe("media-1");
|
||||||
|
expect(b.id).toBe("media-2");
|
||||||
|
});
|
||||||
|
});
|
||||||
19
packages/media/src/__factories__/media.factory.ts
Normal file
19
packages/media/src/__factories__/media.factory.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { defineFactory } from "@repo/core-testing/factory";
|
||||||
|
|
||||||
|
export interface Media {
|
||||||
|
id: string;
|
||||||
|
alt: string;
|
||||||
|
url: string;
|
||||||
|
filename: string;
|
||||||
|
mimeType: string;
|
||||||
|
filesize: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const mediaFactory = defineFactory<Media>(({ sequence }) => ({
|
||||||
|
id: `media-${sequence}`,
|
||||||
|
alt: `Media ${sequence}`,
|
||||||
|
url: `https://cdn.example.com/media-${sequence}.png`,
|
||||||
|
filename: `media-${sequence}.png`,
|
||||||
|
mimeType: "image/png",
|
||||||
|
filesize: 1024,
|
||||||
|
}));
|
||||||
@@ -2,7 +2,11 @@
|
|||||||
"extends": "@repo/core-typescript/base.json",
|
"extends": "@repo/core-typescript/base.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"lib": ["ES2022", "DOM"]
|
"rootDir": ".",
|
||||||
|
"lib": ["ES2022", "DOM"],
|
||||||
|
"paths": {
|
||||||
|
"@/*": ["./src/*"]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"include": ["src/**/*"],
|
"include": ["src/**/*"],
|
||||||
"exclude": ["node_modules", "dist"]
|
"exclude": ["node_modules", "dist"]
|
||||||
|
|||||||
9
packages/media/vitest.config.ts
Normal file
9
packages/media/vitest.config.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import path from "node:path";
|
||||||
|
import { mergeConfig } from "vitest/config";
|
||||||
|
import { nodeVitestConfig } from "@repo/core-typescript/vitest.base.node";
|
||||||
|
|
||||||
|
export default mergeConfig(nodeVitestConfig, {
|
||||||
|
resolve: {
|
||||||
|
alias: { "@": path.resolve(__dirname, "./src") },
|
||||||
|
},
|
||||||
|
});
|
||||||
29
packages/navigation/src/__factories__/header.factory.test.ts
Normal file
29
packages/navigation/src/__factories__/header.factory.test.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from "vitest";
|
||||||
|
import { headerFactory } from "@/__factories__/header.factory";
|
||||||
|
|
||||||
|
describe("headerFactory", () => {
|
||||||
|
beforeEach(() => headerFactory.reset());
|
||||||
|
|
||||||
|
it("returns a Header with stable defaults", () => {
|
||||||
|
const h = headerFactory.build();
|
||||||
|
expect(h.items).toHaveLength(0);
|
||||||
|
expect(h.logoId).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("applies overrides", () => {
|
||||||
|
const h = headerFactory.build({
|
||||||
|
logoId: "logo-42",
|
||||||
|
items: [{ label: "Home", href: "/", external: false }],
|
||||||
|
});
|
||||||
|
expect(h.logoId).toBe("logo-42");
|
||||||
|
expect(h.items).toHaveLength(1);
|
||||||
|
expect(h.items[0]?.label).toBe("Home");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("builds multiple headers independently", () => {
|
||||||
|
const a = headerFactory.build({ items: [{ label: "A", href: "/a", external: false }] });
|
||||||
|
const b = headerFactory.build({ items: [] });
|
||||||
|
expect(a.items).toHaveLength(1);
|
||||||
|
expect(b.items).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
6
packages/navigation/src/__factories__/header.factory.ts
Normal file
6
packages/navigation/src/__factories__/header.factory.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import { defineFactory } from "@repo/core-testing/factory";
|
||||||
|
import type { Header } from "../entities/header.js";
|
||||||
|
|
||||||
|
export const headerFactory = defineFactory<Header>(() => ({
|
||||||
|
items: [],
|
||||||
|
}));
|
||||||
1
packages/navigation/src/__factories__/index.ts
Normal file
1
packages/navigation/src/__factories__/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { headerFactory } from "./header.factory.js";
|
||||||
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@@ -601,6 +601,9 @@ importers:
|
|||||||
'@repo/core-eslint':
|
'@repo/core-eslint':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../core-eslint
|
version: link:../core-eslint
|
||||||
|
'@repo/core-testing':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../core-testing
|
||||||
'@repo/core-typescript':
|
'@repo/core-typescript':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../core-typescript
|
version: link:../core-typescript
|
||||||
|
|||||||
Reference in New Issue
Block a user