JavaScript collectors

To use Buildkite Test Engine with your JavaScript (npm) projects, use the :github: test-collector-javascript package with a supported test framework. Buildkite Test Engine supports the following test frameworks:

Recommended setup

The recommended way to set up a new test suite is to add the Tests Buildkite plugin to the step that runs your tests. It works with every runner that bktec supports, and the entire setup is configuration-only. You can get a test suite running through changes to pipeline.yml alone, with no modifications to your application code.

Use the language-specific test collector documented on this page when you want deeper framework integration, such as custom execution tags, span annotations, or richer per-framework data. For a runner that bktec supports, you can use bktec to run and split tests while the collector uploads the results. To prevent duplicate test executions, set BUILDKITE_TEST_ENGINE_UPLOAD_RESULTS to false. If you use the Tests Buildkite plugin, set upload-results: false.

You can also upload test results by importing JSON or JUnit XML.

Tests Buildkite plugin example for Jest

The following step uses the Tests Buildkite plugin to run a Jest suite through bktec. The plugin downloads bktec, requests an OIDC token, ensures the test suite exists, and exports the environment variables that bktec expects, so the step's command only needs to invoke bktec run:

steps:
  - label: "Jest"
    command: bktec run
    plugins:
      - tests#v1.0.0:
          test-runner: jest
          result-path: jest-results.json
    parallelism: 4

See the Tests Buildkite plugin page for the full plugin reference, including all supported options, running tests inside Docker, and dynamic parallelism with bktec plan.

Add the test collector package

Whichever test framework you use, you first need to add and authenticate the buildkite-test-collector.

To add the test collector package:

  1. In your CI environment, set the BUILDKITE_ANALYTICS_TOKEN environment variable to your Test Engine API token. To learn how to set environment variables securely in Pipelines, see Managing pipeline secrets.

  2. On the command line, create a new branch by running:

    git checkout -b install-buildkite-test-engine
    
  3. Install buildkite-test-collector using your package manager.

    For npm, run:

    npm install --save-dev buildkite-test-collector
    

    For yarn, run:

    yarn add --dev buildkite-test-collector
    

Configure the test framework

With the test collector installed, you need to configure it in the test framework.

Jest

If you're already using Jest, you can add buildkite-test-collector/jest/reporter to the list of reporters to collect test results into your Test Engine dashboard.

To configure Jest:

  1. Make sure Jest runs with access to CI environment variables.
  2. Add "buildkite-test-collector/jest/reporter" to Jest's reporters configuration array (typically found in jest.config.js, jest.config.js, or package.json):

    {
        "reporters": ["default", "buildkite-test-collector/jest/reporter"],
        "testLocationInResults": true,
    }
    

    Note: The "testLocationInResults": true setting enables column and line capture for Test Engine.

Collecting Jest tests with OpenTelemetry

The JavaScript collector can send Jest results to Buildkite Test Engine as OpenTelemetry spans. Each test execution becomes a root span. Optional child spans show operations inside the test, such as HTTP requests and database queries.

Use Jest 29 or later with the default jest-circus runner. OpenTelemetry collection requires buildkite-test-collector version 1.13.0 or later. The setup below uses version 1.14.0. OpenTelemetry collection is disabled by default.

  1. Create a test suite if you do not have one. Configure OIDC authentication, or store your suite API token securely as BUILDKITE_ANALYTICS_TOKEN in the environment that runs Jest.

  2. Install the collector and its optional OpenTelemetry dependencies:

    npm install --save-dev buildkite-test-collector@1.14.0 \
      '@opentelemetry/api@~1.9.0' \
      '@opentelemetry/sdk-trace-base@^2.0.0' \
      '@opentelemetry/resources@^2.0.0' \
      '@opentelemetry/exporter-trace-otlp-proto@>=0.200.0 <0.300.0' \
      '@opentelemetry/context-async-hooks@^2.0.0'
    

    Use the OpenTelemetry JavaScript SDK 2.x dependencies shown here, not unversioned installs. SDK 3.x is not supported. These dependencies require Node.js 18.19 or later in the 18.x release line, or Node.js 20.6 or later.

  3. Configure both the reporter and the runtime in jest.config.js. Replace an existing Buildkite reporter entry instead of adding a second one:

    module.exports = {
      reporters: [
        'default',
        ['buildkite-test-collector/jest/reporter', { otelEnabled: true }],
      ],
      testEnvironment: 'node',
      runtime: 'buildkite-test-collector/jest/runtime',
      testLocationInResults: true,
    };
    

    Keep your existing testEnvironment setting, such as node, jsdom, or a custom environment. If you used an earlier beta, remove testEnvironment: 'buildkite-test-collector/jest/environment' and add the runtime shown above. No environment wrapper is needed.

  4. Run Jest with its token and CI environment variables available:

    npx jest
    
  5. Open the new run in your Buildkite Test Engine suite. For upload problems, check the collector's warnings and the suite's Uploads page.

See the collector's OpenTelemetry guide for more details.

Jasmine

