Skip to content

Commit

Permalink
Fix vitest in Astro 5 (#12009)
Browse files Browse the repository at this point in the history
* Fix vitest in Astro 5

* Add a changeset
  • Loading branch information
matthewp authored Sep 16, 2024
1 parent 2062958 commit f10a3b7
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-mice-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes use of Vitest with Astro 5
3 changes: 2 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@
"rollup": "^4.21.2",
"sass": "^1.78.0",
"undici": "^6.19.8",
"unified": "^11.0.5"
"unified": "^11.0.5",
"vitest": "^2.1.1"
},
"engines": {
"node": "^18.17.1 || ^20.3.0 || >=21.0.0",
Expand Down
6 changes: 4 additions & 2 deletions packages/astro/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { UserConfig as ViteUserConfig } from 'vite';
import { Logger } from '../core/logger/core.js';
import { createRouteManifest } from '../core/routing/index.js';
import type { AstroInlineConfig, AstroUserConfig } from '../types/public/config.js';
import { createDevelopmentManifest } from '../vite-plugin-astro-server/plugin.js';

export function defineConfig(config: AstroUserConfig) {
return config;
Expand All @@ -12,7 +13,7 @@ export function getViteConfig(
inlineAstroConfig: AstroInlineConfig = {},
) {
// Return an async Vite config getter which exposes a resolved `mode` and `command`
return async ({ mode, command }: { mode: string; command: 'serve' | 'build' }) => {
return async ({ mode, command }: { mode: 'dev'; command: 'serve' | 'build' }) => {
// Vite `command` is `serve | build`, but Astro uses `dev | build`
const cmd = command === 'serve' ? 'dev' : command;

Expand Down Expand Up @@ -42,6 +43,7 @@ export function getViteConfig(
let settings = await createSettings(config, userViteConfig.root);
settings = await runHookConfigSetup({ settings, command: cmd, logger });
const manifest = await createRouteManifest({ settings }, logger);
const devSSRManifest = createDevelopmentManifest(settings);
const viteConfig = await createVite(
{
mode,
Expand All @@ -50,7 +52,7 @@ export function getViteConfig(
astroContentListenPlugin({ settings, logger, fs }),
],
},
{ settings, logger, mode, sync: false, manifest },
{ settings, logger, mode, sync: false, manifest, ssrManifest: devSSRManifest },
);
await runHookConfigDone({ settings, logger });
return mergeConfig(viteConfig, userViteConfig);
Expand Down
14 changes: 9 additions & 5 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,21 @@ import { joinPaths } from './path.js';
import { vitePluginServerIslands } from './server-islands/vite-plugin-server-islands.js';
import { isObject } from './util.js';

interface CreateViteOptions {
type CreateViteOptions = {
settings: AstroSettings;
logger: Logger;
mode: 'dev' | 'build' | string;
// will be undefined when using `getViteConfig`
command?: 'dev' | 'build';
fs?: typeof nodeFs;
sync: boolean;
manifest: ManifestData;
} & ({
mode: 'dev';
ssrManifest: SSRManifest;
} | {
mode: 'build';
ssrManifest?: SSRManifest;
}
});

const ALWAYS_NOEXTERNAL = [
// This is only because Vite's native ESM doesn't resolve "exports" correctly.
Expand Down Expand Up @@ -134,8 +138,8 @@ export async function createVite(
astroScriptsPlugin({ settings }),
// The server plugin is for dev only and having it run during the build causes
// the build to run very slow as the filewatcher is triggered often.
mode !== 'build' &&
vitePluginAstroServer({ settings, logger, fs, manifest, ssrManifest: ssrManifest! }), // ssrManifest is only required in dev mode, where it gets created before a Vite instance is created, and get passed to this function
mode === 'dev' &&
vitePluginAstroServer({ settings, logger, fs, manifest, ssrManifest }), // ssrManifest is only required in dev mode, where it gets created before a Vite instance is created, and get passed to this function
envVitePlugin({ settings }),
astroEnv({ settings, mode, sync }),
markdownVitePlugin({ settings, logger }),
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/test/fixtures/vitest/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'astro/config';

export default defineConfig({
output: "static",
});
13 changes: 13 additions & 0 deletions packages/astro/test/fixtures/vitest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@test/vitest",
"version": "0.0.0",
"type": "module",
"private": true,
"scripts": {
"test": "vitest run"
},
"dependencies": {
"astro": "workspace:*",
"vitest": "^2.1.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, test } from 'vitest';

test('sup', () => {
expect(true).toBeTruthy();
})
7 changes: 7 additions & 0 deletions packages/astro/test/fixtures/vitest/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
<title>testing</title>
</head>
<body>
</body>
</html>
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/vitest/vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference types="vitest" />
import { getViteConfig } from 'astro/config';

export default getViteConfig({
test: {
// Vitest configuration options
},
});
25 changes: 25 additions & 0 deletions packages/astro/test/vitest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import { createVitest } from 'vitest/node'

describe('vitest', () => {
it('basics', async () => {
const config = new URL('./fixtures/vitest/vitest.config.js', import.meta.url);

const vitest = await createVitest('test', {
config: fileURLToPath(config),
root: fileURLToPath(new URL('./fixtures/vitest/', import.meta.url)),
watch: false
});

try {
await vitest.start();
} catch(_) {
assert.ok(false, 'test failed');
} finally {
await vitest.close();
}
});
});
Loading

0 comments on commit f10a3b7

Please sign in to comment.