Testing
本頁內容尚未翻譯。
Testing helps you write and maintain working Astro code. Astro supports many popular tools for unit tests, component tests, and end-to-end tests including Jest, Mocha, Jasmine, Cypress and Playwright. You can even install framework-specific testing libraries such as React Testing Library to test your UI framework components.
Testing frameworks allow you to state assertions or expectations about how your code should behave in specific situations, then compare these to the actual behavior of your current code.
Unit and integration tests
Section titled Unit and integration testsVitest
Section titled VitestA Vite-native unit test framework with ESM, TypeScript and JSX support powered by esbuild.
Use Astro’s getViteConfig()
helper in your vitest.config.ts
configuration file to set up Vitest with your Astro project’s settings:
/// <reference types="vitest" />import { getViteConfig } from 'astro/config';
export default getViteConfig({ test: { // Vitest configuration options },});
By default, getViteConfig()
will try to load an Astro config file in your project and apply it to the test environment.
As of Astro 4.8, if you need to customize the Astro configuration applied in your tests, pass a second argument to getViteConfig()
:
export default getViteConfig( { test: { /* Vitest configuration options */ } }, { site: 'https://example.com/', trailingSlash: 'always', },);
See the Astro + Vitest starter template on GitHub.
Vitest and Container API
Section titled Vitest and Container API
新增於:
[email protected]
You can natively test Astro components using the container API. First, setup vitest
as explained above, then create a .test.js
file to test your component:
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, { slots: { default: 'Card content', }, });
expect(result).toContain('This is a card'); expect(result).toContain('Card content');});
End-to-end tests
Section titled End-to-end testsPlaywright
Section titled PlaywrightPlaywright is an end-to-end testing framework for modern web apps. Use the Playwright API in JavaScript or TypeScript to test your Astro code on all modern rendering engines including Chromium, WebKit, and Firefox.
Installation
Section titled InstallationYou can get started and run your tests using the VS Code Extension.
Alternatively, you can install Playwright within your Astro project using the package manager of your choice. Follow the CLI steps to choose JavaScript/TypeScript, name your test folder, and add an optional GitHub Actions workflow.
npm init playwright@latest
pnpm create playwright
yarn create playwright
Create your first Playwright test
Section titled Create your first Playwright test-
Choose a page to test. This example will test the example page
index.astro
below.src/pages/index.astro ------<html lang="en"><head><title>Astro is awesome!</title><meta name="description" content="Pull content from anywhere and serve it fast with Astro's next-gen islands architecture." /></head><body></body></html>