firsttris / vscode-jest-runner

Simple way to run or debug one or more tests from context menu, codelens or command plalette
https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner
MIT License
265 stars 124 forks source link

Auto-detect project path? #135

Closed cdauth closed 3 years ago

cdauth commented 3 years ago

I'm new to vscode and this extension, so I'm not sure if I'm missing something.

My workspace contains a Java backend and a TypeScript/JavaScript frontend. The frontend is in folder scroll-documents-ui/src/main/frontend. Getting this extension to work wasn't easy, as it was not clear from the documentation which config option I would have to set:

The main pain points that I see here are:

I'm wondering why this extension is not detecting the project path automatically? My guess would be that it should be the closest ancestor directory to the test for which a node_modules/.bin/jest sub-path exists.

firsttris commented 3 years ago

hey @cdauth

i think this feature is currently not implemented.

we almost had it implemented but it caused other setups to fail.

read more about it here https://github.com/firsttris/vscode-jest-runner/issues/124

berndartmueller commented 3 years ago

For anyone who's interested, here's a simple shell script which I use for a Yarn workspaces project:

# Extract working directory based on jest config file

# Path to Jest config file supplied by VSCode Jest Runner
jest_config=$3

# Remove part after last slash
cwd=$(echo "$jest_config" | sed 's|\(.*\)/.*|\1|')

cd "$cwd"

command="node '$cwd/node_modules/.bin/jest' '$1' -c $jest_config -t '$5' $6"

eval "$command"

cd ../..

How to use

  1. Put script into ./bin/jest.sh in the root folder of your project
  2. Add "jestrunner.jestCommand": ". bin/jest.sh" to your VSCode settings.json
  3. Use VSCode Jest runner as usual end enjoy your working tests :)

This script is my opinionated way to auto-detect the working directory of the test file. It can be improved to be more "smart" about how to find the CWD (e.g. traverse the file tree looking for a package.json,..)

Tohsig commented 3 years ago

Thanks for suggesting that approach, @berndartmueller! Just one note, I think you accidentally dropped a dot: "jestrunner.jestCommand": ". .bin/jest.sh"

On the off chance it helps someone else, my command line currently looks like this: command="yarn test:unit '$1' -c $jest_config -t '$5' $6"

Working like a charm.

berndartmueller commented 3 years ago

@Tohsig You’re welcome!

This dot is actually on purpose. See: https://stackoverflow.com/a/16011496

Tohsig commented 3 years ago

@berndartmueller Sorry, my last comment was poorly worded. I meant your instructions just have a small typo.

Should be .bin/jest.sh, given the path in step 1.

firsttris commented 3 years ago

Hey guys, for me and many others the extension already works well with yarn workspaces.

the only thing which is not currently supported is if your jest binrary / config is not in the root folder.

i think the smartest way to solve this would be a todo a reverse lookup for the jest binrary. To find the hierarchy level where the jest binrary is in the node_modules. This should be the correct folder to start Jest.

TranquilMarmot commented 3 years ago

A reverse lookup would be really nice; in a monorepo we have, jest is installed in each sub-project and not at the root and each subproject has its own jest config in package.json.

Something like this...

project-root
  ├── project-a
  │   ├── package.json
  │   ├── node_modules
  │   └── src/FileA.test.tsx
  ├── project-b
  │   ├── package.json
  │   ├── node_modules
  │   └── src/FileB.test.tsx

The main issue I run into is that separate Jest config lives in each package.json.

When running FileA.test.tsx, I need it to run project-root/project-a/node_modules/.bin/jest from the project-a directory.

When running FileB.test.tsx, I need it to run project-root/project-b/node_modules/.bin/jest from the project-b directory.