To configure Jasmine:

  1. Follow the Jasmine docs to add the Buildkite reporter. For example:

    // SpecHelper.js
    var BuildkiteReporter = require("buildkite-test-collector/jasmine/reporter");
    var buildkiteReporter = new BuildkiteReporter();
    
    jasmine.getEnv().addReporter(buildkiteReporter);
    
  2. (Optional) To pass in the API token using a custom environment variable, use the following report options:

    // SpecHelper.js
    var buildkiteReporter = new BuildkiteReporter(undefined, {
        token: process.env.CUSTOM_ENV_VAR,
    });
    

Mocha

To configure Mocha:

  1. Install the mocha-multi-reporters in your project by running:

    npm install mocha-multi-reporters --save-dev
    
  2. Configure it to run your desired reporter and the Buildkite reporter:

    // config.json
    {
      "reporterEnabled": "spec, buildkite-test-collector/mocha/reporter"
    }
    
  3. Update your test script to use the Buildkite reporter using mocha-multi-reporters:

    // package.json
    "scripts": {
      "test": "mocha --reporter mocha-multi-reporters --reporter-options configFile=config.json"
    },
    
  4. (Optional) To pass in the API token using a custom environment variable, use the report options. Since the reporter options are passed in as a JSON file, we recommend you put the environment variable name as a string value in the config.json, which is retrieved using dotenv in the mocha reporter.

    // config.json
    {
      "reporterEnabled": "spec, buildkite-test-collector/mocha/reporter",
      "buildkiteTestCollectorMochaReporterReporterOptions": {
        "token_name": "CUSTOM_ENV_VAR_NAME"
      }
    }
    

Cypress

To configure Cypress:

  1. Make sure Cypress runs with access to CI environment variables.
  2. Update your Cypress configuration.

    // cypress.config.js
    
    // Send results to Test Engine
    reporter: "buildkite-test-collector/cypress/reporter",
    

    Note: To pass in the API token using a custom environment variable, add the reporterOptions option to your Cypress configuration:

    // cypress.config.js
    
    // Send results to Test Engine
    reporterOptions: {
      token_name: "CUSTOM_ENV_VAR_NAME"
    }
    

Playwright

If you're already using Playwright, you can add buildkite-test-collector/playwright/reporter to the list of reporters to collect test results into your Test Engine dashboard.

To configure Playwright:

  1. Make sure Playwright runs with access to CI environment variables.
  2. Add "buildkite-test-collector/playwright/reporter" to Playwright's reporter configuration array (typically found in playwright.config.js):

    // playwright.config.js
    {
      "reporter": [
        ["line"],
        ["buildkite-test-collector/playwright/reporter"]
      ]
    }
    

Collecting Playwright tests with OpenTelemetry

The JavaScript collector can send Playwright results to Buildkite Test Engine as OpenTelemetry spans. Each test attempt, including retries and skipped tests, becomes a root span. Playwright steps, hooks, fixtures, assertions, and actions become nested child spans.

OpenTelemetry collection requires buildkite-test-collector version 1.14.0 or later and is disabled by default.

  1. Create a test suite if you do not have one. Configure OIDC authentication, or store your suite API token securely as BUILDKITE_ANALYTICS_TOKEN in the environment that runs Playwright.

  2. Install the collector and its optional OpenTelemetry SDK 2.x dependencies:

    npm install --save-dev buildkite-test-collector@1.14.0 \
      '@opentelemetry/api@~1.9.0' \
      '@opentelemetry/sdk-trace-base@^2.0.0' \
      '@opentelemetry/context-async-hooks@^2.0.0' \
      '@opentelemetry/exporter-trace-otlp-proto@>=0.200.0 <0.300.0'
    
  3. Enable OpenTelemetry on the reporter in playwright.config.js. Replace an existing Buildkite reporter entry instead of adding a second one:

    module.exports = {
      reporter: [
        ['line'],
        ['buildkite-test-collector/playwright/reporter', { otelEnabled: true }],
      ],
    };
    

    No fixture changes or custom runtime are needed. Collection happens in the reporter and does not capture automatic HTTP or database spans, manual spans inside workers, or browser application traces.

  4. Run Playwright with its token and CI environment variables available:

    npx playwright test
    
  5. Open the new run in your Buildkite Test Engine suite. For upload problems, check the collector's warnings and the suite's Uploads page.

Configuration or upload failures do not fall back to JSON uploads. To return to JSON reporting, disable otelEnabled.

See the collector's OpenTelemetry guide for more details.

Vitest

If you are already using Vitest, you can add buildkite-test-collector/vitest/reporter to the list of reporters to collect test results in your Test Engine dashboard.

To configure Vitest:

Update your Vitest configuration:

// vitest.config.js OR vite.config.js OR vitest.workspace.js

test: {
  // Send results to Test Engine
   reporters: [
     'default',
     'buildkite-test-collector/vitest/reporter'
   ],
   // Enable column + line capture for Test Engine
   includeTaskLocation: true,
}

If you would like to pass in the API token using a custom environment variable, you can do so using the report options.

// vitest.config.js OR vite.config.js OR vitest.workspace.js
test: {
   // Send results to Test Engine
   reporters: [
     'default',
     [
       "buildkite-test-collector/vitest/reporter",
       { token: process.env.CUSTOM_ENV_VAR },
     ],
   ],
}

