Testing
Questi contenuti non sono ancora disponibili nella tua lingua.
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:
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()
:
See the Astro + Vitest starter template on GitHub.
Vitest and Container API
Section titled Vitest and Container API
Aggiunto in:
astro@4.9.0
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:
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.
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. -
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.You can set
"baseURL": "http://localhost:4321"
in theplaywright.config.ts
configuration file to usepage.goto("/")
instead ofpage.goto("http://localhost:4321/")
for a more convenient URL.
Running your Playwright tests
Section titled Running your Playwright testsYou 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.
-
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: -
To see the full HTML Test Report, open it using the following command:
Run your tests against your production code to more closely resemble your live, deployed site.
Advanced: Launching a development web server during the tests
Section titled Advanced: Launching a development web server during the testsYou 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:
-
Add a test script to your
package.json
file in the project root, such as"test:e2e": "playwright test"
. -
In
playwright.config.ts
, add thewebServer
object and update the command value tonpm run preview
. -
Run
npm run build
, then runnpm run test:e2e
to run the Playwright tests.
More information about Playwright can be found in the links below:
Cypress
Section titled CypressCypress is a front-end testing tool built for the modern web. Cypress enables you to write end-to-end tests for your Astro site.
Installation
Section titled InstallationYou can install Cypress using the package manager of your choice.
This will install Cypress locally as a dev dependency for your project.
Configuration
Section titled ConfigurationIn the root of your project, create a cypress.config.js
file with the following content:
Create your first Cypress test
Section titled Create your first Cypress test-
Choose a page to test. This example will test the example page
index.astro
below. -
Create an
index.cy.js
file in thecypress/e2e
folder. Use the following test in the file to verify that the page title and header are correct.You can set
"baseUrl": "http://localhost:4321"
in thecypress.config.js
configuration file to usecy.visit("/")
instead ofcy.visit("http://localhost:4321/")
for a more convenient URL.
Running your Cypress tests
Section titled Running your Cypress testsCypress 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:
Alternatively, to run the test using the Cypress App, execute the following command:
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:
To check that your test really does work, you can change the following line in the index.astro
file:
Then run the test again. You should see a red “x” in the output confirming that your test failed.
Next steps
Section titled Next stepsMore information about Cypress can be found in the links below:
NightwatchJS
Section titled NightwatchJSNightwatch.js is a test automation framework with a powerful set of tools to write, run, and debug your tests across the web with built-in support for all major browsers and their mobile equivalents, as well as native mobile applications.
Installation
Section titled InstallationYou can install NightwatchJS within your Astro project using the package manager of your choice. Follow the CLI steps to choose JavaScript/TypeScript, name your test folder, and select whether or not to include component testing and testing on mobile browsers.
Create your first Nightwatch test
Section titled Create your first Nightwatch test-
Choose a page to test. This example will test the example page
index.astro
below. -
Create a new folder
src/test/
and add the following test file:You can set
"baseURL": "http://localhost:4321"
in thenightwatch.conf.js
configuration file to usebrowser.navigateTo("/")
instead ofbrowser.navigateTo("http://localhost:4321/")
for a more convenient URL.
Running your NightwatchJS tests
Section titled Running your NightwatchJS testsYou 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.
You can run the tests with the NightwatchJS VSCode Extension or using the CLI steps below:
-
To run all tests, enter the following command in the terminal. Optionally, include the file name to run just the single test:
Additionally, you can run the tests against a specific browser using the
--environment
or-e
CLI argument. If you don’t have the relevant browser installed, Nightwatch will attempt to set it up for you using Selenium Manager: -
To see the full HTML Test Report, open it using the following command:
Run your tests against your production code to more closely resemble your live, deployed site.
More information about NightwatchJS can be found in the links below:
Learn