Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/nvh95/vitest-preview

Debug your Vitest tests. Effortlessly. πŸ§ͺπŸ–Όβš‘οΈ
https://github.com/nvh95/vitest-preview

debug frontend productivity react svelte testing vite vitest vue

Last synced: about 2 months ago
JSON representation

Debug your Vitest tests. Effortlessly. πŸ§ͺπŸ–Όβš‘οΈ

Lists

README

        


Vitest Preview Logo


Vitest Preview


Debug your Vitest tests. Effortlessly. πŸ§ͺπŸ–Όβš‘οΈ


Vitest Preview Demo


Try Vitest Preview Online. No downloads needed!

[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-)

[![npm version](https://img.shields.io/npm/v/vitest-preview)](https://www.npmjs.com/package/vitest-preview)
[![Best of JS](https://img.shields.io/endpoint?url=https://bestofjs-serverless.now.sh/api/project-badge?fullName=nvh95%2Fvitest-preview%26since=weekly)](https://bestofjs.org/projects/vitest-preview)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-green.svg)](./CONTRIBUTING.md)
[![Mentioned in Awesome Jest](https://awesome.re/mentioned-badge.svg)](https://github.com/vitejs/awesome-vite#testing)

[![Try Vitest Preview now](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/edit/vitest-preview?file=src%2FApp.test.tsx,README.md)

## Why **vitest-preview**

When writing tests, we usually have to debug by reading the cryptic HTML output on the terminal. Sometimes, they are too complicated to visualize the UI in our heads. `vitest-preview` previews your Vitest tests right in a browser, then you can see your actual UI visually. You can write tests and watch rendered output changes accordingly. Vitest Preview lets you concentrate on tests in the "real world" rather than deciphering HTML code.

`vitest-preview` is built on top of [Vite](https://vitejs.dev), it's blazing fast and easy to use.

`vitest-preview` is a younger sibling of [`jest-preview`](https://github.com/nvh95/jest-preview) with the same idea and to solve the same problem. If you are writing tests using Jest, [give it a try](https://stackblitz.com/edit/jest-preview?file=src%2FApp.test.tsx,README.md).

## Features

- πŸ‘€ Visualize your testing UI in an external browser in milliseconds.
- πŸ”„ Auto reloads the browser when `debug()` is executed.
- πŸ’… Fully support CSS
- πŸŒ„ Support viewing images.

## Installation

```bash
npm install --save-dev vitest-preview
# Or
yarn add -D vitest-preview
pnpm add -D vitest-preview
```

## Configuration

### Process CSS

You need to configure `vitest` to process CSS by:

```diff
// vite.config.js
export default defineConfig({
test: {
+ css: true,
},
});

```

You might want to import your CSS global files in `setupFiles`:

```diff
// vite.config.js
export default defineConfig({
test: {
+ setupFiles: './src/test/setup.ts',
},
});

```

```diff
// src/test/setup.ts
+import './global.css';
+import '@your-design-system/css/dist/index.min.css';
+import 'bootstrap/dist/css/bootstrap.min.css';

```

### Add script vitest-preview

`vitest-preview` has a CLI that opens **Vitest Preview Dashboard** where you can preview your tests' UI. You can update your `package.json` to add a script for more convenience:

```json
"scripts": {
"vitest-preview": "vitest-preview"
},
```

### Update .gitignore

Update your `.gitignore`

```diff
// .gitignore
+.vitest-preview
```

## Usage

Put `debug()` wherever you want to see the UI in your tests.

```diff
+import { debug } from 'vitest-preview';

describe('App', () => {
it('should work as expected', () => {
render();
+ debug();
});
});
```

Open the **Vitest Preview Dashboard** by running the CLI command (updated in [Configuration](#configuration)):

```bash
npm run vitest-preview
# Or
yarn vitest-preview
pnpm vitest-preview
```

Then execute your tests that contain `debug()`. You will see the UI of your tests at [http://localhost:5006](http://localhost:5006).

## Examples

- [react-testing-library](https://github.com/nvh95/vitest-preview/tree/main/examples/react-testing-library)
- [@vue/test-utils](https://github.com/nvh95/vitest-preview/tree/main/examples/vue-test-utils)

## Is there a similar library for Jest

Yes, it is. It's [Jest Preview](https://github.com/nvh95/jest-preview).

## Star history

[![Star History Chart](https://api.star-history.com/svg?repos=nvh95/vitest-preview&type=Date)](https://star-history.com/#nvh95/vitest-preview&Date)

## Contributing

Please see the contribution guide at [CONTRIBUTING.md](./CONTRIBUTING.md).

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):



Hung Viet Nguyen
Hung Viet Nguyen

πŸ’»
Andre
Andre

πŸ’‘
RenΓ© Wang
RenΓ© Wang

🌍

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

## License

![This is open source software](https://user-images.githubusercontent.com/8603085/161439058-98faea42-c6e6-46f4-9ce6-218fad5f3b9a.gif)

MIT

## Support

If you like the project, please consider supporting it by giving a ⭐️ to encourage the author.