I’ve started to use Astro more and more in my projects for building static stites and blogs. (This site, for example, is now built with Astro!) The concept of content collections is great but I find it very tricky to test for things like “pages with draft: true should not be visible in production but should be visible in development”. If you try to test collections in CI you get an Astro error like this:
The collection "blog" does not exist.Please ensure it is defined in your content config.There is a closed issue with a comment from a core maintainer saying that astro:content isn’t designed to be used outside of the astro runtime so I’m assuming this is not something that the team sees as a problem so I wanted to find a workaround.
Turns out, it’s pretty easy; just starting the dev server once is enough for Astro to create all the necessary data on disk that you can then use Vitest to test collections as well. I use this in GitHub actions:
- name: Test run: | timeout 5s pnpm dev || true pnpm testThe manual timeout is obviously not optimal — but it works!