TypeScript Snapshots Language Service Plugin

1.7.0 · active · verified Sun Apr 19

The `typescript-snapshots-plugin` enhances the TypeScript language service to provide deep IDE support for Jest or similar snapshot testing frameworks. It integrates features directly into the editor, such as displaying snapshot content on quick info (hover) for `toMatchSnapshot` calls and enabling "Go to Definition" navigation from the test call to its corresponding `.snap` file. The plugin also supports dynamically constructed test names, albeit currently limited to constant expressions for accurate linking. It is installed as a development dependency and configured within the `plugins` array of your `tsconfig.json`. The current stable version is 1.7.0, indicating active maintenance and development. Its key differentiator is streamlining the snapshot testing workflow by bringing snapshot content and navigation directly into the TypeScript editor experience, reducing context switching and improving developer productivity.

Common errors

Warnings

Install

Quickstart

Demonstrates `typescript-snapshots-plugin` setup in `package.json` and `tsconfig.json`, with a sample Jest test file. Highlights hover-to-view and go-to-definition features, and configures VSCode for optimal plugin usage.

{
  "name": "my-snapshot-project",
  "version": "1.0.0",
  "devDependencies": {
    "jest": "^29.0.0",
    "ts-jest": "^29.0.0",
    "typescript": "^5.0.0",
    "typescript-snapshots-plugin": "^1.7.0"
  },
  "scripts": {
    "test": "jest"
  }
}

// tsconfig.json
{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": [
    "src/**/*"
  ],
  "plugins": [{
    "name": "typescript-snapshots-plugin",
    "useJSTagsForSnapshotHover": true // Enable for better VSCode markdown rendering
  }]
}

// src/my-component.test.ts
import { test, expect, describe } from '@jest/globals';

describe('MyComponent rendering', () => {
  test('should match the snapshot', () => {
    const data = {
      id: 123,
      name: 'Example Component',
      settings: {
        theme: 'dark',
        fontSize: 14
      }
    };
    // Hover over 'toMatchSnapshot' to see the snapshot content inline.
    // Go to Definition (F12) on 'toMatchSnapshot' to navigate to the .snap file.
    expect(data).toMatchSnapshot();
  });

  const componentName = 'DynamicWidget'; // Must be a constant for dynamic names
  test(`renders a ${componentName} correctly`, () => {
    const widgetData = { type: componentName, config: { enabled: true } };
    expect(widgetData).toMatchSnapshot();
  });
});

// To ensure the plugin works in VSCode:
// 1. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
// 2. Search for "TypeScript: Select TypeScript Version...".
// 3. Choose "Use Workspace Version" to ensure VSCode uses your project's TypeScript compiler.

view raw JSON →