diff --git a/packages/astro/test/content-layer.test.js b/packages/astro/test/content-layer.test.js index f170f4892cc44..28a3c3811657b 100644 --- a/packages/astro/test/content-layer.test.js +++ b/packages/astro/test/content-layer.test.js @@ -87,6 +87,14 @@ describe('Content Layer', () => { ]); }); + it('Returns nested json `file()` loader collection', async () => { + assert.ok(json.hasOwnProperty('nestedJsonLoader')); + assert.ok(Array.isArray(json.nestedJsonLoader)); + + const ids = json.nestedJsonLoader.map((item) => item.data.id); + assert.deepEqual(ids, ['bluejay', 'robin', 'sparrow', 'cardinal', 'goldfinch']); + }); + it('Returns yaml `file()` loader collection', async () => { assert.ok(json.hasOwnProperty('yamlLoader')); assert.ok(Array.isArray(json.yamlLoader)); diff --git a/packages/astro/test/fixtures/content-layer/src/content/config.ts b/packages/astro/test/fixtures/content-layer/src/content/config.ts index e8fc7fb84adee..6659dfdad39b4 100644 --- a/packages/astro/test/fixtures/content-layer/src/content/config.ts +++ b/packages/astro/test/fixtures/content-layer/src/content/config.ts @@ -76,6 +76,18 @@ const fish = defineCollection({ }), }); +const birds = defineCollection({ + loader: file('src/data/birds.json', { + parser: (text) => JSON.parse(text).birds, + }), + schema: z.object({ + id: z.string(), + name: z.string(), + breed: z.string(), + age: z.number(), + }), +}); + // Absolute paths should also work const absoluteRoot = new URL('../../content-outside-src', import.meta.url); @@ -164,6 +176,7 @@ export const collections = { dogs, cats, fish, + birds, numbers, spacecraft, increment, diff --git a/packages/astro/test/fixtures/content-layer/src/data/birds.json b/packages/astro/test/fixtures/content-layer/src/data/birds.json new file mode 100644 index 0000000000000..3e7d83795a127 --- /dev/null +++ b/packages/astro/test/fixtures/content-layer/src/data/birds.json @@ -0,0 +1,34 @@ +{ + "birds": [ + { + "id": "bluejay", + "name": "Blue Jay", + "breed": "Cyanocitta cristata", + "age": 3 + }, + { + "id": "robin", + "name": "Robin", + "breed": "Turdus migratorius", + "age": 2 + }, + { + "id": "sparrow", + "name": "Sparrow", + "breed": "Passer domesticus", + "age": 1 + }, + { + "id": "cardinal", + "name": "Cardinal", + "breed": "Cardinalis cardinalis", + "age": 4 + }, + { + "id": "goldfinch", + "name": "Goldfinch", + "breed": "Spinus tristis", + "age": 2 + } + ] +} diff --git a/packages/astro/test/fixtures/content-layer/src/pages/collections.json.js b/packages/astro/test/fixtures/content-layer/src/pages/collections.json.js index 18265095f9b3d..c02d5ee17d81c 100644 --- a/packages/astro/test/fixtures/content-layer/src/pages/collections.json.js +++ b/packages/astro/test/fixtures/content-layer/src/pages/collections.json.js @@ -24,6 +24,8 @@ export async function GET() { const tomlLoader = await getCollection('songs'); + const nestedJsonLoader = await getCollection('birds'); + return new Response( devalue.stringify({ customLoader, @@ -37,6 +39,7 @@ export async function GET() { images, yamlLoader, tomlLoader, + nestedJsonLoader, }), ); }