Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vitest fails because of the ImageFunction helper (with content collections) #10822

Closed
1 task
ArmandPhilippot opened this issue Apr 19, 2024 · 0 comments · Fixed by #10846
Closed
1 task

Vitest fails because of the ImageFunction helper (with content collections) #10822

ArmandPhilippot opened this issue Apr 19, 2024 · 0 comments · Fixed by #10846
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@ArmandPhilippot
Copy link
Contributor

Astro Info

Astro                    v4.6.3
Node                     v20.12.0
System                   Linux (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

The issue:

Astro offers an image helper to validate the content collections schema using Zod:

const pagesCollection = defineCollection({
  type: 'content',
  schema: ({ image }) =>
    z.object({
      cover: z
        .object({
          alt: z.string(),
          src: image(),
        })
        .optional(),
      title: z.string(),
    }),
});

I have a function that wraps getCollection to manipulate the result before returning it in getStaticPaths to avoid repeats. I want to test that function to be sure nothing is broken.

Now, if no Markdown files use a cover (from the example above), Vitest can run the tests successfully. However if a Markdown file defines a cover, Vitest will fail to run the tests that calls the image helper:

 FAIL  src/utils/collections.test.ts > get-pages > should return the pages
TypeError: Cannot read properties of undefined (reading '_activePlugin')
 ❯ emitFile node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:50850:53
 ❯ emitESMImage node_modules/astro/dist/assets/utils/emitAsset.js:30:20
 ❯ Object.transform node_modules/astro/dist/content/runtime-assets.js:7:24
 ❯ async file:(redacted)/astro-vitest-image-content-collections/node_modules/zod/lib/index.mjs:2267:32
 ❯ async file:(redacted)/astro-vitest-image-content-collections/node_modules/zod/lib/index.mjs:2267:32
 ❯ ZodObject.safeParseAsync node_modules/zod/lib/index.mjs:677:24
     675|         };
     676|         const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
     677|         const result = await (isAsync(maybeAsyncResult)
        |                        ^
     678|             ? maybeAsyncResult
     679|             : Promise.resolve(maybeAsyncResult));
 ❯ getEntryData node_modules/astro/dist/content/utils.js:74:20
 ❯ getContentEntryModule node_modules/astro/dist/content/vite-plugin-content-imports.js:158:35
 ❯ TransformContext.transform node_modules/astro/dist/content/vite-plugin-content-imports.js:74:67
 ❯ Object.transform node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:51186:30

I know, I should probably extract the manipulation in another function and only test that function. So there is a workaround and this is a minor bug. Nonetheless, I think the image helper should not break the tests.

Steps to reproduce:

I included a Stackblitz link with a minimal reproduction. The added files are in src/assets, in src/content and in src/utils (+ vitest.config.ts).

However, its seems it is not possible to run Vitest from the command line (no output). So you need to:

  1. Download the project
  2. Run npm i to install the dependencies
  3. Run npm run test to execute Vitest and to see the error
  4. Edit src/content/pages/page-with-cover.md to remove the cover from the frontmatter
  5. Run again npm run test to execute Vitest and to see no errors

What's the expected result?

Be able to run tests with Vitest for functions that involves Astro's own functions (like getCollection) when the image helper is used with content collections.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/astro-vitest-image-content-collections?file=src%2Futils%2Fcollections.test.ts

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Apr 19, 2024
@matthewp matthewp added - P3: minor bug An edge case that only affects very specific usage (priority) and removed needs triage Issue needs to be triaged labels Apr 22, 2024
@matthewp matthewp self-assigned this Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants