"Error: Preset jest.preset.js not found" when using IntelliJ to run Jest tests in NX Workspace

Viewed 990

I have an NX workspace with Node + Angular libraries. When upgrading from NX version 13.x to 14.x, NX reconfigured Jest through its automated migration process.

When running the project:test generator, my jest tests work as expected. However, when trying to run unit tests directly through the IDE (IntelliJ), I get the following error:

Error: ● Validation Error:

  Preset jest.preset.js not found.

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

The project is setup with project level configuration files, as shown below.

module.exports = {
   coverageDirectory: '<path>',
   displayName      : '<name>',
   globals          : {
      'ts-jest': {
         stringifyContentPathRegex: '\\.(html|svg)$',
         tsconfig                 : '<rootDir>/tsconfig.spec.json'
      }
   },

   setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],

   snapshotSerializers    : [
      'jest-preset-angular/build/serializers/no-ng-attributes',
      'jest-preset-angular/build/serializers/ng-snapshot',
      'jest-preset-angular/build/serializers/html-comment'
   ],
   transform              : {
      '^.+.(ts|mjs|js|html)$': 'jest-preset-angular'
   },
   transformIgnorePatterns: ['node_modules/(?!.*.mjs$)'],
   preset                 : '../../../../../jest.preset.ts'
};

The preset file exits and looks like:

const nxPreset = require('@nrwl/jest/preset');

module.exports = { ...nxPreset };

I also have a global jest.config.ts file. This file does have a reference to jest.preset.js which does not exist within its scope. Also, not existing, is a globally scoped test-setup.ts file. Shown below:

const { getJestProjects } = require('@nrwl/jest');

module.exports = {
  globals: {
    'ts-jest': {
      astTransformers: {
        before: [
          'jest-preset-angular/build/InlineFilesTransformer',
          'jest-preset-angular/build/StripStylesTransformer'
        ]
      },
      stringifyContentPathRegex: '\\.(html|svg)$',
      tsconfig: '<rootDir>/tsconfig.spec.json'
    }
  },
  preset             : 'jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment'
  ],
  projects: [
    ...getJestProjects(),
    //Lots of projects
  ]
};

I imagine that there was something awry in the migration, but I do not understand jest configuration well enough to know how to deconflict these. There are multiple issues in the global config file, however when I eliminate the global config file I get a host of other errors (as in it appears that it tries to run my ts tests in js).

Any thoughts as to how I could rectify this issue would be appreciated.

1 Answers

I was able to overcome this problem by adjusting the "working directory" in my test configuration.

enter image description here

I was getting the error Preset ../../jest.preset.ts not found so I found the jest.preset.ts file in my project and set the working directory of my configuration to be two folders "below" that file.

E.g.

  • My test file is in src/app/my-app/A/B/C
  • My jest.preset.ts file is in src
  • The error was Preset ../../jest.preset.ts not found
  • Set the working directory to src/app/my-app (so the test runner will "step up" to the src folder)

enter image description here

Related