Allure Vitest
Generate beautiful HTML reports using Allure Report and your Vitest tests.
How to start
1. Prepare your project
Open a terminal and go to the project directory. For example:
Bashcd /home/user/myproject
Install the Allure Report command-line tool, if it is not yet installed in your operating system. Note that Allure Report requires Java, see the installation instructions.
Install the Allure Vitest adapter.
Modify your Vitest configuration file, e.g.,
vitest.config.ts
.Add an
AllureReporter
instance to the list of reporters. If necessary, pass configuration options in the argument for theAllureReporter
, see Allure Vitest configuration.Optionally, add
"allure-vitest/setup"
to thesetupFiles
list. This enables the test plan support and also allows for a cleaner style of function calls throughout the API, see Allure Vitest reference.
Here is an example of a configuration file:
TypeScriptimport AllureReporter from "allure-vitest/reporter"; import { defineConfig } from "vitest/config"; export default defineConfig({ test: { setupFiles: ["allure-vitest/setup"], reporters: ["default", new AllureReporter({})], }, });
2. Run tests
Run your Vitest tests same way as your would run them usually. For example:
This will save necessary data into allure-results
or other directory, according to the Configuration. If the directory already exists, the new files will be added to the existing ones, so that a future report will be based on them all.
3. Generate a report
Finally, run Allure to convert the test results into an HTML report. This will automatically open your browser to view the report.
If necessary, replace allure-results
with the path to the directory specified in the Configuration.
There are some options that can affect how the report is generated. Run allure --help
for the full list of options.
Writing tests
The Allure Vitest adapter extends the standard reporting features of Vitest by providing additional capabilities for crafting more informative and structured tests. This section highlights key enhancements that can be utilized:
- Metadata Annotation: Enhance test reports with descriptions, links, and other metadata.
- Test Organization: Structure your tests into clear hierarchies for better readability and organization organize tests.
- Step Division: Break down tests into smaller test steps for easier understanding and maintenance.
- Parametrized Tests: Clearly describe the parameters for parametrized tests to specify different scenarios.
- Attachments: Automatically capture screenshots and other files during test execution.
- Test Selection: Use a test plan file to select which tests to run, allowing for flexible test execution.
- Environment Details: Include comprehensive environment information to accompany the test report.
Adding Metadata
Allure allows you to enrich your reports with a variety of metadata. This additional information provides context and details for each test, enhancing the report's usefulness. Refer to the metadata reference section for an exhaustive list of what can be added.
To assign a metadata field, call a corresponding method at any point inside a test method's body. Note, however, that it is highly recommended to assign all metadata as early as possible. Otherwise, there is a risk of the test failing before having all metadata set, which is bad for the test report's readability.
TypeScriptimport { test } from "vitest";
test("Test Authentication", async () => {
await allure.displayName("Test Authentication");
await allure.description(
"This test attempts to log into the website using a login and a password. Fails if any error happens.\n\nNote that this test does not test 2-Factor Authentication.",
);
await allure.owner("John Doe");
// ...
});
Organize tests
As described in Improving navigation in your test report, Allure supports multiple ways to organize tests into hierarchical structures.
To specify a test's location in the behavior-based hierarchy:
TypeScriptimport { test } from "vitest";
test("Test Authentication", async () => {
await allure.epic("Web interface");
await allure.feature("Essential features");
await allure.story("Authentication");
// ...
});
To specify a test's location in the suite-based hierarchy:
TypeScriptimport { test } from "vitest";
test("Test Authentication", async () => {
await allure.parentSuite("Tests for web interface");
await allure.suite("Tests for essential features");
await allure.subSuite("Tests for authentication");
// ...
});
Divide a test into steps
To create steps and sub-steps, you can use the step()
function, see the reference.
TypeScriptimport { test } from "vitest";
test("Test Authentication", async () => {
await allure.step("Step 1", async () => {
await allure.step("Sub-step 1", async () => {
// ...
});
await allure.step("Sub-step 2", async () => {
// ...
});
});
await allure.step("Step 2", async () => {
// ...
});
});
Describe parametrized tests
Since tests in Vitest, unlike in some other frameworks, are written as anonymous functions, it is very easy to implement the parametrized tests pattern, i.e. to run the same test logic with different test data. To do so, just write the test inside a loop and use the variable parameters in both its title and its body.
To display a parameter value in the test report, pass it to the parameter()
function.
Attach screenshots and other files
In Allure reports, you have the ability to attach various types of files, which can greatly enhance the comprehensibility of the report. A common practice is to attach screenshots that capture the state of the user interface at specific moments during test execution.
For detailed instructions on how to implement attachments, refer to the attachments section in the Allure Vitest reference.
TypeScriptimport { test } from "vitest";
import * as fs from "fs";
test("Test Authentication", async () => {
await allure.attachment("Screenshot", fs.readFileSync("/path/to/image.png"), "image/png");
// ...
});
Select tests via a test plan file
If your project has "allure-vitest/setup"
included in the setupFiles
list (see Prepare your project), the test plan feature becomes enabled. This means that if the ALLURE_TESTPLAN_PATH
environment variable is defined and points to an existing file, Vitest will only run tests listed in this file.
Here's an example of running tests according to a file named testplan.json
(assuming you use the npm
package manager):
Environment information
For the main page of the report, you can collect various information about the environment in which the tests were executed.
For example, it is a good idea to use this to remember the OS version and Node.js version. This may help the future reader investigate bugs that are reproducible only in some environments.
To provide environment information, put a file named environment.properties
into the allure-results
directory after running the tests. See the example in Environment file.
Note that this feature should be used for properties that do not change for all tests in the report. If you have properties that can be different for different tests, consider using Parametrized tests.