Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gcoguiec/vitest-double
👥 Typed doubles for Vitest.
https://github.com/gcoguiec/vitest-double
double vitest
Last synced: about 1 month ago
JSON representation
👥 Typed doubles for Vitest.
- Host: GitHub
- URL: https://github.com/gcoguiec/vitest-double
- Owner: gcoguiec
- License: bsd-2-clause
- Created: 2024-05-13T13:08:08.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-09-06T15:12:57.000Z (4 months ago)
- Last Synced: 2024-11-30T22:33:11.294Z (about 1 month ago)
- Topics: double, vitest
- Language: TypeScript
- Homepage: https://github.com/gcoguiec/vitest-double
- Size: 92.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
vitest-double
Typed doubles for Vitest.
## Table of Contents
- [Overview](#overview)
- [Getting Started](#getting-started)
- [Usage](#usage)
- [License](#license)## Overview
`vitest-double` is a tiny helper that allows you to create the full or partial "double" of a typed object while keeping its original type signature.
## About the name
Double as in doppelgänger and 'vitesse double' also means 'double speed' in French, It's a _double_ pun.
## Getting started
To add vitest-double to your project:
```bash
npm install -D vitest-double
``````bash
yarn add -D vitest-double
``````bash
pnpm add -D vitest-double
``````bash
bun add -D vitest-double
```## Usage
Calling `double()` will create a double instance with correct type signature:
```ts
import { double } from 'vitest-double';// Mocking the Navigator browser object:
const navigator = double();
```You can use Vitest's usual bells and whistles to mock a double partially:
```ts
import { vi, expect } from 'vitest';
import { double } from 'vitest-double';// Example with HTMLElement type:
const element = double({
hidden: false,
focus: vi.fn(),
removeAttribute: vi.fn(),
removeEventListener: vi.fn(),
setAttribute: vi.fn(),
addEventListener: vi.fn()
});// [something calls focus() on HTML element...]
expect(element.focus).toHaveBeenCalled();
expect(element).not.toHaveDirtyProperty('hidden');
```Doubles can be used to mock fairly complex structures; here is an example of VSCode's API:
```ts
// vscode-shim.ts
import type * as vscode from 'vscode';import { double } from 'vitest-double';
export const workspace = double({
getConfiguration: vi.fn(() =>
double({
get: vi.fn(() => {})
})
)
});export const window = double({
createOutputChannel: double(() =>
double({
clear: vi.fn(),
onDidChangeLogLevel: vi.fn()
})
)
});
```Register your shim in your Vite or Vitest configuration file and use it in your specs:
```ts
resolve: {
alias: {
vscode: resolve(process.cwd(), 'vscode-shim.ts');
}
}
```You can use doubles to mock pretty much every third-party API.
## License
This project is licensed under [BSD 2-Clause](https://spdx.org/licenses/BSD-2-Clause.html).