5

I am using the VSCode Jest extension.

One of the features it gives you, is running your tests in the IDE and giving inline error messages.

enter image description here

The error it is giving me here TypeError: this.children.flatMap is not a function is the Jest tests running in the wrong node runtime - .flatMap is included in versions of node >11.

When I run Jest from command line, I also get this error, which I've fixed by adding a .nvmrc to my project, and using nvm to change the node version.

How do I tell Jest/VSCode to simillarly change its Node runtime?

3
  • One solution is to change the node version in terminal and start vscode with the code command. But this is a little annoying as I typically start VScode with a taskbar shortcut Oct 13, 2019 at 8:54
  • was adding .nvmrc help? If yes could you add it as an answer?
    – otong
    Sep 7, 2020 at 8:08
  • @otong adding .nvmrc does not help, mine is already there and it still uses old node version Sep 28, 2023 at 18:53

2 Answers 2

4

You can add in you setting.json the next lines:

"jest.shell": "/bin/zsh",
"jest.jestCommandLine": "source ~/.zshrc && nvm use 14.15 && yarn test",

that works if you're using zsh

2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 12, 2022 at 13:03
  • Sounds like you can also use "jest.jestCommandLine": "source ~/.zshrc && nvm exec $* && yarn test" if you have any nvm auto version switch enabled for your zsh/terminal, see github.com/jest-community/vscode-jest/issues/… Sep 28, 2023 at 19:06
0

This issue is tracked within the jest-community/vscode-jest#72. As per this comment, the best solution currently seems to be to update your system to use the Node version specified within your .nvmrc by default:

nvm alias default $(cat .nvmrc)

Obviously this is not ideal as it affects your entire system and will cause issues if you have multiple projects using different versions of Node.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .