KC's Workspace
    Preparing search index...
    Index

    Constructors

    Properties

    distPath: string

    A path to the built Vitest directory. This is usually a folder in node_modules.

    logger: Logger

    The logger instance used to log messages. It's recommended to use this logger instead of console. It's possible to override stdout and stderr streams when initiating Vitest.

    new Vitest('test', {
    stdout: new Writable(),
    })
    packageInstaller: VitestPackageInstaller

    The package installer instance used to install Vitest packages.

    await vitest.packageInstaller.ensureInstalled('@vitest/browser', process.cwd())
    
    projects: TestProject[]

    A list of projects that are currently running. If projects were filtered with --project flag, they won't appear here.

    provide: <T extends never>(key: T, value: ProvidedContext[T]) => void

    Provide a value to the test context. This value will be available to all tests with inject.

    The version control system provider used to detect changed files. This is used with the --changed flag to determine which test files to run. By default, Vitest uses Git. You can provide a custom implementation via experimental.vcsProvider in your config.

    version: string

    Current Vitest version.

    '2.0.0'
    
    watcher: VitestWatcher

    A watcher handler. This is not the file system watcher. The handler only exposes methods to handle changed files.

    If you have your own watcher, you can use these methods to replicate Vitest behaviour.

    version: string

    Accessors

    • get cache(): VitestCache

      Test results and test file stats cache. Primarily used by the sequencer to sort tests.

      Returns VitestCache

    • get config(): ResolvedConfig

      The global config.

      Returns ResolvedConfig

    • get snapshot(): SnapshotManager

      The global snapshot manager. You can access the current state on snapshot.summary.

      Returns SnapshotManager

    • get state(): StateManager
      Experimental

      The global test state manager. The State API is experimental and not subject to semver.

      Returns StateManager

    • get vite(): ViteDevServer

      Global Vite's dev server instance.

      Returns ViteDevServer

    Methods

    • Gracefully cancel the current test run. Vitest will wait until all running tests are finished before cancelling.

      Parameters

      Returns Promise<void>

    • Vitest automatically caches test specifications for each file. This method clears the cache for the given file or the whole cache altogether.

      Parameters

      • OptionalmoduleId: string

      Returns void

    • Closes all projects and their associated resources. This can only be called once; the closing promise is cached until the server restarts.

      Returns Promise<void>

    • Parameters

      • Optionalfilters: string[]
      • Optionaloptions: { staticParse?: boolean; staticParseConcurrency?: number }

      Returns Promise<TestRunResult>

    • Collect tests in specified modules. Vitest will run the files to collect tests.

      Parameters

      Returns Promise<TestRunResult>

    • Creates a coverage provider if coverage is enabled in the config.

      Returns Promise<CoverageProvider | null>

    • Returns void

    • Returns Promise<void>

    • Enable the mode that allows updating snapshots when running tests. This method doesn't run any tests.

      Every test that runs after this method is called will update snapshots. To disable the mode, call resetSnapshotUpdate.

      Returns void

    • Closes all projects and exit the process

      Parameters

      • Optionalforce: boolean

        If true, the process will exit immediately after closing the projects.

      Returns Promise<void>

    • Experimental

      Deletes all Vitest caches, including experimental.fsModuleCache.

      Returns Promise<void>

    • Experimental

      Returns module's diagnostic. If testModule is not provided, selfTime and totalTime will be aggregated across all tests.

      If the module was not transformed or executed, the diagnostic will be empty.

      Parameters

      Returns Promise<SourceModuleDiagnostic>

    • Parameters

      Returns Promise<TestModule>

    • Parameters

      • specifications: TestSpecification[]
      • Optionaloptions: { concurrency?: number }
        • Optionalconcurrency?: number
          os.availableParallelism()
          

      Returns Promise<TestModule[]>

    • Returns the regexp used for the global test name pattern.

      Returns RegExp | undefined

    • Get test specifications associated with the given module. If module is not a test file, an empty array is returned.

      Note: this method relies on a cache generated by globTestSpecifications. If the file was not processed yet, use project.matchesGlobPattern instead.

      Parameters

      • moduleId: string

        The module ID to get test specifications for.

      Returns TestSpecification[]

    • Parameters

      • name: string

      Returns TestProject

    • Get global provided context.

      Returns ProvidedContext

    • Returns the list of test files that match the config and filters.

      Parameters

      • Optionalfilters: string[]

        String filters to match the test files

      Returns Promise<TestSpecification[]>

    • Return project that has the root (or "global") config.

      Returns TestProject

    • Returns the seed, if tests are running in a random order.

      Returns number | null

    • Glob test files in every project and create a TestSpecification for each file and pool.

      Parameters

      • Optionalfilters: string[]

        String filters to match the test files.

      Returns Promise<TestSpecification[]>

    • Import a file using Vite module runner. The file will be transformed by Vite and executed in a separate context.

      Type Parameters

      • T

      Parameters

      • moduleId: string

        The ID of the module in Vite module graph

      Returns Promise<T>

    • Returns Promise<void>

      use standalone() instead

    • Invalidate a file in all projects.

      Parameters

      • filepath: string

      Returns void

    • Returns Promise<void>

    • Check if the project with a given name should be included.

      Parameters

      • name: string

      Returns boolean

    • Merge reports from multiple runs located in the specified directory (value from --merge-reports if not specified).

      Parameters

      • Optionaldirectory: string

      Returns Promise<TestRunResult>

    • Register a handler that will be called when the test run is cancelled with vitest.cancelCurrentRun.

      Parameters

      Returns () => void

    • Register a handler that will be called when the server is closed.

      Parameters

      Returns void

    • Register a handler that will be called when a file is changed. This callback should return true of false indicating whether the test file needs to be rerun.

      Parameters

      Returns void

      const testsToRun = [resolve('./test.spec.ts')]
      vitest.onFilterWatchedSpecification(specification => testsToRun.includes(specification.moduleId))
    • Register a handler that will be called when the server is restarted due to a config change.

      Returns void

    • Register a handler that will be called when the tests are rerunning.

      Parameters

      Returns void

    • Rerun files and trigger onWatcherRerun, onWatcherStart and onTestsRerun events.

      Parameters

      • specifications: TestSpecification[]

        A list of specifications to run.

      • OptionalallTestsRun: boolean

        Indicates whether all tests were run. This only matters for coverage.

      Returns Promise<TestRunResult>

    • Resets the global test name pattern. This method doesn't run any tests.

      Returns void

    • Disable the mode that allows updating snapshots when running tests.

      Returns void

    • Runs tests for the given file paths. This does not trigger onWatcher* events.

      Parameters

      • filepaths: string[]

        A list of file paths to run tests for.

      • OptionalallTestsRun: boolean

        Indicates whether all tests were run. This only matters for coverage.

      Returns Promise<TestRunResult>

    • Run tests for the given test specifications. This does not trigger onWatcher* events.

      Parameters

      • specifications: TestSpecification[]

        A list of specifications to run.

      • OptionalallTestsRun: boolean

        Indicates whether all tests were run. This only matters for coverage.

      Returns Promise<TestRunResult>

    • Set the global test name pattern to a regexp. This method doesn't run any tests.

      Parameters

      Returns void

    • Should the server be kept running after the tests are done.

      Returns boolean

    • Initialize reporters and the coverage provider. This method doesn't run any tests. If the --watch flag is provided, Vitest will still run changed tests even if this method was not called.

      Returns Promise<void>

    • Initialize reporters, the coverage provider, and run tests. This method can throw an error:

      • FilesNotFoundError if no tests are found
      • GitNotFoundError if --related flag is used, but git repository is not initialized
      • Error from the user reporters

      Parameters

      • Optionalfilters: string[]

        String filters to match the test files

      Returns Promise<TestRunResult>

    • Update snapshots in specified files. If no files are provided, it will update files with failed tests and obsolete snapshots.

      Parameters

      • Optionalfiles: string[]

        The list of files on the file system

      Returns Promise<TestRunResult>

    • If there is a test run happening, returns a promise that will resolve when the test run is finished.

      Returns Promise<void>