vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
13.1k stars 1.18k forks source link

vite-node: Support interactive console (REPL) #6919

Open rChaoz opened 1 day ago

rChaoz commented 1 day ago

Clear and concise description of the problem

I like to test part of my code in the Node interactive console, i.e. REPL:

$ node
> const { myFunc } = await import("$lib/my/file")
> myFunc(123)

However, this of course does not work because it doesn't resolve stuff (like the "$lib" alias, virtual modules etc.). For this, I use vite-node. But currently this only works with files - I have to create a file, run it with vite-node, repeat.

Suggested solution

Allow running vite-node without arguments to enter interactive mode (REPL).

Alternative

Keep using files instead.

Additional context

No response

Validations

hi-ogawa commented 15 hours ago

This is probably doable. I wonder if there's any tools already doing this. I have a prototype based on Vite 6's module runner in https://github.com/hi-ogawa/vite-environment-examples/tree/main/examples/browser-cli.

To make it work like node's repl, we'll need more proper transforms to handle declarations let, const and also add return to the last expression statement. I just found Node does something like this https://github.com/nodejs/node/blob/746b17e1a5a9d09294b4c993a0319be6e5c39837/lib/repl.js#L432