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.

A 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:

vitest.config.ts
import { getViteConfig } from 'astro/config';
export default getViteConfig({
test: {
// Vitest configuration options
},
});

See the Astro + Vitest starter template on GitHub.

Cypress is a front-end testing tool built for the modern web. Cypress enables you to write end-to-end tests for your Astro site.

You can install Cypress using the package manager of your choice.

Terminal window
npm install -D cypress

This will install Cypress locally as a dev dependency for your project.

In the root of your project, create a cypress.config.js file with the following content:

cypress.config.js
import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
supportFile: false
}
})

Create your first Cypress test

Section titled Create your first Cypress test
  1. 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 island architecture." />
    </head>
    <body>
    <h1>Hello world from Astro</h1>
    </body>
    </html>
  2. Create an index.cy.js file in the cypress/e2e folder. Use the following test in the file to verify that the page title and header are correct.

    cypress/e2e/index.cy.js
    it('titles are correct', () => {
    const page = cy.visit('http://localhost:4321');
    page.get('title').should('have.text', 'Astro is awesome!')
    page.get('h1').should('have.text', 'Hello world from Astro');
    });

Cypress can be run from the command line or from the Cypress App. The App provides a visual interface for running and debugging your tests.

First, start the dev server so Cypress can access your live site.

To run our test from the previous example using the command line, execute the following command:

Terminal window
npx cypress run

Alternatively, to run the test using the Cypress App, execute the following command:

Terminal window
npx cypress open

Once the Cypress App is launched, choose E2E Testing, then select the browser to be used to run tests.

Once the test run is finished, you should see green check marks in the output confirming that your test passed:

Output from npx cypress run
Running: index.cy.js (1 of 1)
titles are correct (107ms)
1 passing (1s)

More information about Cypress can be found in the links below:

Playwright 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.

You 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.

Terminal window
npm init playwright@latest

Create your first Playwright test

Section titled Create your first Playwright test
  1. 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 island architecture." />
    </head>
    <body></body>
    </html>
  2. Create a new folder and add the following test file in src/test. Copy and paste the following test into the file to verify that the page meta information is correct. Update the value of the page <title> to match the page you are testing.

    src/test/index.spec.ts
    import { test, expect } from '@playwright/test';
    test('meta is correct', async ({ page }) => {
    await page.goto("http://localhost:4321/");
    await expect(page).toHaveTitle('Astro is awesome!');
    });

You can run a single test or several tests at once, testing one or multiple browsers. By default, your test results will be shown in the terminal. Optionally, you can open the HTML Test Reporter to show a full report and filter test results.

  1. To run our test from the previous example using the command line, use the test command. Optionally, include the file name to run just the single test:

    Terminal window
    npx playwright test index.spec.ts
  2. To see the full HTML Test Report, open it using the following command:

    Terminal window
    npx playwright show-report

Advanced: Launching a development web server during the tests

Section titled Advanced: Launching a development web server during the tests

You can also have Playwright start your server when you run your testing script by using the webServer option in the Playwright configuration file.

Here is an example of the configuration and commands required when using npm:

  1. Add a test script to your package.json file in the project root, such as "test:e2e": "playwright test".

  2. In playwright.config.ts, add the webServer object and update the command value to npm run preview.

    playwright.config.ts
    import { defineConfig } from '@playwright/test';
    export default defineConfig({
    webServer: {
    command: 'npm run preview',
    url: 'http://localhost:4321/',
    timeout: 120 * 1000,
    reuseExistingServer: !process.env.CI,
    },
    use: {
    baseURL: 'http://localhost:4321/',
    },
    });
  3. Run npm run build, then run npm run test:e2e to run the Playwright tests.

More information about Playwright can be found in the links below: