Store Chat Messages & State Without Managing Infrastructure.Check Out DialogueDB
Skip to content
Back to examples

Generate Jest Tests

This example creates an LLM-powered test generator that produces comprehensive Jest test cases based on a given TypeScript function.

Step 1 - Prompt Template

This prompt encourages high-coverage testing and supports injecting stylistic preferences or test conventions if available.

ts
const PROMPT = `As a senior Typescript developer, you write unit tests with 100% test coverage.
  Below is a function, and you need to write unit tests that cover 100% of the branches and functionality. 
  Think step by step before you start writing the tests. Make sure some of your tests expect failures, or try to trick the function.
  We really need good coverage here.
  {{ #if exampleTests }}
  ## Below is an example of how we write jest tests, how we prefer to mock, etc. 
  {{ exampleTests }}
  {{ /if }}
  ## Here is the function:
  \`\`\`ts
  {{ code }}
  \`\`\``;

Step 2 - LLM Executor Function

The function wraps the llm-exe tools to build a complete execution pipeline for generating test cases in markdown format.

ts
export async function generateTests(sourceCode: string, exampleTests?: string) {
  const llm = useLlm("anthropic.claude-3-7-sonnet");
  const prompt = createChatPrompt<{ code: string; exampleTests?: string }>(
    PROMPT
  );
  const parser = createParser("markdownCodeBlock");
  const testGenExecutor = createLlmExecutor({ llm, prompt, parser });

  return testGenExecutor.execute({ code: sourceCode, exampleTests });
}

Example Usage

ts
import { generateTests } from "<your-file-path>";

const fn = `function sum(a: number, b: number): number {
  if (typeof a !== 'number' || typeof b !== 'number') {
    throw new Error("Invalid input");
  }
  return a + b;
}`;

const response = await generateTests(fn);

console.log(response.code);
/**
 * ```ts
 * it('should add two numbers', () => {
 *   expect(sum(2, 3)).toBe(5);
 * });
 * // More tests...
 * ```
 */

💡 Tip: Pass in exampleTests to guide style or mocking expectations.

Complete File

ts

import {
  createChatPrompt,
  createParser,
  createLlmExecutor,
  useLlm,
} from "llm-exe";

const PROMPT = `As a senior Typescript developer, you write unit tests with 100% test coverage.
  Below is a function, and you need to write unit tests that cover 100% of the branches and functionality. 
  Think step by step before you start writing the tests. Make sure some of your tests expect failures, or try to trick the function.
  We really need good coverage here.
  {{ #if exampleTests }}
  ## Below is an example of how we write jest tests, how we prefer to mock, etc. 
  {{ exampleTests }}
  {{ /if }}
  ## Here is the function:
  \`\`\`ts
  {{ code }}
  \`\`\``;

export async function generateTests(sourceCode: string, exampleTests?: string) {
  const llm = useLlm("anthropic.claude-3-7-sonnet");
  const prompt = createChatPrompt<{ code: string; exampleTests?: string }>(
    PROMPT
  );
  const parser = createParser("markdownCodeBlock");
  const testGenExecutor = createLlmExecutor({ llm, prompt, parser });

  return testGenExecutor.execute({ code: sourceCode, exampleTests });
}