Closed cdauth closed 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
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 ../..
"jestrunner.jestCommand": ". bin/jest.sh"
to your VSCode settings.json
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,..)
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.
@Tohsig You’re welcome!
This dot is actually on purpose. See: https://stackoverflow.com/a/16011496
@berndartmueller Sorry, my last comment was poorly worded. I meant your instructions just have a small typo.
jest.sh
under .bin
bin/jest.sh
. Should be .bin/jest.sh
, given the path in step 1.
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.
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.
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:node_modules/.bin/jest
based on my workspace directory, rather than the frontend directory.jestrunner.jestPath
didn't work, because it was running the tests from the workspace directory, rather than the frontend directory. For some reason this caused jest not to be able to parse the test files. Also, a relative path worked forjestrunner.jestPath
despite the documentation specifying that it needs to be an absolute path.jestrunner.projectPath
worked, although only as an absolute path, not as a relative path.The main pain points that I see here are:
jestrunner.projectPath
as an absolute path makes it impossible to share my workspace config with other developers.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.