Giter Site home page Giter Site logo

nvh95 / vitest-preview Goto Github PK

View Code? Open in Web Editor NEW
308.0 3.0 13.0 597 KB

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

Home Page: https://www.vitest-preview.com

License: MIT License

JavaScript 16.11% TypeScript 61.39% HTML 5.96% AppleScript 16.55%
debug frontend productivity react svelte testing vite vitest vue

vitest-preview's Introduction

Vitest Preview Logo

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

Vitest Preview Demo

Try Vitest Preview Online. No downloads needed!

All Contributors

npm version Best of JS PRs Welcome Mentioned in Awesome Jest

Try Vitest Preview now

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, it's blazing fast and easy to use.

vitest-preview is a younger sibling of jest-preview with the same idea and to solve the same problem. If you are writing tests using Jest, give it a try.

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

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:

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

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

// vite.config.js
export default defineConfig({
  test: {
+    setupFiles: './src/test/setup.ts',
  },
});
// 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:

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

Update .gitignore

Update your .gitignore

// .gitignore
+.vitest-preview

Usage

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

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

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

Open the Vitest Preview Dashboard by running the CLI command (updated in Configuration):

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.

Examples

Is there a similar library for Jest

Yes, it is. It's Jest Preview.

Star history

Star History Chart

Contributing

Please see the contribution guide at CONTRIBUTING.md.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Hung Viet Nguyen
Hung Viet Nguyen

πŸ’»
Andre
Andre

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

🌍

This project follows the all-contributors specification. Contributions of any kind welcome!

License

This is open source software

MIT

Support

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

vitest-preview's People

Contributors

allcontributors[bot] avatar imgbot[bot] avatar nvh95 avatar rivertwilight avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vitest-preview's Issues

Fix stackblitz example

Describe the bug

When clicking Try now the stackblitz instance doesn't seem to be able to build the modules, reinstalling the module manually works https://stackblitz.com/edit/vitest-preview-fh8b6e

~/projects/vitest-preview
❯ npm install && npm start
warn preInstall No description field
warn preInstall No repository field
warn preInstall No license field
β”Œ [1/4] πŸ”  Resolving dependencies
β”” 𐄂 Failed
ERR!: request to https://nr.staticblitz.com/vitest-preview failed, reason: socket hang up

error Command failed with exit code 1

~/projects/vitest-preview

Screenshots

Screen Shot 2022-10-31 at 12 15 17 PM

Reproduce

Go to http://bit.ly/try-vitest-preview, try to use the example

Please provide a minimal reproduction for the issue. Thanks.

You can use one of the following options to reproduce the issue:

Expected behavior

  • The example works as expected

Environment (please complete the following information)

  • OS: macOS 12.5.1
  • Browser: Chrome 106.0.5249.119

Additional context

Add the ability to run the server from code

We are developing an analogue of create-react-app in our company. And we want to add vitest-preview to it. There is currently no way to run the server from code.

Sample:

import { startServer } from 'vitest-preview/node';

startServer();

[Feature request] autoPreview mode

Is your feature request related to a problem? Please describe.

Not related to a problem.

Describe the solution you'd like

A way to setup autoPreview.

Describe how should we implement this feature

Same functionality as jest-preview

Describe alternatives you've considered

Attempt to setup jest-preview with vitest,

Additional context

N/A

Add `vitest-preview setup` CLI

Is your feature request related to a problem? Please describe.

When integrating vitest-preview, beside installing pnpm add -D vitest-preview, users need to do some other things like:

  • Set vitest config css: true
  • Setup global CSS
  • Add vitest-preview to package.json's scripts
  • Update .gitignore
  • Add debug() to a test file

Describe the solution you'd like

  • Add a new CLI vitest-preview setup
  • Update the docs to recommend new approach

Describe how should we implement this feature

Consider https://github.com/nvh95/jest-preview/blob/61648e16e455f4eb561c267c9f7d897c953e6dcb/cli/index.js

Additional context

People usually integrate vitest-preview by running pnpm add -D vitest-preview and it's very easy for them to forget to config a thing.

Try now and try online does not work

Latest Firefox browser.

Try it now is not working. Clicking count does nothing, editing App.test.tsx does nothing new, runs the test, that is all. There is no test preview and debug does nothing. Lots of errors in the console.

Uncaught (in promise) Error: getTranslators: detection is already running
    at Zotero.Translate.Web.<anonymous> (translate.js:1091:45)
    at Zotero.Translate.Web.getTranslators (promise.js:36:17)
    at Object.init (inject.js:105:25)
    at Object.historyChanged (inject.js:705:21)
    at messageListener (messaging_inject.js:122:50)
    at messaging_inject.js:132:6

When the 'root' configuration is set to something other than the default, hot reload doesn't work

Potential solution

I was able to get this to work by adding { root: '.' } in previewServer.mjs to the createServer call
image

Describe the bug

Hot reload doesn't work if the 'root' option is configured to something other than the default in vite.config.ts.

Reproduce

Take the example project, change the 'root' field in vite.config.ts to something else (like "test-root"). Change the 'root' field to '.' in the test.root option so that tests still work. Run vitest-preview and run a test.

https://stackblitz.com/edit/vitest-preview-9hat82?file=src%2FApp.test.tsx

Expected behavior

vitest-preview should hot reload, but it doesn't. You only see the results after a refresh.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.