Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
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.
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
Jest is a popular testing framework with a focus on simplicity. It provides a similar set of features to Vitest, including mocking, snapshot testing, and watch mode. However, Jest is often considered slower than Vitest due to its heavier architecture.
Mocha is a flexible testing framework for Node.js and the browser. It's known for its simplicity and support for various assertion libraries. Unlike Vitest, Mocha does not include a built-in assertion library or mocking utilities, requiring additional packages for these features.
AVA is a test runner for Node.js with a concise API, detailed error output, and process isolation for concurrent test execution. It differs from Vitest in its approach to concurrency and its minimalistic design.
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not require a DOM and comes with an assertion library. Jasmine is less modern compared to Vitest and does not support ES modules natively.
A blazing fast test runner powered by Vite.
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
vitest
will read your root vite.config.ts
when it present to match with the plugins and setup as your Vite app. If you want to it to have a different configuration for testing, you could either:
vitest.config.ts
, which will have the higher priority--config
option to CLI, e.g. vitest --config ./path/to/vitest.config.ts
process.env.VITEST
to conditionally apply differnet configuration in vite.config.ts
To configure vitest
itself, add test
property in your Vite config
// vite.config.ts
import { defineConfig } from 'vite'
export default defineConfig({
test: {
// ...
}
})
By default, vitest
does not provide global APIs for explicitness. If you prefer to use the APIs globally like Jest, you can pass the --global
option to CLI or add global: true
in the config.
// vite.config.ts
import { defineConfig } from 'vite'
export default defineConfig({
test: {
global: true
}
})
To get TypeScript working with the global APIs, add vitest/global
to the types
filed in your tsconfig.json
// tsconfig.json
{
"compilerOptions": {
"types": [
"vitest/global"
]
}
}
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)
})
})
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)
})
})
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')
})
MIT License © 2021 Anthony Fu
FAQs
Next generation testing framework powered by Vite
The npm package vitest receives a total of 5,750,241 weekly downloads. As such, vitest popularity was classified as popular.
We found that vitest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.