Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-results-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"evalite": minor
---

Add only modifier
24 changes: 24 additions & 0 deletions packages/evalite-tests/tests/fixtures/each-only/each-only.eval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { evalite } from "evalite";

evalite.each([{ name: "variant-1", input: "v1" }]).only("Only Each Test", {
data: () => {
return [{ input: "only-each", expected: "only-each" }];
},
task: function getTask(input: string) {
console.log("task() called in Only Each Test");
return input;
},
scorers: [],
});

evalite.each([{ name: "variant-1", input: "v1" }])("Regular Each Test", {
data: () => {
return [{ input: "regular-each", expected: "regular-each" }];
},
task: function getTask(input: string) {
// This should not be called because another test has .only()
console.log("task() called in Regular Each Test");
return input;
},
scorers: [],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { evalite } from "evalite";

evalite.only("Only Test 1", {
data: () => {
return [{ input: "only1", expected: "only1" }];
},
task: function getTask(input: string) {
console.log("task() called in Only Test 1");
return input;
},
scorers: [],
});

evalite.only("Only Test 2", {
data: () => {
return [{ input: "only2", expected: "only2" }];
},
task: function getTask(input: string) {
console.log("task() called in Only Test 2");
return input;
},
scorers: [],
});

evalite("Regular Test", {
data: () => {
return [{ input: "regular", expected: "regular" }];
},
task: function getTask(input: string) {
// This should not be called because other tests have .only()
console.log("task() called in Regular Test");
return input;
},
scorers: [],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { evalite } from "evalite";

evalite.only("Only Test", {
data: () => {
return [{ input: "only", expected: "only" }];
},
task: function getTask(input: string) {
console.log("task() called in Only Test");
return input;
},
scorers: [],
});

evalite("Regular Test", {
data: () => {
return [{ input: "regular", expected: "regular" }];
},
task: function getTask(input: string) {
// This should not be called because another test has .only()
console.log("task() called in Regular Test");
return input;
},
scorers: [],
});
40 changes: 40 additions & 0 deletions packages/evalite-tests/tests/test-modifiers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,43 @@ it("should not call opts.data() for a skipped test with .each().skip()", async (
expect(output).toContain("opts.data() called in Regular Each Test");
expect(output).not.toContain("opts.data() called in Skipped Each Test");
});

it("should only run tests marked with .only()", async () => {
await using fixture = await loadFixture("test-modifiers-only");

await fixture.run({
mode: "run-once-and-exit",
});

const output = fixture.getOutput();

expect(output).toContain("task() called in Only Test");
expect(output).not.toContain("task() called in Regular Test");
});

it("should run all tests marked with .only()", async () => {
await using fixture = await loadFixture("test-modifiers-multiple-only");

await fixture.run({
mode: "run-once-and-exit",
});

const output = fixture.getOutput();

expect(output).toContain("task() called in Only Test 1");
expect(output).toContain("task() called in Only Test 2");
expect(output).not.toContain("task() called in Regular Test");
});

it("should only run tests marked with .each().only()", async () => {
await using fixture = await loadFixture("each-only");

await fixture.run({
mode: "run-once-and-exit",
});

const output = fixture.getOutput();

expect(output).toContain("task() called in Only Each Test");
expect(output).not.toContain("task() called in Regular Each Test");
});
21 changes: 19 additions & 2 deletions packages/evalite/src/evalite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,18 @@ evalite.skip = <TInput, TOutput, TExpected = undefined>(
opts: Evalite.RunnerOpts<TInput, TOutput, TExpected>
) => registerEvalite(evalName, opts, { modifier: "skip" });

evalite.only = <TInput, TOutput, TExpected = undefined>(
evalName: string,
opts: Evalite.RunnerOpts<TInput, TOutput, TExpected>
) => registerEvalite(evalName, opts, { modifier: "only" });

evalite.each = <TVariant>(
variants: Array<{ name: string; input: TVariant; only?: boolean }>
) => {
function createEvals<TInput, TOutput, TExpected = undefined>(
evalName: string,
opts: Evalite.RunnerOpts<TInput, TOutput, TExpected, TVariant>,
modifier?: "skip"
modifier?: "skip" | "only"
) {
const hasOnlyFlag = variants.some((v) => v.only === true);
const filteredVariants = hasOnlyFlag
Expand Down Expand Up @@ -185,6 +190,13 @@ evalite.each = <TVariant>(
return createEvals(evalName, opts, "skip");
};

eachFn.only = <TInput, TOutput, TExpected = undefined>(
evalName: string,
opts: Evalite.RunnerOpts<TInput, TOutput, TExpected, TVariant>
) => {
return createEvals(evalName, opts, "only");
};

return eachFn;
};

Expand Down Expand Up @@ -223,7 +235,12 @@ function registerEvalite<TInput, TOutput, TExpected>(
variantGroup?: string;
} = {}
) {
const describeFn = vitestOpts.modifier === "skip" ? describe.skip : describe;
const describeFn =
vitestOpts.modifier === "skip"
? describe.skip
: vitestOpts.modifier === "only"
? describe.only
: describe;
const datasetPromise: Promise<Result<any, Error>> =
vitestOpts.modifier === "skip"
? Promise.resolve({ success: true, data: [] })
Expand Down
1 change: 1 addition & 0 deletions packages/evalite/src/run-evalite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ export const runEvalite = async (opts: {
mode: "test",
browser: undefined,
config: false,
allowOnly: true,
},
{
...mergedViteConfig,
Expand Down