Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Astro 5.0.2 with vitest: getViteConfig and renderToSting of container fail

I just upgraded my Astro site to 5.0.2, but there seem to be breaking changes As per the docs, I use the following vitest config (root of project):

/// <reference types="vitest/config" />
import { getViteConfig } from 'astro/config';

export default getViteConfig({
    test: { <-- Syntax highlighting complains here that this does not exist.
      include: ['./tests/*.{test,spec}.?(c|m)[jt]s?(x)'],
      reporters: [
        'default',
        ['junit', { suiteName: 'UI tests' }]
      ],
    }
  });

Then, I have a mock component:

---
const { title, date } = Astro.props;
---
<div>
    <h1>{title}</h1>
    <p>{date}</p>
</div>

On which I try to run the following test:

import { experimental_AstroContainer as AstroContainer } from 'astro/container';
import { expect, test } from 'vitest';
import Card from '../src/components/Card.astro';

test('Card with slots', async () => {
  const container = await AstroContainer.create();
  const result = await container.renderToString(Card, {
    props: {title: "Title", date: "20 Aug 2024"}
  });

  //expect(result).toContain('This is a card');
  //expect(result).toContain('Card content');
});

Now, if I run with the above configuration, I get:

Vitest caught 2 unhandled errors during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

I can run with the normal vitest defineConfig, but then I get:

Error: Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.

Althought I do not see any error. I know that for getViteConfig, there has been an issue, but this is long closed.

Versions: Astro: 5.0.2 Vitest: "^2.1.8"

Any Ideas?

like image 239
thomi Avatar asked Sep 30 '25 10:09

thomi


1 Answers

I had the same syntax highlighting issue, and the astro check script was also reporting an error in this file.

Tests also stopped running correctly after upgrading Astro to version 5.2.5.

What I did to fix it was update the vitest dependency from version 2.1.3 to 3.0.5 using the command:

npm install -D vitest@latest
like image 99
Victor Salles Avatar answered Oct 02 '25 05:10

Victor Salles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!