Vitest test reporting

Using the Vitest test framework? Follow the instructions below to report results. If you are not using Vitest please see the Node.js docs for integrating with a lower-level library.

Installation

npm install vitest-tesults-reporter --save

Configuration

Add the reporter to your Vitest configuration in vite.config.js (or vitest.config.js). See https://vitest.dev/config/ for details about Vitest configuration:

import TesultsReporter from 'vitest-tesults-reporter'

export default defineConfig({
  test: {
    globals: true,
    reporters: [
      'default',
      new TesultsReporter({
        'tesults-target': process.env.TESULTS_TARGET || 'token',
        // Optional:
        // 'tesults-files': './tesults-files',
        // 'tesults-build-name': '1.0.0',
        // 'tesults-build-desc': 'Build description',
        // 'tesults-build-result': 'pass',
        // 'tesults-build-reason': 'Build failure reason',
      })
    ]
  }
})

You can now run Vitest as usual with the npx vitest run command (or npm test if configured in package.json).

The 'token' above should be replaced with your Tesults target token, which you received when creating a project or target, and can be regenerated from the configuration menu.

The reporter treats the describe as a test suite and each test (or it) as a test case.

describe('SuiteA', () => {
  it("Test1", () => {
    expect(sum(1, 2)).toBe(3);
  });

  it("Test2", () => {
    expect(sum(2, 2)).toBe(4);
  });
});

describe('SuiteB', () => {
  it("Test3", () => {
    expect(sum(3, 3)).toBe(6);
  });
});

We recommend using 'describe' so that test cases can be appropriately grouped by test suite on Tesults.

tesults-targetRequired

Required to upload to Tesults, if this arg is not provided the reporter does not attempt upload, effectively disabling it. Get your target token from the configuration menu in the Tesults web interface.

{ "tesults-target": "eyJ0eXAiOiJ..." }

Basic configuration complete

At this point the vitest-tesults-reporter will push results to Tesults when you run your tests. The tesults-target option must be supplied to indicate which target to use. See above for complete details.

Enhanced reporting

Use the Tesults reporter to report additional properties for each test case. To begin doing this, import the enhanced reporting functions in your test file:

import { description, step, custom, file } from 'vitest-tesults-reporter'

Important: Enhanced reporting functions require globals: true in your Vitest configuration. This allows the reporter to automatically detect the current test context.

Report custom fields, test steps, descriptions and attach files to test cases using the reporter functions. Call reporter functions from within your tests (it/test block):

description

Add a description of the test case for reporting purposes.

description("some description here")

custom

Add a custom field and value to the test case for reporting purposes.

custom("Some custom field", "Some custom value")

file

Associate a file to the test case in order to upload it for reporting.

file("/absolute/path/to/file/screenshot.png")

Caution: If uploading files the time taken to upload is entirely dependent on your network speed. Typical office upload speeds of 100 - 1000 Mbps should allow upload of even hundreds of files quite quickly, just a few seconds, but if you have slower access it may take hours. We recommend uploading a reasonable number of files for each test case. The upload method blocks at the end of a test run while uploading test results and files. When starting out test without files first to ensure everything is setup correctly.

step

Add test steps to the test case for reporting. Each step is an object with a name and result (one of [pass, fail, unknown]). You can also add the optional fields desc (description) and reason (failure reason in case of step fail).

step({
  name: "First step",
  result: "pass"
})
step({
  name: "Second step",
  desc: "Second step description",
  result: "fail"
})

Files generated by tests

This method of uploading files is no longer recommended. Utilize the file method described above to simplify uploading files from tests.

tesults-filesOptional

Provide the top-level directory where files generated during testing are saved for the running test run. Files, including logs, screen captures and other artifacts will be automatically uploaded.

{ "tesults-target": "eyJ0eXAiOiJ...", "tesults-files": "/Users/admin/Desktop/temporary" }

This is one area where the vitest-tesults-reporter is opinionated and requires that files generated during a test run be saved locally temporarily within a specific directory structure.

Store all files in a temporary directory as your tests run. After Tesults upload is complete, delete the temporary directory or just have it overwritten on the next test run.

Within this temporary directory create subdirectories matching the test suite (describe) and test case (test/it) name.

