or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced.mdannotation-queues.mdanonymizer.mdclient-api.mddatasets.mdevaluation.mdfeedback.mdgetting-started.mdindex.mdjest.mdlangchain.mdopentelemetry.mdprompts.mdrun-trees.mdschemas.mdtesting.mdtracing.mdvercel.mdvitest.mdworkflows.mdwrappers.md
tile.json

testing.mddocs/

Testing Framework Integration

LangSmith provides seamless integration with Jest and Vitest testing frameworks, enabling test-driven evaluation workflows where tests automatically create datasets, run evaluations, and track results.

Overview

The testing integrations extend familiar testing APIs with LangSmith-specific features:

  • Automatic Dataset Creation: Tests create dataset examples automatically
  • Custom Matchers: LLM-specific assertions for semantic similarity and closeness
  • Feedback Logging: Track evaluation metrics during test execution
  • Evaluator Wrappers: Create reusable evaluation logic
  • Experiment Tracking: Test runs create experiments in LangSmith

When to use testing integration:

  • Build test suites for LLM applications
  • Create evaluation datasets through testing
  • Regression testing for model outputs
  • CI/CD integration with quality gates

Choose Your Framework

LangSmith provides identical testing APIs for both Jest and Vitest. Choose based on your existing test infrastructure:

Jest Integration →

Use if you're already using Jest or prefer its ecosystem.

import { test, expect } from "langsmith/jest";

test(
  "greeting generation",
  {
    input: { name: "Alice" },
    expected: { greeting: "Hello, Alice!" },
  },
  async (input) => {
    return { greeting: `Hello, ${input.name}!` };
  }
);

Complete Jest documentation →

Vitest Integration →

Use if you're using Vite/Vitest or want faster test execution.

import { test, expect } from "langsmith/vitest";

test(
  "summarize text correctly",
  {
    input: { text: "Long document..." },
    expected: { summary: "Summary" }
  },
  async (input) => {
    const result = await summarizeText(input.text);
    return result;
  }
);

Vitest requires reporter configuration:

// vitest.config.ts
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    reporters: ["default", "langsmith/vitest/reporter"]
  }
});

Complete Vitest documentation →

Comparison: Jest vs Vitest

FeatureJestVitest
Test APIIdenticalIdentical
Custom Matchers
Evaluators
Reporter ConfigNot requiredRequired in vitest.config.ts
PerformanceGoodFaster (Vite-based)
Watch ModeBuilt-inBuilt-in
Parallel Tests✓ (better performance)
ES ModulesRequires configurationNative support
TypeScriptRequires ts-jestNative support

Recommendation: Both provide identical LangSmith functionality. Choose based on:

  • Use Jest if you already use Jest or have Jest-based CI/CD pipelines
  • Use Vitest if you use Vite, want faster tests, or prefer native ES modules/TypeScript

Shared Features

Both frameworks provide:

Custom Matchers for LLM Outputs

// Relative closeness (normalized edit distance)
expect(output).toBeRelativeCloseTo("Expected text", { threshold: 0.8 });

// Absolute closeness (raw edit distance)
expect(output).toBeAbsoluteCloseTo("Expected text", { threshold: 5 });

// Semantic similarity (embeddings-based)
expect(output).toBeSemanticCloseTo("Expected meaning", { threshold: 0.85 });

// Custom evaluators
expect(output).evaluatedBy(customEvaluator);

Feedback and Output Logging

// Log evaluation feedback
logFeedback({
  key: "accuracy",
  score: 0.95,
  comment: "High quality output"
});

// Log intermediate outputs
logOutputs({ step1: result1, step2: result2 });

Reusable Evaluators

import { wrapEvaluator } from "langsmith/jest";  // or "langsmith/vitest"

const customEvaluator = wrapEvaluator((args) => {
  const { input, output, expected } = args;
  return {
    key: "custom_metric",
    score: calculateScore(output, expected),
    comment: "Evaluation comment"
  };
});

Quick Examples

Basic Test

// Jest
import { test } from "langsmith/jest";

// Vitest
import { test } from "langsmith/vitest";

// Identical API
test(
  "test name",
  {
    input: { data: "input" },
    expected: { result: "output" }
  },
  async (input) => {
    return { result: processData(input.data) };
  }
);

With Custom Evaluators

import { test, expect, wrapEvaluator } from "langsmith/jest";  // or vitest

const qualityEvaluator = wrapEvaluator((args) => ({
  key: "quality",
  score: args.output.score > 0.8 ? 1 : 0
}));

test(
  "quality check",
  {
    input: { prompt: "Test" },
    evaluators: [qualityEvaluator]
  },
  async (input) => {
    const result = await generate(input.prompt);
    expect(result).evaluatedBy(qualityEvaluator);
    return result;
  }
);

Dataset Creation

test(
  "translation test",
  {
    input: { text: "Hello", lang: "es" },
    expected: "Hola",
    datasetName: "translation-tests",  // Automatically creates dataset
    projectName: "translation-eval"
  },
  async (input) => {
    return await translate(input.text, input.lang);
  }
);

Next Steps

Related Documentation