Vscode-jest: Run Jest inside sub-folder

Created on 18 Nov 2016  路  27Comments  路  Source: jest-community/vscode-jest

image

I'm getting this error when trying to watch jest:
command 'extension.jest.startJest' not found

So, after I installed the extension, nothing happens:
image

Most helpful comment

Also having that problem. Jest Debug Console says:

Exception raised: Process failed: spawn jest ENOENT

Took me a while, but I finally figured it out:

Final solution

  1. Create new runJest.sh file (in workspace root)
  2. In my case, it contains: cd relative/path/to/project && npm test # NOTE: path containspackage.jsonand have atestscript defined
  3. In VSCode's settings.json, add: "jest.pathToJest": "sh runJest.sh"
  4. Runs super smooth!

Making the Debugger work as well

When pressing "Debug" next to a test, things still error out, so what to do?

As they point out in "How to I debug tests?", you need to add a custom test runner config in launch.json which MUST contain name: "vscode-jest-tests". I added this (and it works!!):

{
    "type": "node",
    "request": "launch",
    "name": "vscode-jest-tests",
    "program": "${workspaceRoot}/projects/todomvc/examples/vanilla-es6/node_modules/jest/bin/jest.js",
    "cwd": "${workspaceRoot}/projects/todomvc/examples/vanilla-es6",
    "args": [
        "--i",
        "--config",
        "jest.config.js"
    ]
}

(NOTE: This even works on Windows if you have Cygwin installed and properly configured)

Problem Solving Process

  1. The official vscode-jest troubleshooting guide tells you to set your workspace's jest.pathToJest setting, however there is no documentation on how its supposed to work.
  2. Looked at the source code and saw one special case returning npm test --, realizing it's actually ill-named: it's a command-line/shell command and not a path.

    • Turns out that jest from the error message (spawn jest ENOENT) refers to the value of pathToJest.

  3. First I tried (in my sample project): "jest.pathToJest": "cd projects/todomvc/examples/vanilla-es6/ && npm test".

    • No dice. It cannot handle chained commands. This post probably would fix that.

    • Also note that VSC project variables/macros (such as ${workspaceFolder}) won't be recognized (at least it did not when I tried, might just be a temporary bug)

  4. Finally came up with final solution mentioned above.

All 27 comments

Jest is running, those white dots represent test that have not been ran yet ( because it only runs in watcher mode ) ( you can see the status in the blue bar at the bottom )

Now that Jest 16 is out, I can do my final polish pass for this extension 馃憤

right, but the dots never turn green/red.
I'm on [email protected]. Maybe that's the issue?

I'm unsure, I'm afraid you'd have to clone and run the project in dev mode to see the JSON being passed around to figure that one out

I am also unable to execute it from the command palette. However if I open the subdirectory for my client code it starts the watcher automatically and runs the tests when I change something.

My guess is that it doesn't work if your client is in a subdirectory, e.g. client/src/. Can you confirm @luisrudge?

The command itself does nothing ( I forgot to remove it ) Jest will always be running in a project that includes the jest module

I see, so the real "issue" is that it should also run when contained in a subdirectory in a monorepo?

Yeah 馃憤

I have none of those to use as an example ( in all of Artsy's codebases )

I am using and older version of this template: https://github.com/serverless/serverless-graphql it has all the frontend code in client/src/ instead of the root folder. But the plugin seems to work with the latest version of the template. 馃檪

Rock 馃憤

might be just be broken when using an older version of Jest, I wanted to add something to ensure a minimum version of 17 at some point

here's the error I'm having with the new version

Exception raised: Process failed: spawn node_modules/.bin/jest ENOENT
Error Parsing :c:\git\oss\project\code\components\Card.spec.js. - TypeError: Cannot read property 'forEach' of undefined

Also getting Exception raised: Process failed: spawn node_modules/.bin/jest ENOENT
I'm using an external jest configuration if it helps.

@luisrudge is that one windows? ( I wonder if I need to do something different on windows ) can you also check to see if node_modules/.bin/jest exists?

@luizbon where is your jest binary? I wanted to get around to making this configurable or be derived from the package.json

I'm on windows indeed. c:\git\oss\project\node_modules\.bin\jest exists

@orta I've checked and node_modules/.bin/jest is there, but I don't have any jest configuration inside the package.json since I'm using an external configuration file.

I have my config inside the package.json file

I still get command 'io.orta.jest.start' not found

even on 1.1.0? Even then, it should auto start and you can click on the status bar to see whats going on
screen shot 2016-11-28 at 18 29 23

I guess, since the command is not found, nothing works. I can't even see the status bar.

After the last update, still not working for me. :(

Then I'd recommend cloning the repo and taking a look at what's going on in debug mode - I'm out of ideas

Or make a sample project that exhibits that behaviour so I can take a look at it

I got a ReferenceError: document is not defined error, which may be what you're seeing - can you check with 1.3.2, think I got it.

As commands are added at runtime, if the extension crashes before they are registered, it would not show - which this crash would have done

command 'io.orta.jest.start' not found 馃槶

I am also getting a similar error where it throws the document is not defined.

image

Ok it's not just me. I miss my green dots.

Also having that problem. Jest Debug Console says:

Exception raised: Process failed: spawn jest ENOENT

Took me a while, but I finally figured it out:

Final solution

  1. Create new runJest.sh file (in workspace root)
  2. In my case, it contains: cd relative/path/to/project && npm test # NOTE: path containspackage.jsonand have atestscript defined
  3. In VSCode's settings.json, add: "jest.pathToJest": "sh runJest.sh"
  4. Runs super smooth!

Making the Debugger work as well

When pressing "Debug" next to a test, things still error out, so what to do?

As they point out in "How to I debug tests?", you need to add a custom test runner config in launch.json which MUST contain name: "vscode-jest-tests". I added this (and it works!!):

{
    "type": "node",
    "request": "launch",
    "name": "vscode-jest-tests",
    "program": "${workspaceRoot}/projects/todomvc/examples/vanilla-es6/node_modules/jest/bin/jest.js",
    "cwd": "${workspaceRoot}/projects/todomvc/examples/vanilla-es6",
    "args": [
        "--i",
        "--config",
        "jest.config.js"
    ]
}

(NOTE: This even works on Windows if you have Cygwin installed and properly configured)

Problem Solving Process

  1. The official vscode-jest troubleshooting guide tells you to set your workspace's jest.pathToJest setting, however there is no documentation on how its supposed to work.
  2. Looked at the source code and saw one special case returning npm test --, realizing it's actually ill-named: it's a command-line/shell command and not a path.

    • Turns out that jest from the error message (spawn jest ENOENT) refers to the value of pathToJest.

  3. First I tried (in my sample project): "jest.pathToJest": "cd projects/todomvc/examples/vanilla-es6/ && npm test".

    • No dice. It cannot handle chained commands. This post probably would fix that.

    • Also note that VSC project variables/macros (such as ${workspaceFolder}) won't be recognized (at least it did not when I tried, might just be a temporary bug)

  4. Finally came up with final solution mentioned above.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kartheektrilochan picture kartheektrilochan  路  28Comments

ngauthier picture ngauthier  路  37Comments

orenmizr picture orenmizr  路  15Comments

dandv picture dandv  路  17Comments

erikns picture erikns  路  16Comments