Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package-lock.json

# Generated documentation
docs/api-reference/
__*__/
.build

CLAUDE.md
54 changes: 54 additions & 0 deletions test/__mocks__/astro-content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Mock for the `astro:content` virtual module.
*
* Implements getCollection/getEntry directly from the pre-built data store,
* bypassing the astro:data-layer-content virtual module which isn't available
* outside of Vite/Astro's build pipeline.
*/
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import * as devalue from 'devalue'

export { defineCollection, defineLiveCollection } from 'astro/content/runtime'
export { z } from 'astro/zod'

// Load the data store once at module init time
const raw = readFileSync(resolve('.astro/data-store.json'), 'utf-8')
const collections: Map<string, Map<string, unknown>> = devalue.unflatten(JSON.parse(raw))

export async function getCollection(collection: string, filter?: (entry: unknown) => unknown) {
const col = collections.get(collection)
if (!col) {
console.warn(`The collection ${JSON.stringify(collection)} does not exist or is empty.`)
return []
}
const entries = [...col.values()].map((raw: any) => ({ ...raw, collection }))
return filter ? entries.filter(filter) : entries
}

export async function getEntry(collectionOrRef: string | { collection: string; id: string }, id?: string) {
const [collection, entryId] =
typeof collectionOrRef === 'object'
? [collectionOrRef.collection, collectionOrRef.id]
: [collectionOrRef, id!]
return collections.get(collection)?.get(entryId) ?? undefined
}

export async function getEntries(entries: { collection: string; id: string }[]) {
return Promise.all(entries.map((e) => getEntry(e)))
}

export function reference(collection: string) {
return (id: string) => ({ collection, id })
}

// Stubs for live collections (not used in tests)
export async function getLiveCollection() {
return { entries: [] }
}
export async function getLiveEntry() {
return { error: new Error('getLiveEntry not supported in tests') }
}
export async function render() {
throw new Error('render() not supported in tests')
}
4 changes: 4 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { getViteConfig } from 'astro/config'
import { resolve } from 'node:path'

export default getViteConfig({
test: {
include: ['test/**/*.test.ts'],
globalSetup: ['test/global-setup.ts'],
alias: {
'astro:content': resolve('./test/__mocks__/astro-content.ts'),
},
},
})
Loading