vscode-jest icon indicating copy to clipboard operation
vscode-jest copied to clipboard

Feature - Run jest with specified node version from .nvmrc file

Open Vadorequest opened this issue 8 years ago • 12 comments

Since the plugins starts without allowing the user to specify what version of node should be used, and use the default selected version in its own process, it is not yet possible to select what version of node should be used.

In my case, my default nvm node version is 4.x. But my project has a .nvmrc with 6.9.1 in it. But Jest uses the default one. (which makes the whole extension unusable since it crashed instantly)

Would it be possible to either specify what path of node we want Jest to use, or, more simply, run a nvm use before the plugin starts if there is a .nvmrc file in the root folder?

Vadorequest avatar Feb 01 '17 15:02 Vadorequest

This seems reasonable to me, it'll have to go in jest-editor-support ( which has just had some changes related to this https://github.com/facebook/jest/pull/2817 )

orta avatar Feb 07 '17 13:02 orta

Definitely needed, current node version management seems difficult.

Also please update README that to change node version for tests one has to do nvm alias default $REQUIRED_NODE_VERSION

liqwid avatar Oct 16 '17 08:10 liqwid

PRs welcome 👍

orta avatar Oct 16 '17 11:10 orta

I know this issue is pretty old, is there a proposed solution or suggested workaround on how to get the watch test use the right node version using nvm?

For what it's worth, when using the debug launch lens, it uses the node version specified in in launch.json, so that works. But the watched test seems to not be using launch..?

roni-frantchi avatar Jul 19 '19 13:07 roni-frantchi

For what it's worth, when using the debug launch lens, it uses the node version specified in in launch.json, so that works. But the watched test seems to not be using launch..?

Same. @orta how do I get jest in watch mode to use the node version specified in launch.json?

Jamelle-Boose avatar Jan 22 '20 10:01 Jamelle-Boose

I am also looking for a solution to use this plugin with nvm...

PixelDuck avatar Jan 19 '21 11:01 PixelDuck

same here, watch not working

TENsaga avatar Jan 26 '21 18:01 TENsaga

During debugging, this extension uses vscode's debug infrastructure that used launch.json; during the regular test run/watch it spawns a node process instead. It would default to the node version from your default shell... I use nvm and it works fine. Do you guys actually want to use a different node version than your default nvm?

connectdotz avatar Jan 29 '21 15:01 connectdotz

During debugging, this extension uses vscode's debug infrastructure that used launch.json; during the regular test run/watch it spawns a node process instead. It would default to the node version from your default shell... I use nvm and it works fine. Do you guys actually want to use a different node version than your default nvm?

Yes, I'd want to use the version specified in the .nvmrc file, not the shell default.

mjpieters avatar Jan 30 '21 16:01 mjpieters

I got it working using a startup script:

  1. Create a node.sh wrapper script to set up the Node version from .nvmrc. (Update shebang and source line to match your shell.)
    #!/bin/zsh
    source ~/.zshrc
    nvm exec $*
    
  2. Update .vscode/settings.json to use the wrapper. (Update jest command to match how you normally run it.)
    {
        "jest.jestCommandLine": "./node.sh npx jest"
    }

sampalmer avatar Oct 09 '21 04:10 sampalmer

1. Create a `node.sh` wrapper script to set up the Node version from `.nvmrc`. (Update shebang and `source` line to match your shell.)
   ```shell
   #!/bin/zsh
   source ~/.zshrc
   nvm exec $*
   ```

Instead of creating your own script, you can use ~/.nvm/nvm-exec for this:

{
    "jest.jestCommandLine": "~/.nvm/nvm-exec npx test"
}

mjpieters avatar Apr 05 '23 17:04 mjpieters

If you use zsh plus nvm auto version switch, you can simply:

"jest.jestCommandLine": "source ~/.zshrc && nvm exec $* && yarn test"

Of course switching to npm run test, npx test, as you wish, instead of yarn test.

Also not sure if this is needed but it's possible:

"jest.shell": "/bin/zsh",

giovannipds avatar Sep 28 '23 19:09 giovannipds