diff --git a/packages/nx/src/generators/testing-utils/create-tree-with-empty-workspace.ts b/packages/nx/src/generators/testing-utils/create-tree-with-empty-workspace.ts index 005fc37e25d9b..c7545096379a9 100644 --- a/packages/nx/src/generators/testing-utils/create-tree-with-empty-workspace.ts +++ b/packages/nx/src/generators/testing-utils/create-tree-with-empty-workspace.ts @@ -1,5 +1,6 @@ import { FsTree } from '../tree'; import type { Tree } from '../tree'; +import { workspaceRoot } from '../../utils/workspace-root'; /** * Creates a host for testing. @@ -8,6 +9,12 @@ export function createTreeWithEmptyWorkspace( opts = {} as { layout?: 'apps-libs' } ): Tree { const tree = new FsTree('/virtual', false); + // Our unit tests are all written as though they are at the root of a workspace + // However, when they are run in a subdirectory of the workspaceRoot, + // the relative path between workspaceRoot and the directory the tests are run + // is prepended to the paths created in the virtual tree. + // Setting this envVar to workspaceRoot prevents this behaviour + process.env.INIT_CWD = workspaceRoot; return addCommonFiles(tree, opts.layout === 'apps-libs'); }