Socket
Socket
Sign inDemoInstall

vitest

Package Overview
Dependencies
12
Maintainers
1
Versions
357
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

vitest


Version published
Weekly downloads
4.9M
decreased by-3.82%
Maintainers
1
Install size
46.1 MB
Created
Weekly downloads
 

Package description

What is vitest?

Vitest is a blazing fast unit test framework powered by Vite. It is designed to provide a delightful testing experience with features like native ES modules support, TypeScript support, and a rich API for running and organizing tests.

What are vitest's main functionalities?

Unit Testing

Vitest allows you to write and run unit tests for your JavaScript/TypeScript code. The example shows a simple test suite for a math operation.

import { describe, it, expect } from 'vitest';
describe('math', () => {
  it('should add numbers correctly', () => {
    expect(1 + 1).toBe(2);
  });
});

Mocking

Vitest provides a built-in mocking utility, allowing you to create mock functions and spy on their behavior.

import { vi } from 'vitest';
const mockFunction = vi.fn(() => 42);
mockFunction();
expect(mockFunction).toHaveBeenCalled();

Snapshot Testing

Vitest supports snapshot testing, which is useful for ensuring your UI does not change unexpectedly. It saves the 'snapshot' of the output and compares it against future test runs.

import { expect, test } from 'vitest';
test('snapshot test', () => {
  const user = { id: 1, name: 'John Doe' };
  expect(user).toMatchSnapshot();
});

Code Coverage

Vitest can generate code coverage reports to help you understand which parts of your codebase are covered by tests.

// Run Vitest with the --coverage flag to generate code coverage reports
// vitest run --coverage

Watch Mode

Vitest's watch mode re-runs tests when it detects changes in the test files or the corresponding source files, providing instant feedback during development.

// Run Vitest in watch mode using the --watch flag
// vitest --watch

Other packages similar to vitest

Readme

Source

vitest

NPM version

A blazing fast test runner powered by Vite.

Features

  • Vite's config, transformers, resolvers, and plugins. Powered by vite-node
  • Jest Snapshot
  • Chai for assertions
  • Sinon for mocking
  • Async suite / test, top level await
  • ESM friendly
  • Out-of-box TypeScript support
  • Suite and Test filtering (skip, only, todo)
import { it, describe, expect, assert } from 'vitest'

describe('suite name', () => {
  it('foo', () => {
    assert.equal(Math.sqrt(4), 2)
  })

  it('bar', () => {
    expect(1 + 1).eq(2)
  })

  it('snapshot', () => {
    expect({ foo: 'bar' }).toMatchSnapshot()
  })
})
$ npx vitest

Filtering

Skipping suites and tasks

Use .skip to avoid running certain suites or tests

describe.skip('skipped suite', () => {
  it('task', () => {
    // Suite skipped, no error
    assert.equal(Math.sqrt(4), 3)
  })
})

describe('suite', () => {
  it.skip('skipped task', () => {
    // Task skipped, no error
    assert.equal(Math.sqrt(4), 3)
  })
})

Selecting suites and tests to run

Use .only to only run certain suites or tests

// Only this suite (and others marked with only) are run
describe.only('suite', () => {
  it('task', () => {
    assert.equal(Math.sqrt(4), 3) 
  })
})

describe('another suite', () => {
  it('skipped task', () => {
     // Task skipped, as tests are running in Only mode
    assert.equal(Math.sqrt(4), 3)
  })

  it.only('task', () => {
     // Only this task (and others marked with only) are run
    assert.equal(Math.sqrt(4), 2)
  })
})

Unimplemented suites and tests

Use .todo to stub suites and tests that should be implemented

 // An entry will be shown in the report for this suite
describe.todo('unimplemented suite')

// An entry will be shown in the report for this task
describe('suite', () => {
  it.todo('unimplemented task')
})

TODO

  • Reporter & Better output
  • Task filter
  • Mock
  • Parallel Executing
  • CLI Help
  • JSDom
  • Watch
  • Coverage

Sponsors

License

MIT License © 2021 Anthony Fu

FAQs

Last updated on 04 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc