feat(vitest): Support projectId in wrapVitest config#1993
Merged
Conversation
## 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 () => {
/* ... */
});
});
Caitlin Pinn (cpinn)
approved these changes
May 12, 2026
Contributor
Caitlin Pinn (cpinn)
left a comment
There was a problem hiding this comment.
Thank you for making the change.
Member
Abhijeet Prasad (AbhiPrasad)
left a comment
There was a problem hiding this comment.
needs a changeset and we can merge this in!
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an optional
projectIdfield to thewrapVitestconfiguration, letting users target a specific Braintrust project by its stable ID instead of by name. WhenprojectIdis set, it takes precedence overprojectName.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 acceptprojectId.Changes
js/src/wrappers/vitest/types.ts— AddprojectId?: stringtoWrapperConfigwith a docstring noting precedence overprojectName.js/src/wrappers/vitest/wrapper.ts— InwrapDescribe's lazygetOrCreateContext, branch onconfig.projectId:initExperiment({ projectId, experiment }).initExperiment(projectName ?? suiteName, { experiment })call (no behavior change for existing users).js/src/wrappers/vitest/README.md— Document the newprojectIdparameter onwrapVitest.js/src/wrappers/vitest/vitest-wrapper.test.ts:initExperimentmock to handle both overloads (positionalprojectNameand options object).configureNode()at module load so AsyncLocalStorage actually propagates context — required for the new tests' synchronous fakedescribefactories to exercise experiment creation.Project selectiondescribe block with four tests:projectIdis forwarded toinitExperiment.projectIdtakes precedence when bothprojectIdandprojectNameare set.projectName-only path is unchanged.Usage