Skip to content

Nx Jest <rootdir> does not resolve properly in jest.preset.js #31635

Closed as not planned
@anthony-b-dev

Description

@anthony-b-dev

Current Behavior

Currently, I have multiple jest.config.ts files in projects throughout my monorepo as a result of the automated library generation process. For some libraries, I have them nested one directory down to group the libraries by application. This results in the various jest.config.ts files being at different levels of the workspace hierarchy.

In jest.preset.js at the root level, I have the following:

const nxPreset = require('@nx/jest/preset').default;

module.exports = {
  ...nxPreset,
  /* TODO: Update to latest Jest snapshotFormat
   * By default Nx has kept the older style of Jest Snapshot formats
   * to prevent breaking of any existing tests with snapshots.
   * It's recommend you update to the latest format.
   * You can do this by removing snapshotFormat property
   * and running tests with --update-snapshot flag.
   * Example: "nx affected --targets=test --update-snapshot"
   * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
   */
  snapshotFormat: { escapeString: true, printBasicPrototype: true },
  setupFiles: ['./jest.setup.ts'],
};

The last line gets me this error when I run nx test:

 NX   ● Validation Error:


        Module ./jest.setup.ts in the setupFiles option was not found.
               <rootDir> is: C:<\path\to\dir>\libs\shared\data

        Configuration Documentation:
        https://jestjs.io/docs/configuration

Because the jest.config.ts files in the libraries are at different levels, I can't hard code a relative path in the file.

Expected Behavior

The <rootdir> in jest.preset.js at the workspace root resolves to the workspace root every single time, not relative to the library root.

GitHub Repo

No response

Steps to Reproduce

  1. Run npx create-nx-worspace with the Angular preset and choosing Jest as the test runner.
  2. Run npm install @nx/nest --save
  3. Run npx nx g @nx/angular:library libs/angular --importPath @testdemo/angular --routing=false --style none
  4. Run npx nx g @nx/nest:library --directory=libs/nested/nestlb --importPath="@test/nestlib" --name=nestlib --simpleName
  5. run npx nx run-many - t test

Nx Report

Node           : 22.13.1
OS             : win32-x64
Native Target  : x86_64-windows
npm            : 9.6.6

nx (global)            : 21.1.2
nx                     : 21.2.0
@nx/js                 : 21.2.0
@nx/jest               : 21.2.0
@nx/eslint             : 21.2.0
@nx/workspace          : 21.2.0
@nx/angular            : 21.2.0
@nx/cypress            : 21.2.0
@nx/devkit             : 21.2.0
@nx/esbuild            : 21.2.0
@nx/eslint-plugin      : 21.2.0
@nx/express            : 21.2.0
@nx/module-federation  : 21.2.0
@nx/nest               : 21.2.0
@nx/node               : 21.2.0
@nx/rspack             : 21.2.0
@nx/vite               : 21.2.0
@nx/web                : 21.2.0
@nx/webpack            : 21.2.0
typescript             : 5.8.3
---------------------------------------
Registered Plugins:
@nx/angular/plugin
---------------------------------------
Community plugins:
@ngrx/effects        : 19.2.1
@ngrx/entity         : 19.2.1
@ngrx/eslint-plugin  : 19.2.1
@ngrx/operators      : 19.2.1
@ngrx/router-store   : 19.2.1
@ngrx/schematics     : 19.2.1
@ngrx/signals        : 19.2.1
@ngrx/store          : 19.2.1
@ngrx/store-devtools : 19.2.1
angular-eslint       : 20.0.0
---------------------------------------
Cache Usage: 446.68 KB / 93.45 GB

Failure Logs

from the recreation steps:

       NX   ● Validation Error:


        Module ./jest.setup.js in the setupFiles option was not found.
               <rootDir> is:  C:\<\path\to\dir>\libs\nested\nestlb      

        Configuration Documentation:
        https://jestjs.io/docs/configuration

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

This issue started a few Nx versions ago, maybe with v18 or v19, I can't pinpoint exactly when. Before then, I was having no issues.

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions