Skip to content

Commit

Permalink
test: add vitest and istanbul (#7)
Browse files Browse the repository at this point in the history
* test: add vitest and istanbul

* test archetypes

* test collections

* changeset
  • Loading branch information
goulvenclech authored May 9, 2024
1 parent eb0edff commit b5be9dc
Show file tree
Hide file tree
Showing 10 changed files with 642 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-lemons-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@goulvenclech/astropi": patch
---

Add tests for collections and archetypes public functions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ dist/
# generated types
.astro/

# generated by Vitest UI
coverage/

# dependencies
node_modules/

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
"description": "",
"type": "module",
"scripts": {
"version": "pnpm changeset version && pnpm i --no-frozen-lockfile"
"version": "pnpm changeset version && pnpm i --no-frozen-lockfile",
"test": "vitest --ui --coverage"
},
"keywords": [],
"author": "",
"license": "ISC",
"packageManager": "pnpm@8.11.0",
"devDependencies": {
"astro": "^4.8.1"
"@vitest/coverage-istanbul": "^1.5.3",
"@vitest/ui": "^1.5.3",
"astro": "^4.8.1",
"vitest": "^1.5.3"
},
"dependencies": {
"@changesets/cli": "^2.27.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/astropi/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface AstropiUserConfig {
type Archetype = {
name: string
path: string
collection: any
collection: string
type: "blog-content" | "docs-content" | "docs-openapi"
}
// Used by "./virtual-user-config.ts"
Expand Down
43 changes: 43 additions & 0 deletions packages/astropi/utils/archetypes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest"
import { vi } from "vitest"
import { getCurrentArchetype } from "./archetypes"

vi.mock("virtual:astropi-user-config", () => {
const userConfig = {
projectName: "Astropi Project",
archetypes: [
{
name: "Blog",
path: "blog",
collection: "blog",
type: "blog-content",
},
{
name: "Changelog",
path: "changelog",
collection: "changelog",
type: "blog-content",
},
{
name: "Docs",
path: "docs",
collection: "docs",
type: "docs-content",
},
],
}
return { userConfig }
})

describe("getCurrentArchetype", () => {
it("should return the current archetype based on the URL location", () => {
const location = new URL("https://astro.build/blog")
const archetype = getCurrentArchetype(location)
expect(archetype).toEqual({
name: "Blog",
path: "blog",
collection: "blog",
type: "blog-content",
})
})
})
61 changes: 61 additions & 0 deletions packages/astropi/utils/collections.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Test packages/astropi/utils/collections.ts exported functions
import { describe, it, expect, vi } from "vitest"
import { generateCollections } from "./collections"

describe("generateCollections", () => {
it("should generate collections based on the user's configuration", () => {
const archetypes: Archetype[] = [
{
name: "blog",
path: "blog",
collection: "blog",
type: "blog-content",
},
{
name: "docs",
path: "docs",
collection: "docs",
type: "docs-content",
},
{
name: "docs-reference",
path: "reference",
collection: "reference",
type: "docs-openapi",
},
]
const injectRoute = vi.fn()
generateCollections(injectRoute, archetypes)
expect(injectRoute).toHaveBeenCalledTimes(6)
expect(injectRoute).toHaveBeenCalledWith({
pattern: "/blog",
entrypoint: "@goulvenclech/astropi/pages/blog.astro",
prerender: true,
})
expect(injectRoute).toHaveBeenCalledWith({
pattern: "/blog/[...slug]",
entrypoint: "@goulvenclech/astropi/pages/[...blog].astro",
prerender: true,
})
expect(injectRoute).toHaveBeenCalledWith({
pattern: "/docs",
entrypoint: "@goulvenclech/astropi/pages/docs-content.astro",
prerender: true,
})
expect(injectRoute).toHaveBeenCalledWith({
pattern: "/docs/[...slug]",
entrypoint: "@goulvenclech/astropi/pages/[...docs-content].astro",
prerender: true,
})
expect(injectRoute).toHaveBeenCalledWith({
pattern: "/reference",
entrypoint: "@goulvenclech/astropi/pages/docs-openapi.astro",
prerender: true,
})
expect(injectRoute).toHaveBeenCalledWith({
pattern: "/reference/[...slug]",
entrypoint: "@goulvenclech/astropi/pages/[...docs-openapi].astro",
prerender: true,
})
})
})
4 changes: 0 additions & 4 deletions packages/astropi/utils/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ function getEntryPoint(archetype: Archetype): string {
return "@goulvenclech/astropi/pages/docs-content.astro"
case "docs-openapi":
return "@goulvenclech/astropi/pages/docs-openapi.astro"
default:
return "@goulvenclech/astropi/pages/index.astro"
}
}

Expand All @@ -61,7 +59,5 @@ function getDynamicEntryPoint(archetype: Archetype): string {
return "@goulvenclech/astropi/pages/[...docs-content].astro"
case "docs-openapi":
return "@goulvenclech/astropi/pages/[...docs-openapi].astro"
default:
return "@goulvenclech/astropi/pages/index.astro"
}
}
Loading

0 comments on commit b5be9dc

Please sign in to comment.