Skip to content

Commit 670d04a

Browse files
feat: only
1 parent 7f73ec0 commit 670d04a

File tree

7 files changed

+148
-2
lines changed

7 files changed

+148
-2
lines changed

.changeset/warm-results-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"evalite": minor
3+
---
4+
5+
Add only modifier
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { evalite } from "evalite";
2+
3+
evalite.each([{ name: "variant-1", input: "v1" }]).only("Only Each Test", {
4+
data: () => {
5+
return [{ input: "only-each", expected: "only-each" }];
6+
},
7+
task: function getTask(input: string) {
8+
console.log("task() called in Only Each Test");
9+
return input;
10+
},
11+
scorers: [],
12+
});
13+
14+
evalite.each([{ name: "variant-1", input: "v1" }])("Regular Each Test", {
15+
data: () => {
16+
return [{ input: "regular-each", expected: "regular-each" }];
17+
},
18+
task: function getTask(input: string) {
19+
// This should not be called because another test has .only()
20+
console.log("task() called in Regular Each Test");
21+
return input;
22+
},
23+
scorers: [],
24+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { evalite } from "evalite";
2+
3+
evalite.only("Only Test 1", {
4+
data: () => {
5+
return [{ input: "only1", expected: "only1" }];
6+
},
7+
task: function getTask(input: string) {
8+
console.log("task() called in Only Test 1");
9+
return input;
10+
},
11+
scorers: [],
12+
});
13+
14+
evalite.only("Only Test 2", {
15+
data: () => {
16+
return [{ input: "only2", expected: "only2" }];
17+
},
18+
task: function getTask(input: string) {
19+
console.log("task() called in Only Test 2");
20+
return input;
21+
},
22+
scorers: [],
23+
});
24+
25+
evalite("Regular Test", {
26+
data: () => {
27+
return [{ input: "regular", expected: "regular" }];
28+
},
29+
task: function getTask(input: string) {
30+
// This should not be called because other tests have .only()
31+
console.log("task() called in Regular Test");
32+
return input;
33+
},
34+
scorers: [],
35+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { evalite } from "evalite";
2+
3+
evalite.only("Only Test", {
4+
data: () => {
5+
return [{ input: "only", expected: "only" }];
6+
},
7+
task: function getTask(input: string) {
8+
console.log("task() called in Only Test");
9+
return input;
10+
},
11+
scorers: [],
12+
});
13+
14+
evalite("Regular Test", {
15+
data: () => {
16+
return [{ input: "regular", expected: "regular" }];
17+
},
18+
task: function getTask(input: string) {
19+
// This should not be called because another test has .only()
20+
console.log("task() called in Regular Test");
21+
return input;
22+
},
23+
scorers: [],
24+
});

packages/evalite-tests/tests/test-modifiers.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,43 @@ it("should not call opts.data() for a skipped test with .each().skip()", async (
5050
expect(output).toContain("opts.data() called in Regular Each Test");
5151
expect(output).not.toContain("opts.data() called in Skipped Each Test");
5252
});
53+
54+
it("should only run tests marked with .only()", async () => {
55+
await using fixture = await loadFixture("test-modifiers-only");
56+
57+
await fixture.run({
58+
mode: "run-once-and-exit",
59+
});
60+
61+
const output = fixture.getOutput();
62+
63+
expect(output).toContain("task() called in Only Test");
64+
expect(output).not.toContain("task() called in Regular Test");
65+
});
66+
67+
it("should run all tests marked with .only()", async () => {
68+
await using fixture = await loadFixture("test-modifiers-multiple-only");
69+
70+
await fixture.run({
71+
mode: "run-once-and-exit",
72+
});
73+
74+
const output = fixture.getOutput();
75+
76+
expect(output).toContain("task() called in Only Test 1");
77+
expect(output).toContain("task() called in Only Test 2");
78+
expect(output).not.toContain("task() called in Regular Test");
79+
});
80+
81+
it("should only run tests marked with .each().only()", async () => {
82+
await using fixture = await loadFixture("each-only");
83+
84+
await fixture.run({
85+
mode: "run-once-and-exit",
86+
});
87+
88+
const output = fixture.getOutput();
89+
90+
expect(output).toContain("task() called in Only Each Test");
91+
expect(output).not.toContain("task() called in Regular Each Test");
92+
});

packages/evalite/src/evalite.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,18 @@ evalite.skip = <TInput, TOutput, TExpected = undefined>(
146146
opts: Evalite.RunnerOpts<TInput, TOutput, TExpected>
147147
) => registerEvalite(evalName, opts, { modifier: "skip" });
148148

149+
evalite.only = <TInput, TOutput, TExpected = undefined>(
150+
evalName: string,
151+
opts: Evalite.RunnerOpts<TInput, TOutput, TExpected>
152+
) => registerEvalite(evalName, opts, { modifier: "only" });
153+
149154
evalite.each = <TVariant>(
150155
variants: Array<{ name: string; input: TVariant; only?: boolean }>
151156
) => {
152157
function createEvals<TInput, TOutput, TExpected = undefined>(
153158
evalName: string,
154159
opts: Evalite.RunnerOpts<TInput, TOutput, TExpected, TVariant>,
155-
modifier?: "skip"
160+
modifier?: "skip" | "only"
156161
) {
157162
const hasOnlyFlag = variants.some((v) => v.only === true);
158163
const filteredVariants = hasOnlyFlag
@@ -185,6 +190,13 @@ evalite.each = <TVariant>(
185190
return createEvals(evalName, opts, "skip");
186191
};
187192

193+
eachFn.only = <TInput, TOutput, TExpected = undefined>(
194+
evalName: string,
195+
opts: Evalite.RunnerOpts<TInput, TOutput, TExpected, TVariant>
196+
) => {
197+
return createEvals(evalName, opts, "only");
198+
};
199+
188200
return eachFn;
189201
};
190202

@@ -223,7 +235,12 @@ function registerEvalite<TInput, TOutput, TExpected>(
223235
variantGroup?: string;
224236
} = {}
225237
) {
226-
const describeFn = vitestOpts.modifier === "skip" ? describe.skip : describe;
238+
const describeFn =
239+
vitestOpts.modifier === "skip"
240+
? describe.skip
241+
: vitestOpts.modifier === "only"
242+
? describe.only
243+
: describe;
227244
const datasetPromise: Promise<Result<any, Error>> =
228245
vitestOpts.modifier === "skip"
229246
? Promise.resolve({ success: true, data: [] })

packages/evalite/src/run-evalite.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ export const runEvalite = async (opts: {
350350
mode: "test",
351351
browser: undefined,
352352
config: false,
353+
allowOnly: true,
353354
},
354355
{
355356
...mergedViteConfig,

0 commit comments

Comments
 (0)