Skip to content

Astro Test Utils

TODO: Some introduction

How to install

Terminal window
npm i -D @inox-tools/astro-tests

Fixtures

config

Type: AstroConfig

The final config passed to Astro’s programatic CLI entrypoints. This configuration can be overriden on each method call. Will be automatically passed to the methods below:

startDevServer

Type: (inlineConfig:AstroInlineConfig) => Promise<DevServer>

Starts a dev server at an available port.

This server can’t be running at the same time for thein same fixture as .preview() since they share ports. Be sure to call devServer.stop() before test exit.

Equivalent to running astro dev.

build

Type: (inlineConfig:AstroInlineConfig) => Promise<void>

Builds into current folder (will erase previous build).

Equivalent to running astro build.

preview

Type: (inlineConfig:AstroInlineConfig) => Promise<PreviewServer>

Starts a preview server.

This server can’t be running at the same time for thein same fixture as .dev() since they share ports. Be sure to call server.stop() before test exit.

Equivalent to running astro preview.

sync

Type: (inlineConfig:AstroInlineConfig) => Promise<void>

Synchronizes the Astro project and configuration with the generated code, populating the src/env.d.ts file and the .astro directory.

Equivalent to running astro sync.

clean

Type: () => Promise<void>

Deletes the generated files from the fixture directory. Specifically, it deletes:

  • The output directory (outDir config)
  • The cache directory (cacheDir config)
  • The .astro directory generated in the project
  • the .astro directory generated in the node_modules

resolveUrl

Type: (url: string) => string

Resolves a relative URL to the full url of the running server.

This can only be called after either .startDevServer() or .preview() is called.

fetch

Type: (url: string, opts?: RequestInit) => Promise<Response>

Send a request to the given URL. If the URL is relative, it will be resolved relative to the root of the server (without a base path).

This can only be called after either .startDevServer() or .preview() is called.

pathExists

Type: (path: string) => boolean

Checks whether the given path exists on the build output (outDir config).

readFile

Type: (path: string) => Promise<string>

Read a file from the build (relative to outDir config).

editFile

Type: (path: string, updater: string | ((content: string) => string)) => Promise<() => void>

Edit a file in the fixture directory.

The first parameter is a path relative to the root of the fixture.

The second parameter can be the new content of the file or a function that takes the current content and returns the new content.

This function returns a Promise that resolves to another function. This resolved function can be called to revert the changes.

All changes made with editFile are automatically reverted before the process exits.

resetAllFiles

Type: () => void

Reset all changes made with .editFile()

readdir

Type: (path: string) => Promise<string[]>

Read a directory from the build output (relative to outDir config).

This is a convenience wrapper around readdir from Node’s FS Promise API.

glob

Type: (pattern: string) => Promise<string[]>

Find entries in the build output matching the glob pattern.

The glob syntax used is from fast-glob.

loadNodeAdapterHandler

Type: () => Promise<(req: http.IncomingMessage, res: http.ServerResponse) => void>

Load the handler for an app built using the Node Adapter.

The handler is the same as a listener for the request event from Node’s native HTTP module.

loadTestAdapterApp

Type: () => Promise<TestApp>

TestApp

type TestApp = {
render: (req: Request) => Promise<Response>;
toInternalApp: () => App;
};

A minimal proxy to the underlying Astro App using the test adapter.

render

Renders a Response from the given Request.

toInternalApp

Returns the underlying Astro App.

Test Adapter

An Astro Adapter that exposes the rendering process to be called directly.

It also collects information about the build passed to the adapter to be inspected.

None of the options are required.

env

Type: Record<string, string | undefined>

Server-side environment variables to be used by astro:env.

setRoutes

Type: (routes: RouteData[]) => Promise<void>

A callback function that will receive the final value of the project routes.

Utilities

No Node checker

A Vite plugin to ensure no module in the final bundle relies on the built-in Node modules.

If the generated bundle contains any reference to a Node module the build will fail.

The checked modules are those from the built-in module list provided as part of node:modules, both with an without the node: prefix, as well as the prefix-only modules.

TODOs

  • Send a PR to Astro Docs fixing the signature of the dev, build, preview and sync APIs;
  • Send a PR to Astro Docs fixing the documentation of AstroInlineConfig;
  • Send a PR to Astro Docs documenting the DevServer;
  • Send a PR to Astro Docs documenting the PreviewServer;