Be aware that if providing build files, the build suite is always set to [build] and files are expected to be located in temporary/[build]/buildname

Caution: If uploading files the time taken to upload is entirely dependent on your network speed. Typical office upload speeds of 100 - 1000 Mbps should allow upload of even hundreds of files quite quickly, just a few seconds, but if you have slower access it may take hours. We recommend uploading a reasonable number of files for each test case. The upload method blocks at the end of a test run while uploading test results and files. When starting out test without files first to ensure everything is setup correctly.

During a test run, save test generated files such as logs and screenshots to a local temporary directory. At the end of the test run all files will automatically be saved to Tesults as long as you save files in the directory structure below. Omit test suite folders if not using test suites.
  • expanded temporary folder
    • expanded Test Suite A
      • expanded Test 1
        • test.log
        • screenshot.png
      • expanded Test 2
        • test.log
        • screenshot.png
    • expanded Test Suite B
      • expanded Test 3
        • metrics.csv
        • error.log
      • expanded Test 4
        • test.log

Build

tesults-build-nameOptional

Use this to report a build version or name for reporting purposes.

{ "tesults-build-name": "1.0.0" }

tesults-build-resultOptional

Use this to report the build result, must be one of [pass, fail, unknown].

{ "tesults-build-result": "pass" }

tesults-build-descOptional

Use this to report a build description for reporting purposes.

{ "tesults-build-desc": "added new feature" }

tesults-build-reasonOptional

Use this to report a build failure reason.

{ "tesults-build-reason": "build error line 201 somefile.js" }

A complete example:

import { describe, it, expect } from 'vitest'
import { description, step, custom, file } from 'vitest-tesults-reporter'

describe('Suite A', () => {
  it('Test 1', () => {
    description("description here")

    step({name: "step 1", desc: "step 1 description", result: "pass"})
    step({name: "step 2", desc: "step 2 description", result: "pass"})

    custom("custom field 1", "custom value 1")
    custom("priority", "high")

    file("/full/path/logs/log1.txt")
    file("/full/path/screenshots/screenshot.png")

    expect(sum(1, 2)).toBe(3)
  });
});

Result Interpretation

Result interpretation is supported by Vitest. Install version 1.0.0 or newer. If you use result interpretation we recommend you add these minimum mapping values:

pass
pass
fail
fail

Go to the Configuration menu.

Configuration menu

Select Build Consolidation.

When executing multiple test runs in parallel or serially for the same build or release, results are submitted to Tesults separately and multiple test runs are generated on Tesults. This is because the default behavior on Tesults is to treat each results submission as a separate test run.

This behavior can be changed from the configuration menu.

Build Consolidation

Click 'Build Consolidation' from the Configuration menu to enable and disable consolidation for a project or by target.

When build consolidation is enabled multiple test runs submitted at different times, with the same build name, will be consolidated into a single test run by Tesults automatically.

This is useful for test frameworks that run batches of test cases in parallel. If you do not have a build name to use for consolidation, consider using a timestamp set at the time the test run starts.

Build Replacement

When build consolidation is enabled, an additional option, build replacement can optionally be enabled too. Just as with build consolidation, when multiple test runs are submitted with the same build name the results are consolidated, but with replacement enabled, if there are test cases with the same suite and name received multiple times, the last received test case replaces an existing test case with the same suite and name. This may be useful to enable in situations where test cases are re-run frequently and you do not want new test cases to be appended and instead want them to replace older test cases. This option is generally best left disabled, unless test cases are often re-run for the same build and you are only interested in the latest result for the run.

Dynamically created test cases

If you dynamically create test cases, such as test cases with variable values, we recommend that the test suite and test case names themselves be static. Provide the variable data information in the test case description or other custom fields but try to keep the test suite and test name static. If you change your test suite or test name on every test run you will not benefit from a range of features Tesults has to offer including test case failure assignment and historical results analysis. You need not make your tests any less dynamic, variable values can still be reported within test case details.

Proxy servers

Does your corporate/office network run behind a proxy server? Contact us and we will supply you with a custom API Library for this case. Without this results will fail to upload to Tesults.

Have questions or need help? Contact us