Skip to content

feat(vitest): Support projectId in wrapVitest config#1993

Merged
Luca Forstner (lforst) merged 2 commits into
mainfrom
05-12-add-wrapvitest-project-id
May 13, 2026
Merged

feat(vitest): Support projectId in wrapVitest config#1993
Luca Forstner (lforst) merged 2 commits into
mainfrom
05-12-add-wrapvitest-project-id

Conversation

@evanmkeith
Copy link
Copy Markdown
Contributor

Summary

Adds an optional projectId field to the wrapVitest configuration, letting users target a specific Braintrust project by its stable ID instead of by name. When projectId is set, it takes precedence over projectName.

Motivation

Project names can be renamed or collide across orgs. Users who already know the project ID (e.g. from CI config or another integration) had no way to bind Vitest experiments to that exact project. This brings the wrapper in line with init / initExperiment, which both accept projectId.

Changes

  • js/src/wrappers/vitest/types.ts — Add projectId?: string to WrapperConfig with a docstring noting precedence over projectName.
  • js/src/wrappers/vitest/wrapper.ts — In wrapDescribe's lazy getOrCreateContext, branch on config.projectId:
    • If set, call initExperiment({ projectId, experiment }).
    • Otherwise, preserve the existing initExperiment(projectName ?? suiteName, { experiment }) call (no behavior change for existing users).
  • js/src/wrappers/vitest/README.md — Document the new projectId parameter on wrapVitest.
  • js/src/wrappers/vitest/vitest-wrapper.test.ts:
    • Update the initExperiment mock to handle both overloads (positional projectName and options object).
    • Add configureNode() at module load so AsyncLocalStorage actually propagates context — required for the new tests' synchronous fake describe factories to exercise experiment creation.
    • Add a Project selection describe block with four tests:
      1. projectId is forwarded to initExperiment.
      2. projectId takes precedence when both projectId and projectName are set.
      3. projectName-only path is unchanged.
      4. Suite-name fallback (no project config) is unchanged.

Usage

import * as vitest from "vitest";
import { wrapVitest } from "braintrust";

const bt = wrapVitest(vitest, {
  projectId: "abc-123-stable-project-id",
});

bt.describe("my suite", () => {
  bt.test("logs to the project by id", async () => {
    /* ... */
  });
});

## Summary

Adds an optional `projectId` field to the `wrapVitest` configuration, letting users target a specific Braintrust project by its stable ID instead of by name. When `projectId` is set, it takes precedence over `projectName`.

## Motivation

Project names can be renamed or collide across orgs. Users who already know the project ID (e.g. from CI config or another integration) had no way to bind Vitest experiments to that exact project. This brings the wrapper in line with `init` / `initExperiment`, which both accept `projectId`.

## Changes

- `js/src/wrappers/vitest/types.ts` — Add `projectId?: string` to `WrapperConfig` with a docstring noting precedence over `projectName`.
- `js/src/wrappers/vitest/wrapper.ts` — In `wrapDescribe`'s lazy `getOrCreateContext`, branch on `config.projectId`:
  - If set, call `initExperiment({ projectId, experiment })`.
  - Otherwise, preserve the existing `initExperiment(projectName ?? suiteName, { experiment })` call (no behavior change for existing users).
- `js/src/wrappers/vitest/README.md` — Document the new `projectId` parameter on `wrapVitest`.
- `js/src/wrappers/vitest/vitest-wrapper.test.ts`:
  - Update the `initExperiment` mock to handle both overloads (positional `projectName` and options object).
  - Add `configureNode()` at module load so AsyncLocalStorage actually propagates context — required for the new tests' synchronous fake `describe` factories to exercise experiment creation.
  - Add a `Project selection` describe block with four tests:
    1. `projectId` is forwarded to `initExperiment`.
    2. `projectId` takes precedence when both `projectId` and `projectName` are set.
    3. `projectName`-only path is unchanged.
    4. Suite-name fallback (no project config) is unchanged.

## Usage

```ts
import * as vitest from "vitest";
import { wrapVitest } from "braintrust";

const bt = wrapVitest(vitest, {
  projectId: "abc-123-stable-project-id",
});

bt.describe("my suite", () => {
  bt.test("logs to the project by id", async () => {
    /* ... */
  });
});
Copy link
Copy Markdown
Contributor

@cpinn Caitlin Pinn (cpinn) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making the change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a changeset and we can merge this in!

@lforst Luca Forstner (lforst) merged commit 6c5ec1f into main May 13, 2026
31 of 32 checks passed
@lforst Luca Forstner (lforst) deleted the 05-12-add-wrapvitest-project-id branch May 13, 2026 08:44
@AbhiPrasad
Copy link
Copy Markdown
Member

released with https://github.com/braintrustdata/braintrust-sdk-javascript/releases/tag/braintrust%403.11.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants