Closed cdaringe closed 8 months ago
I'll reopen this one for discussion 👍
I think this case is already supported, but I could be wrong.
If you try it with jestrunner.changeDirectoryToWorkspaceRoot
, it will run cd <path>
before running the jest command.
You can see here it is falling back to currentPackagePath
, which upsearches for package.json
:
Based on the code, perhaps the current docs could be improved:
jestrunner.changeDirectoryToWorkspaceRoot | Changes directory to workspace root before executing the test -- | --I think this setting should really be:
Changes directory to the resolved project path. The fallback order is
jestrunner.projectPath
, falling back to the nearestpackage.json
, falling back to the workspace root.
Does this cover your use case? Thanks
Problem
In monorepos, some modules are only available in the sub-package under test, thus aren't available if
cwd
the monorepo root.For example:
If cwd is
<root>
, andfoo.spec.ts
calls jest.mock on a package that is only presentpackages/foo/node_modules
, jest crashes!Discussion
<root>
Support for a feature like
cwdNearestPackageJson: true
could easily:With such a feature, monorepo users could run a test from any sub-package, and reliably run in the same pwd and node_modules context as if they had just
cd <path-to-package> && jest .
!