@mcpjam/vitest 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -9
- package/dist/index.d.ts +22 -20
- package/dist/index.js +85 -18
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ const corpus = loadCorpusFromLock(lock);
|
|
|
25
25
|
|
|
26
26
|
describeEvalSuite("refund flows", corpus.toEvalSuite(), {
|
|
27
27
|
factory: () => buildExecutor(),
|
|
28
|
-
run: { iterations: 25 },
|
|
28
|
+
run: { iterations: 25, runTimeoutMs: 240_000, mcpjam: { strict: true } },
|
|
29
29
|
gate: {
|
|
30
30
|
minimumPassRate: 0.9,
|
|
31
31
|
maximumP95LatencyMs: 30_000,
|
|
@@ -34,6 +34,8 @@ describeEvalSuite("refund flows", corpus.toEvalSuite(), {
|
|
|
34
34
|
});
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
+
Set `MCPJAM_API_KEY` in CI. Strict reporting fails the test when evidence cannot be persisted, while keeping the completed local measurements available through `getResults()` and `getReportingReceipt()`. For an intentional local-only run, set `mcpjam: { enabled: false }`.
|
|
38
|
+
|
|
37
39
|
`vitest run` then reports one test per hosted case, titled with its dashboard
|
|
38
40
|
id:
|
|
39
41
|
|
|
@@ -57,16 +59,19 @@ given — a final `it` for the policy.
|
|
|
57
59
|
| --- | --- |
|
|
58
60
|
| `executor` | A ready `HostExecutor`. |
|
|
59
61
|
| `factory` | Builds one inside `beforeAll`, for an executor that must connect first. Mutually exclusive with `executor`. |
|
|
60
|
-
| `run` | `
|
|
62
|
+
| `run` | SDK execution controls: `iterations` (or suite `defaults.iterations`), `concurrency`, `timeoutMs`, `runTimeoutMs`, `evaluatorTimeoutMs`, `maxCapturedBytes`, `signal`, `mcpjam`, … |
|
|
61
63
|
| `gate` | A `GatePolicy`. Omit to register no gate test. |
|
|
62
|
-
| `hookTimeoutMs` |
|
|
64
|
+
| `hookTimeoutMs` | Vitest hook timeout. Default 300000. Configure SDK execution bounds separately. |
|
|
65
|
+
| `dispose` | Optional async cleanup for the executor, invoked in `afterAll` even after failure. |
|
|
66
|
+
| `summary` | `table` by default, or `none`. Reports measurements and persistence separately. |
|
|
67
|
+
| `only` / `skip` | Suite case IDs to focus/skip. Skip wins; unknown IDs are rejected. |
|
|
63
68
|
|
|
64
69
|
### `testEval(test, options)`
|
|
65
70
|
|
|
66
71
|
The single-test seat, for a file that owns one eval and wants no suite.
|
|
67
72
|
|
|
68
73
|
```ts
|
|
69
|
-
import { EvalTest } from "@mcpjam/sdk";
|
|
74
|
+
import { EvalTest, assertion } from "@mcpjam/sdk";
|
|
70
75
|
import { mintCaseId } from "@mcpjam/sdk/contract";
|
|
71
76
|
import { testEval } from "@mcpjam/vitest";
|
|
72
77
|
|
|
@@ -79,15 +84,27 @@ testEval(
|
|
|
79
84
|
new EvalTest({
|
|
80
85
|
id: "c_V1StGXR8Z5jdHi6Bmy",
|
|
81
86
|
name: "refunds a duplicate charge",
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
execute: async (executor) => {
|
|
88
|
+
await executor.run("refund the duplicate charge");
|
|
89
|
+
},
|
|
90
|
+
evaluators: {
|
|
91
|
+
mode: "extend",
|
|
92
|
+
list: [assertion({ type: "toolCalledAtLeastOnce", toolName: "create_refund" })],
|
|
85
93
|
},
|
|
86
94
|
}),
|
|
87
|
-
{ factory: () => buildExecutor(), run: { iterations: 25 } }
|
|
95
|
+
{ factory: () => buildExecutor(), run: { iterations: 25, mcpjam: { strict: true } } }
|
|
88
96
|
);
|
|
89
97
|
```
|
|
90
98
|
|
|
99
|
+
The nested case test is named `passes`; its outer title retains the case's name and hosted ID.
|
|
100
|
+
Both facades expose `.skip(...)` and `.only(...)`. Skipped registrations never construct an executor.
|
|
101
|
+
Focused registrations use Vitest's native focus behavior and are rejected when CI disables `allowOnly`.
|
|
102
|
+
|
|
103
|
+
Suite filtering preserves the original case inventory. A gate over a subset is incomplete by default;
|
|
104
|
+
use `gate.selectionScope: "selected"` only when intentionally certifying that selection. Hosted subset
|
|
105
|
+
reporting is currently refused until persisted selection support is available; use `mcpjam.enabled: false`
|
|
106
|
+
for local filtered runs. Skipping everything produces no execution and no passing acceptance claim.
|
|
107
|
+
|
|
91
108
|
### `planEvalSuite(suite, options)`
|
|
92
109
|
|
|
93
110
|
Pure. Returns the titles that *would* be registered — useful for asserting your
|
|
@@ -107,7 +124,7 @@ not an optimization: the suite uploads a single hosted run, computes one
|
|
|
107
124
|
aggregate evaluation-config hash, and executes cases sequentially. Calling it
|
|
108
125
|
per test would produce N hosted runs and N aggregate hashes. Each `it` is an
|
|
109
126
|
assertion over that one already-computed result, so per-test timeouts are
|
|
110
|
-
irrelevant
|
|
127
|
+
irrelevant to execution. Set `hookTimeoutMs` above the SDK run deadline. The SDK uses separate iteration, evaluator, and run bounds; its abort signal is cooperative for custom external side effects. `maxCapturedBytes` defaults to 16 MiB per iteration and bounds retained SDK evidence. Exceeding it returns unavailable capture with evaluator error rows and drops the oversized transcript from the result. It does not cap allocations inside your executor or custom code.
|
|
111
128
|
|
|
112
129
|
**A wrapper, not a reporter.** A vitest Reporter observes tests; it cannot
|
|
113
130
|
decide what a test *is*. Evals need the opposite, so failures land on named
|
package/dist/index.d.ts
CHANGED
|
@@ -94,29 +94,31 @@ type EvalSuiteVitestOptions = {
|
|
|
94
94
|
executor?: HostExecutor;
|
|
95
95
|
/** Built inside `beforeAll` — for an executor that must connect first. */
|
|
96
96
|
factory?: () => HostExecutor | Promise<HostExecutor>;
|
|
97
|
-
run: EvalTestRunOptions
|
|
97
|
+
run: Omit<EvalTestRunOptions, "iterations"> & {
|
|
98
|
+
iterations?: number;
|
|
99
|
+
};
|
|
100
|
+
/** Selected case identities. Selection retains the source coverage manifest. */
|
|
101
|
+
only?: readonly string[];
|
|
102
|
+
/** Skips win over only; no skipped case executes. */
|
|
103
|
+
skip?: readonly string[];
|
|
104
|
+
/** Explicit cleanup, run even when execution or reporting fails. */
|
|
105
|
+
dispose?: (executor: HostExecutor) => void | Promise<void>;
|
|
106
|
+
/** Defaults to table. Formatting errors never replace evaluation results. */
|
|
107
|
+
summary?: "none" | "table";
|
|
98
108
|
/** Omit to register no gate test. */
|
|
99
109
|
gate?: GatePolicy;
|
|
100
110
|
hookTimeoutMs?: number;
|
|
101
111
|
};
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
* reported for a case that is not part of any suite.
|
|
114
|
-
*
|
|
115
|
-
* `gate` is honoured here exactly as in `describeEvalSuite` — via
|
|
116
|
-
* `gateInputFromRunResult` rather than the suite variant. Accepting the option
|
|
117
|
-
* and ignoring it would be the worst outcome available: a policy that reports
|
|
118
|
-
* green because nothing evaluated it.
|
|
119
|
-
*/
|
|
120
|
-
declare function testEval(test: EvalTest, options: EvalTestVitestOptions): void;
|
|
112
|
+
declare const describeEvalSuite: ((name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) => void) & {
|
|
113
|
+
skip: (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) => void;
|
|
114
|
+
only: (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) => void;
|
|
115
|
+
};
|
|
116
|
+
type EvalTestVitestOptions = Omit<EvalSuiteVitestOptions, "run" | "only" | "skip"> & {
|
|
117
|
+
run: EvalTestRunOptions;
|
|
118
|
+
};
|
|
119
|
+
declare const testEval: ((test: EvalTest, options: EvalTestVitestOptions) => void) & {
|
|
120
|
+
skip: (test: EvalTest, options: EvalTestVitestOptions) => void;
|
|
121
|
+
only: (test: EvalTest, options: EvalTestVitestOptions) => void;
|
|
122
|
+
};
|
|
121
123
|
|
|
122
124
|
export { DEFAULT_HOOK_TIMEOUT_MS, type EvalCasePlan, type EvalSuitePlan, type EvalSuiteVitestOptions, type EvalTestVitestOptions, GATE_TEST_TITLE, describeEvalSuite, gateFailureMessage, planEvalSuite, runAndAssertCase, testEval };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { describe, beforeAll, it } from 'vitest';
|
|
2
|
-
import {
|
|
1
|
+
import { describe, beforeAll, afterAll, it } from 'vitest';
|
|
2
|
+
import { formatRunSummaryTable, assertGate, gateInputFromSuiteResult, gateInputFromRunResult, formatGateReport } from '@mcpjam/sdk';
|
|
3
3
|
|
|
4
4
|
// src/index.ts
|
|
5
5
|
var DEFAULT_HOOK_TIMEOUT_MS = 3e5;
|
|
@@ -33,6 +33,8 @@ function runAndAssertCase(run, testName, failureReport) {
|
|
|
33
33
|
`No result was recorded for eval case "${testName}". The suite ran, but this case was not part of it.`
|
|
34
34
|
);
|
|
35
35
|
}
|
|
36
|
+
if (run.iterations === 0)
|
|
37
|
+
throw new Error(`No iterations executed for eval case "${testName}".`);
|
|
36
38
|
if (run.failures === 0) return;
|
|
37
39
|
let detail = "";
|
|
38
40
|
try {
|
|
@@ -64,19 +66,53 @@ async function resolveExecutor(options) {
|
|
|
64
66
|
"describeEvalSuite needs an `executor` or a `factory` to build one."
|
|
65
67
|
);
|
|
66
68
|
}
|
|
67
|
-
function
|
|
69
|
+
function registerEvalSuite(name, suite, options, mode = "normal") {
|
|
68
70
|
const plan = planEvalSuite(suite, options);
|
|
71
|
+
const available = new Set(suite.getAll().map((test) => test.getId()));
|
|
72
|
+
for (const id of [...options.only ?? [], ...options.skip ?? []]) {
|
|
73
|
+
if (!available.has(id)) throw new Error(`Unknown selected case ID: ${id}`);
|
|
74
|
+
}
|
|
75
|
+
const skipped = new Set(options.skip ?? []);
|
|
76
|
+
const selectedIds = [...available].filter(
|
|
77
|
+
(id) => !skipped.has(id) && (!options.only || options.only.includes(id))
|
|
78
|
+
);
|
|
79
|
+
const selectedSuite = options.only || options.skip ? suite.subset(selectedIds) : suite;
|
|
69
80
|
const testsByName = new Map(
|
|
70
|
-
|
|
81
|
+
selectedSuite.getAll().map((test) => [test.getName(), test])
|
|
71
82
|
);
|
|
72
|
-
describe
|
|
83
|
+
const register = mode === "skip" ? describe.skip : mode === "only" || options.only ? describe.only : describe;
|
|
84
|
+
register(name, () => {
|
|
85
|
+
let executor;
|
|
73
86
|
let result;
|
|
74
87
|
beforeAll(async () => {
|
|
75
|
-
|
|
76
|
-
|
|
88
|
+
if (selectedIds.length === 0) return;
|
|
89
|
+
executor = await resolveExecutor(options);
|
|
90
|
+
try {
|
|
91
|
+
result = await selectedSuite.run(executor, {
|
|
92
|
+
...options.run,
|
|
93
|
+
summary: "none"
|
|
94
|
+
});
|
|
95
|
+
} finally {
|
|
96
|
+
const completed = result ?? selectedSuite.getResults();
|
|
97
|
+
if (completed && options.summary !== "none") {
|
|
98
|
+
try {
|
|
99
|
+
console.log(
|
|
100
|
+
formatRunSummaryTable(
|
|
101
|
+
completed,
|
|
102
|
+
selectedSuite.getReportingReceipt()
|
|
103
|
+
)
|
|
104
|
+
);
|
|
105
|
+
} catch {
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
77
109
|
}, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);
|
|
110
|
+
afterAll(async () => {
|
|
111
|
+
if (executor) await options.dispose?.(executor);
|
|
112
|
+
});
|
|
78
113
|
for (const entry of plan.cases) {
|
|
79
|
-
|
|
114
|
+
const registerCase = entry.caseId && !selectedIds.includes(entry.caseId) ? it.skip : it;
|
|
115
|
+
registerCase(entry.title, () => {
|
|
80
116
|
const test = testsByName.get(entry.testName);
|
|
81
117
|
runAndAssertCase(
|
|
82
118
|
result?.tests.get(entry.testName),
|
|
@@ -91,7 +127,10 @@ function describeEvalSuite(name, suite, options) {
|
|
|
91
127
|
throw new Error("The eval suite did not run, so it cannot be gated.");
|
|
92
128
|
}
|
|
93
129
|
try {
|
|
94
|
-
assertGate(
|
|
130
|
+
assertGate(
|
|
131
|
+
gateInputFromSuiteResult(result),
|
|
132
|
+
options.gate
|
|
133
|
+
);
|
|
95
134
|
} catch (error) {
|
|
96
135
|
throw new Error(gateFailureMessage(error));
|
|
97
136
|
}
|
|
@@ -99,18 +138,42 @@ function describeEvalSuite(name, suite, options) {
|
|
|
99
138
|
}
|
|
100
139
|
});
|
|
101
140
|
}
|
|
102
|
-
|
|
141
|
+
var describeEvalSuite = Object.assign(
|
|
142
|
+
(name, suite, options) => registerEvalSuite(name, suite, options),
|
|
143
|
+
{
|
|
144
|
+
skip: (name, suite, options) => registerEvalSuite(name, suite, options, "skip"),
|
|
145
|
+
only: (name, suite, options) => registerEvalSuite(name, suite, options, "only")
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
function registerTestEval(test, options, mode = "normal") {
|
|
103
149
|
const scenarioId = test.getConfig().externalCaseId;
|
|
104
150
|
const name = test.getName();
|
|
105
151
|
const suffix = scenarioId === void 0 ? "" : ` [${scenarioId}]`;
|
|
106
152
|
const title = name.endsWith(suffix) ? name : `${name}${suffix}`;
|
|
107
|
-
describe
|
|
153
|
+
const register = mode === "skip" ? describe.skip : mode === "only" ? describe.only : describe;
|
|
154
|
+
register(title, () => {
|
|
155
|
+
let executor;
|
|
108
156
|
let run;
|
|
109
157
|
beforeAll(async () => {
|
|
110
|
-
|
|
111
|
-
|
|
158
|
+
executor = await resolveExecutor(options);
|
|
159
|
+
try {
|
|
160
|
+
run = await test.run(executor, { ...options.run, summary: "none" });
|
|
161
|
+
} finally {
|
|
162
|
+
const completed = run ?? test.getResults();
|
|
163
|
+
if (completed && options.summary !== "none") {
|
|
164
|
+
try {
|
|
165
|
+
console.log(
|
|
166
|
+
formatRunSummaryTable(completed, test.getReportingReceipt())
|
|
167
|
+
);
|
|
168
|
+
} catch {
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
112
172
|
}, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);
|
|
113
|
-
|
|
173
|
+
afterAll(async () => {
|
|
174
|
+
if (executor) await options.dispose?.(executor);
|
|
175
|
+
});
|
|
176
|
+
it("passes", () => {
|
|
114
177
|
runAndAssertCase(run, name, () => test.getFailureReport());
|
|
115
178
|
});
|
|
116
179
|
if (options.gate) {
|
|
@@ -119,10 +182,7 @@ function testEval(test, options) {
|
|
|
119
182
|
throw new Error("The eval did not run, so it cannot be gated.");
|
|
120
183
|
}
|
|
121
184
|
try {
|
|
122
|
-
assertGate(
|
|
123
|
-
gateInputFromRunResult(run),
|
|
124
|
-
options.gate
|
|
125
|
-
);
|
|
185
|
+
assertGate(gateInputFromRunResult(run), options.gate);
|
|
126
186
|
} catch (error) {
|
|
127
187
|
throw new Error(gateFailureMessage(error));
|
|
128
188
|
}
|
|
@@ -130,6 +190,13 @@ function testEval(test, options) {
|
|
|
130
190
|
}
|
|
131
191
|
});
|
|
132
192
|
}
|
|
193
|
+
var testEval = Object.assign(
|
|
194
|
+
(test, options) => registerTestEval(test, options),
|
|
195
|
+
{
|
|
196
|
+
skip: (test, options) => registerTestEval(test, options, "skip"),
|
|
197
|
+
only: (test, options) => registerTestEval(test, options, "only")
|
|
198
|
+
}
|
|
199
|
+
);
|
|
133
200
|
|
|
134
201
|
export { DEFAULT_HOOK_TIMEOUT_MS, GATE_TEST_TITLE, describeEvalSuite, gateFailureMessage, planEvalSuite, runAndAssertCase, testEval };
|
|
135
202
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAmDO,IAAM,uBAAA,GAA0B;AAGhC,IAAM,eAAA,GAAkB;AAkCxB,SAAS,aAAA,CACd,KAAA,EACA,OAAA,GAAiC,EAAC,EACnB;AACf,EAAA,MAAM,QAAQ,KAAA,CAAM,MAAA,EAAO,CAAE,GAAA,CAAI,CAAC,IAAA,KAAuB;AACvD,IAAA,MAAM,QAAA,GAAW,KAAK,OAAA,EAAQ;AAC9B,IAAA,MA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IAAA,GAAO,KAAK,OAAA,EAAQ;AAC1B,EAAA,MAAM,MAAA,GAAS,UAAA,KAAe,MAAA,GAAY,EAAA,GAAK,KAAK,UAAU,CAAA,CAAA,CAAA;AAC9D,EAAA,MAAM,KAAA,GAAQ,KAAK,QAAA,CAAS,MAAM,IAAI,IAAA,GAAO,CAAA,EAAG,IAAI,CAAA,EAAG,MAAM,CAAA,CAAA;AAE7D,EAAA,QAAA,CAAS,OAAO,MAAM;AACpB,IAAA,IAAI,GAAA;AAEJ,IAAA,SAAA,CAAU,YAAY;AACpB,MAAA,MAAM,QAAA,GAAW,MAAM,eAAA,CAAgB,OAAO,CAAA;AAC9C,MAAA,GAAA,GAAM,MAAM,IAAA,CAAK,GAAA,CAAI,QAAA,EAAU,QAAQ,GAAG,CAAA;AAAA,IAC5C,CAAA,EAAG,OAAA,CAAQ,aAAA,IAAiB,uBAAuB,CAAA;AAEnD,IAAA,EAAA,CAAG,OAAO,MAAM;AACd,MAAA,gBAAA,CAAiB,GAAA,EAAK,IAAA,EAAM,MAAM,IAAA,CAAK,kBAAkB,CAAA;AAAA,IAC3D,CAAC,CAAA;AAED,IAAA,IAAI,QAAQ,IAAA,EAAM;AAChB,MAAA,EAAA,CAAG,iBAAiB,MAAM;AACxB,QAAA,IAAI,CAAC,GAAA,EAAK;AACR,UAAA,MAAM,IAAI,MAAM,8CAA8C,CAAA;AAAA,QAChE;AACA,QAAA,IAAI;AACF,UAAA,UAAA;AAAA,YACE,uBAAuB,GAAG,CAAA;AAAA,YAC1B,OAAA,CAAQ;AAAA,WACV;AAAA,QACF,SAAS,KAAA,EAAO;AACd,UAAA,MAAM,IAAI,KAAA,CAAM,kBAAA,CAAmB,KAAK,CAAC,CAAA;AAAA,QAC3C;AAAA,MACF,CAAC,CAAA;AAAA,IACH;AAAA,EACF,CAAC,CAAA;AACH","file":"index.js","sourcesContent":["/**\n * Run MCPJam eval suites inside vitest.\n *\n * --- A wrapper, not a reporter ---\n *\n * The obvious alternative was a custom vitest Reporter. This is deliberately\n * not that. A reporter observes tests; it cannot decide what a test IS. Evals\n * need the opposite: one `it` per eval case so failures land on named tests in\n * every vitest UI, watch mode and CI annotation that already exists, with no\n * bespoke output for anyone to learn. A reporter would also have no seat for\n * the gate, which is a verdict over the whole run rather than any one case.\n *\n * --- One run, many tests ---\n *\n * `suite.run()` is called ONCE, in `beforeAll`. That is not an optimization:\n * the suite uploads a single hosted run, computes one aggregate evaluation\n * config hash, and executes its cases sequentially. Calling it per `it` would\n * produce N hosted runs, N aggregate hashes, and a concurrency pattern the\n * suite explicitly avoids. The `it`s then read that one result.\n *\n * So an eval case's `it` is an ASSERTION over an already-computed result, not\n * the thing that runs it. Vitest timeouts on those tests are therefore\n * irrelevant; the timeout that matters is the hook's, which is why it defaults\n * to five minutes and is configurable.\n *\n * --- The gate is its own test ---\n *\n * The per-case tests answer \"did this case pass\". The gate answers \"does this\n * run satisfy the policy\", which is a different question with a different\n * failure mode — a run where every case passed can still breach a latency or\n * score-integrity gate. Giving it a named `it` means CI shows WHICH question\n * failed instead of a bare non-zero exit.\n */\n\nimport { beforeAll, describe, it } from \"vitest\";\nimport {\n assertGate,\n formatGateReport,\n gateInputFromRunResult,\n gateInputFromSuiteResult,\n type EvalRunResult,\n type EvalSuite,\n type EvalSuiteResult,\n type EvalTest,\n type EvalTestRunOptions,\n type GatePolicy,\n type GateReport,\n type HostExecutor,\n} from \"@mcpjam/sdk\";\n\n/** Five minutes. An eval suite is not a unit test. */\nexport const DEFAULT_HOOK_TIMEOUT_MS = 300_000;\n\n/** The vitest test title used for the policy gate. */\nexport const GATE_TEST_TITLE = \"eval gate\";\n\nexport type EvalCasePlan = {\n /** The vitest test title. */\n title: string;\n /** The suite key this case's result is recorded under. */\n testName: string;\n /** The hosted case id, when this case came from a hosted corpus. */\n scenarioId?: string;\n /**\n * The case's DECLARED id (`EvalTestConfig.id`).\n *\n * Surfaced so a CI reporter can key a case by its identity rather than by a\n * title that gets renamed. Deliberately NOT used in the vitest title: the\n * `[scenarioId]` suffix is the hosted-dashboard grep handle and still rides\n * `externalCaseId`, so changing what the suffix carries would break the greps\n * that exist.\n */\n caseId?: string;\n};\n\nexport type EvalSuitePlan = {\n cases: EvalCasePlan[];\n /** Present only when a gate policy was supplied. */\n gateTestTitle?: string;\n};\n\n/**\n * Decide the vitest titles for a suite, without registering anything.\n *\n * Pure and exported so the naming rules are testable on their own. A hosted\n * case's id is appended so a test title is greppable back to the dashboard row\n * it came from — explicitly, never inferred from the test name.\n */\nexport function planEvalSuite(\n suite: EvalSuite,\n options: { gate?: GatePolicy } = {}\n): EvalSuitePlan {\n const cases = suite.getAll().map((test): EvalCasePlan => {\n const testName = test.getName();\n const config = test.getConfig();\n const scenarioId = config.externalCaseId;\n // Read defensively: a user can end up with `@mcpjam/vitest` beside an older\n // `@mcpjam/sdk` copy, where `id` does not exist yet. An absent `caseId` is\n // a missing convenience; a thrown TypeError would be a broken test run.\n const caseId = (config as { id?: string }).id;\n const declared = caseId === undefined ? {} : { caseId };\n if (scenarioId === undefined) {\n return { title: testName, testName, ...declared };\n }\n // A corpus already suffixes `[id]` onto cases whose titles collide, so\n // appending unconditionally would render `Title [id] [id]`.\n const suffix = ` [${scenarioId}]`;\n return {\n title: testName.endsWith(suffix) ? testName : `${testName}${suffix}`,\n testName,\n scenarioId,\n ...declared,\n };\n });\n\n return {\n cases,\n ...(options.gate ? { gateTestTitle: GATE_TEST_TITLE } : {}),\n };\n}\n\n/**\n * Turn one case's recorded result into a pass or a thrown failure.\n *\n * Exported so the RED path is testable inside an ordinary `it` — a generated\n * failing test cannot be asserted on from within the same run without failing\n * the run itself.\n */\nexport function runAndAssertCase(\n run: EvalRunResult | undefined,\n testName: string,\n failureReport?: () => string\n): void {\n if (!run) {\n // Reachable if a case was added to the suite after the plan was taken.\n throw new Error(\n `No result was recorded for eval case \"${testName}\". The suite ran, but ` +\n `this case was not part of it.`\n );\n }\n if (run.failures === 0) return;\n\n let detail = \"\";\n try {\n detail = failureReport?.() ?? \"\";\n } catch {\n // A missing detail report must not replace the real failure with a\n // secondary one about formatting it.\n detail = \"\";\n }\n\n const summary =\n `${testName}: ${run.failures} of ${run.iterations} ` +\n `${run.iterations === 1 ? \"iteration\" : \"iterations\"} failed.`;\n throw new Error(detail ? `${summary}\\n\\n${detail}` : summary);\n}\n\n/**\n * Render a gate failure for a test message.\n *\n * Duck-typed as well as `instanceof`-checked. A published wrapper can end up\n * beside a second copy of `@mcpjam/sdk` — a mismatched transitive range, a\n * pnpm layout — and `instanceof` silently fails across copies. Falling back to\n * the message would drop the per-gate table, which is the whole reason the\n * error carries a report.\n */\nexport function gateFailureMessage(error: unknown): string {\n const report = (error as { report?: GateReport } | null)?.report;\n if (report && typeof report === \"object\" && \"outcome\" in report) {\n return formatGateReport(report);\n }\n return error instanceof Error ? error.message : String(error);\n}\n\nexport type EvalSuiteVitestOptions = {\n /** A ready executor. Mutually exclusive with `factory`. */\n executor?: HostExecutor;\n /** Built inside `beforeAll` — for an executor that must connect first. */\n factory?: () => HostExecutor | Promise<HostExecutor>;\n run: EvalTestRunOptions;\n /** Omit to register no gate test. */\n gate?: GatePolicy;\n hookTimeoutMs?: number;\n};\n\nasync function resolveExecutor(\n options: EvalSuiteVitestOptions\n): Promise<HostExecutor> {\n if (options.executor && options.factory) {\n throw new Error(\n \"Pass either `executor` or `factory` to describeEvalSuite, not both.\"\n );\n }\n if (options.executor) return options.executor;\n if (options.factory) return await options.factory();\n throw new Error(\n \"describeEvalSuite needs an `executor` or a `factory` to build one.\"\n );\n}\n\n/**\n * Register a vitest `describe` for an eval suite: one test per case, plus the\n * gate test when a policy is given.\n */\nexport function describeEvalSuite(\n name: string,\n suite: EvalSuite,\n options: EvalSuiteVitestOptions\n): void {\n const plan = planEvalSuite(suite, options);\n // Captured BEFORE the run so a case's detailed report is reachable by name\n // without re-walking the suite inside every test.\n const testsByName = new Map<string, EvalTest>(\n suite.getAll().map((test) => [test.getName(), test])\n );\n\n describe(name, () => {\n let result: EvalSuiteResult | undefined;\n\n beforeAll(async () => {\n const executor = await resolveExecutor(options);\n result = await suite.run(executor, options.run);\n }, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);\n\n for (const entry of plan.cases) {\n it(entry.title, () => {\n const test = testsByName.get(entry.testName);\n runAndAssertCase(result?.tests.get(entry.testName), entry.testName, () =>\n test ? test.getFailureReport() : \"\"\n );\n });\n }\n\n if (plan.gateTestTitle) {\n it(plan.gateTestTitle, () => {\n if (!result) {\n throw new Error(\"The eval suite did not run, so it cannot be gated.\");\n }\n try {\n // `assertGate` throws on `incomplete` too: a gate that could not be\n // decided has not been satisfied.\n //\n // NO WAIVER PATH HERE, and its absence is a decision rather than an\n // omission. `GateReport` gained a `waived` outcome for the HOSTED\n // gate, where a waiver is an audited platform record with an\n // authorized granter, a reason and an expiry behind it. This gate\n // runs against a local, code-first suite result: there is no such\n // record to consult, and the only way to express \"waive this\" would\n // be a flag in the same file as the assertion — a gate that turns\n // itself off, which is not a waiver at all. A caller who wants a\n // policy relaxed here edits the policy, in the open, in git.\n //\n // `formatGateReport` below renders a `waived` report correctly if\n // one ever does reach it, so the widened union is safe to pass\n // through untouched.\n assertGate(gateInputFromSuiteResult(result), options.gate as GatePolicy);\n } catch (error) {\n throw new Error(gateFailureMessage(error));\n }\n });\n }\n });\n}\n\nexport type EvalTestVitestOptions = EvalSuiteVitestOptions;\n\n/**\n * The single-test seat, for a file that owns one eval and wants no suite.\n *\n * Not implemented by wrapping the test in a throwaway `EvalSuite`: a suite\n * aggregates and uploads as a unit, so a synthetic one would change what is\n * reported for a case that is not part of any suite.\n *\n * `gate` is honoured here exactly as in `describeEvalSuite` — via\n * `gateInputFromRunResult` rather than the suite variant. Accepting the option\n * and ignoring it would be the worst outcome available: a policy that reports\n * green because nothing evaluated it.\n */\nexport function testEval(\n test: EvalTest,\n options: EvalTestVitestOptions\n): void {\n const scenarioId = test.getConfig().externalCaseId;\n const name = test.getName();\n const suffix = scenarioId === undefined ? \"\" : ` [${scenarioId}]`;\n const title = name.endsWith(suffix) ? name : `${name}${suffix}`;\n\n describe(title, () => {\n let run: EvalRunResult | undefined;\n\n beforeAll(async () => {\n const executor = await resolveExecutor(options);\n run = await test.run(executor, options.run);\n }, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);\n\n it(title, () => {\n runAndAssertCase(run, name, () => test.getFailureReport());\n });\n\n if (options.gate) {\n it(GATE_TEST_TITLE, () => {\n if (!run) {\n throw new Error(\"The eval did not run, so it cannot be gated.\");\n }\n try {\n assertGate(\n gateInputFromRunResult(run),\n options.gate as GatePolicy\n );\n } catch (error) {\n throw new Error(gateFailureMessage(error));\n }\n });\n }\n });\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAoDO,IAAM,uBAAA,GAA0B;AAGhC,IAAM,eAAA,GAAkB;AAkCxB,SAAS,aAAA,CACd,KAAA,EACA,OAAA,GAAiC,EAAC,EACnB;AACf,EAAA,MAAM,QAAQ,KAAA,CAAM,MAAA,EAAO,CAAE,GAAA,CAAI,CAAC,IAAA,KAAuB;AACvD,IAAA,MAAM,QAAA,GAAW,KAAK,OAAA,EAAQ;AAC9B,IAAA,MAAM,MAAA,GAAS,KAAK,SAAA,EAAU;AAC9B,IAAA,MAAM,aAAa,MAAA,CAAO,cAAA;AAI1B,IAAA,MAAM,SAAU,MAAA,CAA2B,EAAA;AAC3C,IAAA,MAAM,WAAW,MAAA,KAAW,MAAA,GAAY,EAAC,GAAI,EAAE,MAAA,EAAO;AACtD,IAAA,IAAI,eAAe,MAAA,EAAW;AAC5B,MAAA,OAAO,EAAE,KAAA,EAAO,QAAA,EAAU,QAAA,EAAU,GAAG,QAAA,EAAS;AAAA,IAClD;AAGA,IAAA,MAAM,MAAA,GAAS,KAAK,UAAU,CAAA,CAAA,CAAA;AAC9B,IAAA,OAAO;AAAA,MACL,KAAA,EAAO,SAAS,QAAA,CAAS,MAAM,IAAI,QAAA,GAAW,CAAA,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A,EAC7B,CAAC,IAAA,EAAgB,OAAA,KACf,gBAAA,CAAiB,MAAM,OAAO,CAAA;AAAA,EAChC;AAAA,IACE,MAAM,CAAC,IAAA,EAAgB,YACrB,gBAAA,CAAiB,IAAA,EAAM,SAAS,MAAM,CAAA;AAAA,IACxC,MAAM,CAAC,IAAA,EAAgB,YACrB,gBAAA,CAAiB,IAAA,EAAM,SAAS,MAAM;AAAA;AAE5C","file":"index.js","sourcesContent":["/**\n * Run MCPJam eval suites inside vitest.\n *\n * --- A wrapper, not a reporter ---\n *\n * The obvious alternative was a custom vitest Reporter. This is deliberately\n * not that. A reporter observes tests; it cannot decide what a test IS. Evals\n * need the opposite: one `it` per eval case so failures land on named tests in\n * every vitest UI, watch mode and CI annotation that already exists, with no\n * bespoke output for anyone to learn. A reporter would also have no seat for\n * the gate, which is a verdict over the whole run rather than any one case.\n *\n * --- One run, many tests ---\n *\n * `suite.run()` is called ONCE, in `beforeAll`. That is not an optimization:\n * the suite uploads a single hosted run, computes one aggregate evaluation\n * config hash, and executes its cases sequentially. Calling it per `it` would\n * produce N hosted runs, N aggregate hashes, and a concurrency pattern the\n * suite explicitly avoids. The `it`s then read that one result.\n *\n * So an eval case's `it` is an ASSERTION over an already-computed result, not\n * the thing that runs it. Vitest timeouts on those tests are therefore\n * irrelevant; the timeout that matters is the hook's, which is why it defaults\n * to five minutes and is configurable.\n *\n * --- The gate is its own test ---\n *\n * The per-case tests answer \"did this case pass\". The gate answers \"does this\n * run satisfy the policy\", which is a different question with a different\n * failure mode — a run where every case passed can still breach a latency or\n * score-integrity gate. Giving it a named `it` means CI shows WHICH question\n * failed instead of a bare non-zero exit.\n */\n\nimport { afterAll, beforeAll, describe, it } from \"vitest\";\nimport {\n assertGate,\n formatGateReport,\n formatRunSummaryTable,\n gateInputFromRunResult,\n gateInputFromSuiteResult,\n type EvalRunResult,\n type EvalSuite,\n type EvalSuiteResult,\n type EvalTest,\n type EvalTestRunOptions,\n type GatePolicy,\n type GateReport,\n type HostExecutor,\n} from \"@mcpjam/sdk\";\n\n/** Five minutes. An eval suite is not a unit test. */\nexport const DEFAULT_HOOK_TIMEOUT_MS = 300_000;\n\n/** The vitest test title used for the policy gate. */\nexport const GATE_TEST_TITLE = \"eval gate\";\n\nexport type EvalCasePlan = {\n /** The vitest test title. */\n title: string;\n /** The suite key this case's result is recorded under. */\n testName: string;\n /** The hosted case id, when this case came from a hosted corpus. */\n scenarioId?: string;\n /**\n * The case's DECLARED id (`EvalTestConfig.id`).\n *\n * Surfaced so a CI reporter can key a case by its identity rather than by a\n * title that gets renamed. Deliberately NOT used in the vitest title: the\n * `[scenarioId]` suffix is the hosted-dashboard grep handle and still rides\n * `externalCaseId`, so changing what the suffix carries would break the greps\n * that exist.\n */\n caseId?: string;\n};\n\nexport type EvalSuitePlan = {\n cases: EvalCasePlan[];\n /** Present only when a gate policy was supplied. */\n gateTestTitle?: string;\n};\n\n/**\n * Decide the vitest titles for a suite, without registering anything.\n *\n * Pure and exported so the naming rules are testable on their own. A hosted\n * case's id is appended so a test title is greppable back to the dashboard row\n * it came from — explicitly, never inferred from the test name.\n */\nexport function planEvalSuite(\n suite: EvalSuite,\n options: { gate?: GatePolicy } = {},\n): EvalSuitePlan {\n const cases = suite.getAll().map((test): EvalCasePlan => {\n const testName = test.getName();\n const config = test.getConfig();\n const scenarioId = config.externalCaseId;\n // Read defensively: a user can end up with `@mcpjam/vitest` beside an older\n // `@mcpjam/sdk` copy, where `id` does not exist yet. An absent `caseId` is\n // a missing convenience; a thrown TypeError would be a broken test run.\n const caseId = (config as { id?: string }).id;\n const declared = caseId === undefined ? {} : { caseId };\n if (scenarioId === undefined) {\n return { title: testName, testName, ...declared };\n }\n // A corpus already suffixes `[id]` onto cases whose titles collide, so\n // appending unconditionally would render `Title [id] [id]`.\n const suffix = ` [${scenarioId}]`;\n return {\n title: testName.endsWith(suffix) ? testName : `${testName}${suffix}`,\n testName,\n scenarioId,\n ...declared,\n };\n });\n\n return {\n cases,\n ...(options.gate ? { gateTestTitle: GATE_TEST_TITLE } : {}),\n };\n}\n\n/**\n * Turn one case's recorded result into a pass or a thrown failure.\n *\n * Exported so the RED path is testable inside an ordinary `it` — a generated\n * failing test cannot be asserted on from within the same run without failing\n * the run itself.\n */\nexport function runAndAssertCase(\n run: EvalRunResult | undefined,\n testName: string,\n failureReport?: () => string,\n): void {\n if (!run) {\n // Reachable if a case was added to the suite after the plan was taken.\n throw new Error(\n `No result was recorded for eval case \"${testName}\". The suite ran, but ` +\n `this case was not part of it.`,\n );\n }\n if (run.iterations === 0)\n throw new Error(`No iterations executed for eval case \"${testName}\".`);\n if (run.failures === 0) return;\n\n let detail = \"\";\n try {\n detail = failureReport?.() ?? \"\";\n } catch {\n // A missing detail report must not replace the real failure with a\n // secondary one about formatting it.\n detail = \"\";\n }\n\n const summary =\n `${testName}: ${run.failures} of ${run.iterations} ` +\n `${run.iterations === 1 ? \"iteration\" : \"iterations\"} failed.`;\n throw new Error(detail ? `${summary}\\n\\n${detail}` : summary);\n}\n\n/**\n * Render a gate failure for a test message.\n *\n * Duck-typed as well as `instanceof`-checked. A published wrapper can end up\n * beside a second copy of `@mcpjam/sdk` — a mismatched transitive range, a\n * pnpm layout — and `instanceof` silently fails across copies. Falling back to\n * the message would drop the per-gate table, which is the whole reason the\n * error carries a report.\n */\nexport function gateFailureMessage(error: unknown): string {\n const report = (error as { report?: GateReport } | null)?.report;\n if (report && typeof report === \"object\" && \"outcome\" in report) {\n return formatGateReport(report);\n }\n return error instanceof Error ? error.message : String(error);\n}\n\nexport type EvalSuiteVitestOptions = {\n /** A ready executor. Mutually exclusive with `factory`. */\n executor?: HostExecutor;\n /** Built inside `beforeAll` — for an executor that must connect first. */\n factory?: () => HostExecutor | Promise<HostExecutor>;\n run: Omit<EvalTestRunOptions, \"iterations\"> & { iterations?: number };\n /** Selected case identities. Selection retains the source coverage manifest. */\n only?: readonly string[];\n /** Skips win over only; no skipped case executes. */\n skip?: readonly string[];\n /** Explicit cleanup, run even when execution or reporting fails. */\n dispose?: (executor: HostExecutor) => void | Promise<void>;\n /** Defaults to table. Formatting errors never replace evaluation results. */\n summary?: \"none\" | \"table\";\n /** Omit to register no gate test. */\n gate?: GatePolicy;\n hookTimeoutMs?: number;\n};\n\nasync function resolveExecutor(\n options: EvalSuiteVitestOptions,\n): Promise<HostExecutor> {\n if (options.executor && options.factory) {\n throw new Error(\n \"Pass either `executor` or `factory` to describeEvalSuite, not both.\",\n );\n }\n if (options.executor) return options.executor;\n if (options.factory) return await options.factory();\n throw new Error(\n \"describeEvalSuite needs an `executor` or a `factory` to build one.\",\n );\n}\n\n/**\n * Register a vitest `describe` for an eval suite: one test per case, plus the\n * gate test when a policy is given.\n */\nfunction registerEvalSuite(\n name: string,\n suite: EvalSuite,\n options: EvalSuiteVitestOptions,\n mode: \"normal\" | \"skip\" | \"only\" = \"normal\",\n): void {\n const plan = planEvalSuite(suite, options);\n const available = new Set(suite.getAll().map((test) => test.getId()));\n for (const id of [...(options.only ?? []), ...(options.skip ?? [])]) {\n if (!available.has(id)) throw new Error(`Unknown selected case ID: ${id}`);\n }\n const skipped = new Set(options.skip ?? []);\n const selectedIds = [...available].filter(\n (id) => !skipped.has(id) && (!options.only || options.only.includes(id)),\n );\n const selectedSuite =\n options.only || options.skip ? suite.subset(selectedIds) : suite;\n // Captured BEFORE the run so a case's detailed report is reachable by name\n // without re-walking the suite inside every test.\n const testsByName = new Map<string, EvalTest>(\n selectedSuite.getAll().map((test) => [test.getName(), test]),\n );\n\n const register =\n mode === \"skip\"\n ? describe.skip\n : mode === \"only\" || options.only\n ? describe.only\n : describe;\n register(name, () => {\n let executor: HostExecutor | undefined;\n let result: EvalSuiteResult | undefined;\n\n beforeAll(async () => {\n if (selectedIds.length === 0) return;\n executor = await resolveExecutor(options);\n try {\n result = await selectedSuite.run(executor, {\n ...options.run,\n summary: \"none\",\n });\n } finally {\n const completed = result ?? selectedSuite.getResults();\n if (completed && options.summary !== \"none\") {\n try {\n console.log(\n formatRunSummaryTable(\n completed,\n selectedSuite.getReportingReceipt(),\n ),\n );\n } catch {\n /* formatting is observational */\n }\n }\n }\n }, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);\n\n afterAll(async () => {\n if (executor) await options.dispose?.(executor);\n });\n for (const entry of plan.cases) {\n const registerCase =\n entry.caseId && !selectedIds.includes(entry.caseId) ? it.skip : it;\n registerCase(entry.title, () => {\n const test = testsByName.get(entry.testName);\n runAndAssertCase(\n result?.tests.get(entry.testName),\n entry.testName,\n () => (test ? test.getFailureReport() : \"\"),\n );\n });\n }\n\n if (plan.gateTestTitle) {\n it(plan.gateTestTitle, () => {\n if (!result) {\n throw new Error(\"The eval suite did not run, so it cannot be gated.\");\n }\n try {\n // `assertGate` throws on `incomplete` too: a gate that could not be\n // decided has not been satisfied.\n //\n // NO WAIVER PATH HERE, and its absence is a decision rather than an\n // omission. `GateReport` gained a `waived` outcome for the HOSTED\n // gate, where a waiver is an audited platform record with an\n // authorized granter, a reason and an expiry behind it. This gate\n // runs against a local, code-first suite result: there is no such\n // record to consult, and the only way to express \"waive this\" would\n // be a flag in the same file as the assertion — a gate that turns\n // itself off, which is not a waiver at all. A caller who wants a\n // policy relaxed here edits the policy, in the open, in git.\n //\n // `formatGateReport` below renders a `waived` report correctly if\n // one ever does reach it, so the widened union is safe to pass\n // through untouched.\n assertGate(\n gateInputFromSuiteResult(result),\n options.gate as GatePolicy,\n );\n } catch (error) {\n throw new Error(gateFailureMessage(error));\n }\n });\n }\n });\n}\n\nexport const describeEvalSuite = Object.assign(\n (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) =>\n registerEvalSuite(name, suite, options),\n {\n skip: (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) =>\n registerEvalSuite(name, suite, options, \"skip\"),\n only: (name: string, suite: EvalSuite, options: EvalSuiteVitestOptions) =>\n registerEvalSuite(name, suite, options, \"only\"),\n },\n);\n\nexport type EvalTestVitestOptions = Omit<\n EvalSuiteVitestOptions,\n \"run\" | \"only\" | \"skip\"\n> & { run: EvalTestRunOptions };\n\n/**\n * The single-test seat, for a file that owns one eval and wants no suite.\n *\n * Not implemented by wrapping the test in a throwaway `EvalSuite`: a suite\n * aggregates and uploads as a unit, so a synthetic one would change what is\n * reported for a case that is not part of any suite.\n *\n * `gate` is honoured here exactly as in `describeEvalSuite` — via\n * `gateInputFromRunResult` rather than the suite variant. Accepting the option\n * and ignoring it would be the worst outcome available: a policy that reports\n * green because nothing evaluated it.\n */\nfunction registerTestEval(\n test: EvalTest,\n options: EvalTestVitestOptions,\n mode: \"normal\" | \"skip\" | \"only\" = \"normal\",\n): void {\n const scenarioId = test.getConfig().externalCaseId;\n const name = test.getName();\n const suffix = scenarioId === undefined ? \"\" : ` [${scenarioId}]`;\n const title = name.endsWith(suffix) ? name : `${name}${suffix}`;\n\n const register =\n mode === \"skip\"\n ? describe.skip\n : mode === \"only\"\n ? describe.only\n : describe;\n register(title, () => {\n let executor: HostExecutor | undefined;\n let run: EvalRunResult | undefined;\n\n beforeAll(async () => {\n executor = await resolveExecutor(options);\n try {\n run = await test.run(executor, { ...options.run, summary: \"none\" });\n } finally {\n const completed = run ?? test.getResults();\n if (completed && options.summary !== \"none\") {\n try {\n console.log(\n formatRunSummaryTable(completed, test.getReportingReceipt()),\n );\n } catch {\n /* formatting is observational */\n }\n }\n }\n }, options.hookTimeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS);\n\n afterAll(async () => {\n if (executor) await options.dispose?.(executor);\n });\n it(\"passes\", () => {\n runAndAssertCase(run, name, () => test.getFailureReport());\n });\n\n if (options.gate) {\n it(GATE_TEST_TITLE, () => {\n if (!run) {\n throw new Error(\"The eval did not run, so it cannot be gated.\");\n }\n try {\n assertGate(gateInputFromRunResult(run), options.gate as GatePolicy);\n } catch (error) {\n throw new Error(gateFailureMessage(error));\n }\n });\n }\n });\n}\n\nexport const testEval = Object.assign(\n (test: EvalTest, options: EvalTestVitestOptions) =>\n registerTestEval(test, options),\n {\n skip: (test: EvalTest, options: EvalTestVitestOptions) =>\n registerTestEval(test, options, \"skip\"),\n only: (test: EvalTest, options: EvalTestVitestOptions) =>\n registerTestEval(test, options, \"only\"),\n },\n);\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcpjam/vitest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Run MCPJam eval suites inside vitest: one test per eval case, plus a gate test that fails the build on a policy breach.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"vitest": ">=3.2.0 <4"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@mcpjam/sdk": "^8.
|
|
32
|
+
"@mcpjam/sdk": "^8.8.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"tsup": "^8.3.5",
|