Skip to content
Vitest 2
Main Navigation GuideAPIConfigBrowser ModeAdvanced
2.1.9
1.6.1
0.34.6

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

Appearance

Sidebar Navigation

Why Vitest

Getting Started

Features

Workspace

CLI

Test Filtering

Reporters

Coverage

Snapshot

Mocking

Testing Types

Vitest UI

In-source Testing

Test Context

Environment

Extending Matchers

IDE Integration

Debugging

Comparisons

Migration Guide

Common Errors

Profiling Test Performance

Improving Performance

On this page

In-Source Testing ​

Vitest provides a way to run tests within your source code along side the implementation, similar to Rust's module tests.

This makes the tests share the same closure as the implementations and able to test against private states without exporting. Meanwhile, it also brings a closer feedback loop for development.

WARNING

This guide explains how to write tests inside your source code. If you need to write tests in separate test files, follow the "Writing Tests" guide.

Setup ​

To get started, put a if (import.meta.vitest) block at the end of your source file and write some tests inside it. For example:

ts
// src/index.ts

// the implementation
export function add(...args: number[]) {
  return args.reduce((a, b) => a + b, 0);
}

// in-source test suites
if (import.meta.vitest) {
  const { it, expect } = import.meta.vitest;
  it('add', () => {
    expect(add()).toBe(0);
    expect(add(1)).toBe(1);
    expect(add(1, 2, 3)).toBe(6);
  });
}

Update the includeSource config for Vitest to grab the files under src/:

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

export default defineConfig({
  test: {
    includeSource: ['src/**/*.{js,ts}'], 
  },
});

Then you can start to test!

bash
$ npx vitest

Production Build ​

For the production build, you will need to set the define options in your config file, letting the bundler do the dead code elimination. For example, in Vite

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

export default defineConfig({
  test: {
    includeSource: ['src/**/*.{js,ts}'],
  },
  define: {
    'import.meta.vitest': 'undefined', 
  }, 
});

Other Bundlers ​

unbuild
ts
// build.config.ts
import { defineBuildConfig } from 'unbuild';

export default defineBuildConfig({
  replace: {
    'import.meta.vitest': 'undefined', 
  }, 
  // other options
});

Learn more: unbuild

rollup
ts
// rollup.config.js
import replace from '@rollup/plugin-replace'; 

export default {
  plugins: [
    replace({
      'import.meta.vitest': 'undefined', 
    }), 
  ],
  // other options
};

Learn more: Rollup

TypeScript ​

To get TypeScript support for import.meta.vitest, add vitest/importMeta to your tsconfig.json:

json
// tsconfig.json
{
  "compilerOptions": {
    "types": [
      "vitest/importMeta"
    ]
  }
}

Reference to examples/in-source-test for the full example.

Notes ​

This feature could be useful for:

  • Unit testing for small-scoped functions or utilities
  • Prototyping
  • Inline Assertion

It's recommended to use separate test files instead for more complex tests like components or E2E testing.

Pager
Previous pageVitest UI
Next pageTest Context

Released under the MIT License.

Copyright (c) 2021-Present Vitest Team

https://v2.vitest.dev/guide/in-source

Released under the MIT License.

Copyright (c) 2021-Present Vitest Team