Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): migrate unstable eval unit tests to vitest (#13300)

Hassieb Pakzad2012bd14ee6d
diff --git a/web/src/__tests__/server/unit/evaluator-preflight.servertest.ts b/web/src/__tests__/server/unit/evaluator-preflight.servertest.ts
index 7fdbce5b76..70ab60f27e 100644
--- a/web/src/__tests__/server/unit/evaluator-preflight.servertest.ts
+++ b/web/src/__tests__/server/unit/evaluator-preflight.servertest.ts
@@ -1,18 +1,13 @@
-/** @jest-environment node */
-
-jest.mock("@langfuse/shared/src/server", () => {
- const actual = jest.requireActual("@langfuse/shared/src/server");
- return {
- ...actual,
- DefaultEvalModelService: {
- fetchValidModelConfig: jest.fn(),
- },
- logger: {
- debug: jest.fn(),
- },
- testModelCall: jest.fn(),
- };
-});
+vi.mock("@langfuse/shared/src/server", async () => ({
+ ...(await vi.importActual("@langfuse/shared/src/server")),
+ DefaultEvalModelService: {
+ fetchValidModelConfig: vi.fn(),
+ },
+ logger: {
+ debug: vi.fn(),
+ },
+ testModelCall: vi.fn(),
+}));
import {
createNumericEvalOutputDefinition,
@@ -30,15 +25,15 @@ const numericOutputDefinition = createNumericEvalOutputDefinition({
});
describe("evaluator preflight", () => {
- const mockFetchValidModelConfig = jest.mocked(
+ const mockFetchValidModelConfig = vi.mocked(
DefaultEvalModelService.fetchValidModelConfig,
);
- const mockTestModelCall = jest.mocked(testModelCall);
+ const mockTestModelCall = vi.mocked(testModelCall);
const originalSkipFlag =
process.env.LANGFUSE_SKIP_EVALUATOR_MODEL_CALL_VALIDATION;
beforeEach(() => {
- jest.resetAllMocks();
+ vi.resetAllMocks();
delete process.env.LANGFUSE_SKIP_EVALUATOR_MODEL_CALL_VALIDATION;
mockFetchValidModelConfig.mockResolvedValue({
valid: true,
diff --git a/web/src/__tests__/server/unit/unstable-evals-contract.servertest.ts b/web/src/__tests__/server/unit/unstable-evals-contract.servertest.ts
index dcd7f19c28..7ff4965a74 100644
--- a/web/src/__tests__/server/unit/unstable-evals-contract.servertest.ts
+++ b/web/src/__tests__/server/unit/unstable-evals-contract.servertest.ts
@@ -1,5 +1,3 @@
-/** @jest-environment node */
-
import {
createNumericEvalOutputDefinition,
EvalTargetObject,
diff --git a/web/src/__tests__/server/unit/unstable-evals-queries.servertest.ts b/web/src/__tests__/server/unit/unstable-evals-queries.servertest.ts
index 910fdf9893..e48f84b0fa 100644
--- a/web/src/__tests__/server/unit/unstable-evals-queries.servertest.ts
+++ b/web/src/__tests__/server/unit/unstable-evals-queries.servertest.ts
@@ -1,6 +1,6 @@
-/** @jest-environment node */
+import type { Mock } from "vitest";
-jest.mock("@langfuse/shared/src/db", () => {
+vi.mock("@langfuse/shared/src/db", () => {
return {
Prisma: {
sql: (strings: TemplateStringsArray, ...values: unknown[]) => ({
@@ -9,14 +9,14 @@ jest.mock("@langfuse/shared/src/db", () => {
}),
},
prisma: {
- $queryRaw: jest.fn(),
+ $queryRaw: vi.fn(),
evalTemplate: {
- findMany: jest.fn(),
- findFirst: jest.fn(),
+ findMany: vi.fn(),
+ findFirst: vi.fn(),
},
jobConfiguration: {
- count: jest.fn(),
- groupBy: jest.fn(),
+ count: vi.fn(),
+ groupBy: vi.fn(),
},
},
};
@@ -30,16 +30,15 @@ import {
listPublicEvaluatorTemplates,
} from "@/src/features/evals/server/unstable-public-api/queries";
-const mockQueryRaw = prisma.$queryRaw as jest.Mock;
-const mockEvalTemplateFindMany = prisma.evalTemplate.findMany as jest.Mock;
-const mockEvalTemplateFindFirst = prisma.evalTemplate.findFirst as jest.Mock;
-const mockJobConfigurationCount = prisma.jobConfiguration.count as jest.Mock;
-const mockJobConfigurationGroupBy = prisma.jobConfiguration
- .groupBy as jest.Mock;
+const mockQueryRaw = prisma.$queryRaw as Mock;
+const mockEvalTemplateFindMany = prisma.evalTemplate.findMany as Mock;
+const mockEvalTemplateFindFirst = prisma.evalTemplate.findFirst as Mock;
+const mockJobConfigurationCount = prisma.jobConfiguration.count as Mock;
+const mockJobConfigurationGroupBy = prisma.jobConfiguration.groupBy as Mock;
describe("unstable public eval queries", () => {
beforeEach(() => {
- jest.clearAllMocks();
+ vi.clearAllMocks();
});
it("paginates latest evaluator versions per family before loading exact templates", async () => {
diff --git a/web/src/__tests__/server/unit/unstable-evals-service.servertest.ts b/web/src/__tests__/server/unit/unstable-evals-service.servertest.ts
index 75e628469c..a429cefe66 100644
--- a/web/src/__tests__/server/unit/unstable-evals-service.servertest.ts
+++ b/web/src/__tests__/server/unit/unstable-evals-service.servertest.ts
@@ -1,48 +1,48 @@
-/** @jest-environment node */
+import type { Mock } from "vitest";
-const mockEvalTemplateCreate = jest.fn();
-const mockEvalTemplateFindMany = jest.fn();
-const mockJobConfigurationFindMany = jest.fn();
-const mockJobConfigurationUpdate = jest.fn();
+const mockEvalTemplateCreate = vi.fn();
+const mockEvalTemplateFindMany = vi.fn();
+const mockJobConfigurationFindMany = vi.fn();
+const mockJobConfigurationUpdate = vi.fn();
-jest.mock(
+vi.mock(
"../../../features/evals/server/unstable-public-api/validation",
- () => {
- const actual = jest.requireActual(
+ async () => {
+ const actual = await vi.importActual(
"../../../features/evals/server/unstable-public-api/validation",
);
return {
...actual,
- assertEvaluatorDefinitionCanRunForPublicApi: jest.fn(),
+ assertEvaluatorDefinitionCanRunForPublicApi: vi.fn(),
};
},
);
-jest.mock("../../../features/evals/server/unstable-public-api/queries", () => ({
- countActiveEvaluationRules: jest.fn(),
- findPublicEvaluatorTemplateOrThrow: jest.fn(),
- countEvaluationRulesForEvaluator: jest.fn(),
- countEvaluationRulesForEvaluatorIds: jest.fn(),
- listPublicEvaluatorTemplates: jest.fn(),
- loadEvaluatorForEvaluationRule: jest.fn(),
- findPublicEvaluationRuleOrThrow: jest.fn(),
+vi.mock("../../../features/evals/server/unstable-public-api/queries", () => ({
+ countActiveEvaluationRules: vi.fn(),
+ findPublicEvaluatorTemplateOrThrow: vi.fn(),
+ countEvaluationRulesForEvaluator: vi.fn(),
+ countEvaluationRulesForEvaluatorIds: vi.fn(),
+ listPublicEvaluatorTemplates: vi.fn(),
+ loadEvaluatorForEvaluationRule: vi.fn(),
+ findPublicEvaluationRuleOrThrow: vi.fn(),
}));
-jest.mock("@langfuse/shared/src/server", () => ({
- ...jest.requireActual("@langfuse/shared/src/server"),
- invalidateProjectEvalConfigCaches: jest.fn(),
+vi.mock("@langfuse/shared/src/server", async () => ({
+ ...(await vi.importActual("@langfuse/shared/src/server")),
+ invalidateProjectEvalConfigCaches: vi.fn(),
ClickHouseClientManager: {
getInstance: () => ({
- closeAllConnections: jest.fn().mockResolvedValue(undefined),
+ closeAllConnections: vi.fn().mockResolvedValue(undefined),
}),
},
logger: {
- debug: jest.fn(),
+ debug: vi.fn(),
},
}));
-jest.mock("@langfuse/shared/src/db", () => ({
+vi.mock("@langfuse/shared/src/db", () => ({
Prisma: {
PrismaClientKnownRequestError: class PrismaClientKnownRequestError extends Error {
code: string;
@@ -65,14 +65,14 @@ jest.mock("@langfuse/shared/src/db", () => ({
},
},
prisma: {
- $transaction: jest.fn(),
+ $transaction: vi.fn(),
dataset: {
- findMany: jest.fn(),
+ findMany: vi.fn(),
},
jobConfiguration: {
- findFirst: jest.fn(),
- create: jest.fn(),
- update: jest.fn(),
+ findFirst: vi.fn(),
+ create: vi.fn(),
+ update: vi.fn(),
},
},
}));
@@ -130,29 +130,29 @@ const managedTemplate = {
};
const mockedPrisma = prisma as unknown as {
- $transaction: jest.Mock;
+ $transaction: Mock;
dataset: {
- findMany: jest.Mock;
+ findMany: Mock;
};
jobConfiguration: {
- findFirst: jest.Mock;
- create: jest.Mock;
- update: jest.Mock;
+ findFirst: Mock;
+ create: Mock;
+ update: Mock;
};
};
-const mockAssertEvaluatorDefinitionCanRunForPublicApi = jest.mocked(
+const mockAssertEvaluatorDefinitionCanRunForPublicApi = vi.mocked(
validationModule.assertEvaluatorDefinitionCanRunForPublicApi,
);
-const mockLoadEvaluatorForEvaluationRule = jest.mocked(
+const mockLoadEvaluatorForEvaluationRule = vi.mocked(
queryModule.loadEvaluatorForEvaluationRule,
);
-const mockCountActiveEvaluationRules = jest.mocked(
+const mockCountActiveEvaluationRules = vi.mocked(
queryModule.countActiveEvaluationRules,
);
-const mockFindPublicEvaluationRuleOrThrow = jest.mocked(
+const mockFindPublicEvaluationRuleOrThrow = vi.mocked(
queryModule.findPublicEvaluationRuleOrThrow,
);
-const mockCountEvaluationRulesForEvaluator = jest.mocked(
+const mockCountEvaluationRulesForEvaluator = vi.mocked(
queryModule.countEvaluationRulesForEvaluator,
);
@@ -190,7 +190,7 @@ const createEvaluationRuleRecord = (overrides?: Record<string, unknown>) =>
describe("unstable public eval services", () => {
beforeEach(() => {
- jest.clearAllMocks();
+ vi.clearAllMocks();
mockCountActiveEvaluationRules.mockResolvedValue(0);
mockCountEvaluationRulesForEvaluator.mockResolvedValue(0);
mockedPrisma.dataset.findMany.mockResolvedValue([]);
diff --git a/web/src/__tests__/server/unstable-evals-api.servertest.ts b/web/src/__tests__/server/unstable-evals-api.servertest.ts
index 7b18e021c9..909ebc5366 100644
--- a/web/src/__tests__/server/unstable-evals-api.servertest.ts
+++ b/web/src/__tests__/server/unstable-evals-api.servertest.ts
@@ -1,5 +1,3 @@
-/** @jest-environment node */
-
import {
makeAPICall,
makeZodVerifiedAPICall,