Collecting Vitest tests with OpenTelemetry

The JavaScript collector can send Vitest results to Buildkite Test Engine as OpenTelemetry spans. Each test becomes a root span, with hook phases, HTTP requests, and custom instrumentation recorded as child spans.

OpenTelemetry collection requires buildkite-test-collector version 1.14.0 or later and is disabled by default. Use Vitest 4.0.11 or later to collect child spans. Vitest 5 requires Node.js 22.12 or later.

  1. Create a test suite if you do not have one. Configure OIDC authentication, or store your suite API token securely as BUILDKITE_ANALYTICS_TOKEN in the environment that runs Vitest.

  2. Install the collector and its optional OpenTelemetry SDK 2.x dependencies:

    npm install --save-dev buildkite-test-collector@1.14.0 \
      '@opentelemetry/api@~1.9.0' \
      '@opentelemetry/sdk-trace-base@^2.0.0' \
      '@opentelemetry/context-async-hooks@^2.0.0' \
      '@opentelemetry/exporter-trace-otlp-proto@>=0.200.0 <0.300.0'
    
  3. Enable the reporter and native tracing in vitest.config.js. Replace an existing Buildkite reporter entry instead of adding a second one:

    import { createRequire } from 'node:module';
    import { defineConfig } from 'vitest/config';
    
    const require = createRequire(import.meta.url);
    
    export default defineConfig({
      test: {
        reporters: [
          'default',
          ['buildkite-test-collector/vitest/reporter', { otelEnabled: true }],
        ],
        includeTaskLocation: true,
        experimental: {
          openTelemetry: {
            enabled: true,
            sdkPath: require.resolve('buildkite-test-collector/vitest/otel.js'),
          },
        },
      },
    });
    

    require.resolve supplies the filesystem path Vitest needs, including with pnpm and Yarn. For a CommonJS configuration, omit createRequire and use require.resolve directly. For multiple projects, configure tracing in the root configuration. When using Yarn without a node_modules directory, declare the dependencies in the root workspace.

  4. To capture HTTP and fetch calls, install the supported instrumentation packages:

    npm install --save-dev \
      '@opentelemetry/instrumentation-http@>=0.200.0 <0.300.0' \
      '@opentelemetry/instrumentation-undici@>=0.11.0 <0.34.0'
    

    Vitest supports only the http and undici instrumentation packages. Declare these dependencies in the project's package.json or a parent workspace's package.json.

  5. Run Vitest with its token and CI environment variables available:

    npx vitest run
    

    Do not pass --reporter flags, which override the reporters in the configuration. Watch mode is not supported.

  6. Open the new run in your Buildkite Test Engine suite. For upload problems, check the collector's warnings and the suite's Uploads page.

Retries share one execution per test, with each attempt's phases and child spans underneath. Browser mode and Vitest 3 send test spans only. A custom SDK or missing native tracing configuration also limits collection to test spans. Vitest 1 and 2 do not upload results in OpenTelemetry mode. Configuration or upload failures do not fall back to JSON uploads. To return to JSON reporting, disable otelEnabled.

See the collector's OpenTelemetry guide for more details, including configuration for bktec.

Save the changes

When your collector is installed, commit and push your changes:

  1. Add the changes to the staging area by running:

    git add .
    
  2. Commit the changes by running:

    git commit -m "Install and set up Buildkite Test Engine"
    
  3. Push the changes by running:

    git push
    

View the results

After completing these steps, you'll see the analytics of test executions on all branches that include this code in the Test Engine dashboard.

If you don't see branch names, build numbers, or commit hashes in the Test Engine dashboard, see CI environments to learn more about exporting your environment.

Upload custom tags for test executions

You can group test executions using custom tags to compare metrics across different dimensions, such as:

  • Language versions
  • Cloud providers
  • Instance types
  • Team ownership
  • and more

Upload-level tags

Tags configured on the collector will be included in each upload batch, and will be applied server-side to every execution therein. This is an efficient way to tag every execution with values that don't vary within one configuration, for example, cloud environment details, language/framework versions. Upload-level tags may be overwritten by execution-level tags.

// Jest -- jest.config.js
reporters: [
  'default',
  'buildkite-test-collector/jest/reporter'
  ['buildkite-test-collector/jest/reporter', {
    tags: { hello: "jest" }
  }]
],

// Cypress -- cypress.config.js
reporterOptions: {
  tags: { "hello": "cypress" },
},

// Mocha -- config.js
"buildkiteTestCollectorMochaReporterReporterOptions": {
  "tags": {
    "hello": "mocha"
  }
}

// Playwright -- playwright.config.js
reporter: [
  ['line'],
  ['buildkite-test-collector/playwright/reporter', {
    tags: { "hello": "playwright" }
  }]
],

Troubleshooting missing test executions and --forceExit

Using the --forceExit option when running Jest could result in missing test executions from Test Engine.

--forceExit could potentially terminate any ongoing processes that are attempting to send test executions to Buildkite.

We recommend using --detectOpenHandles to track down open handles which are preventing Jest from exiting cleanly.