nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.16k stars 2.31k forks source link

Allow other test transformers like `@swc/jest` and don't require `ts-jest` to be installed #15342

Closed mcmxcdev closed 1 year ago

mcmxcdev commented 1 year ago

Current Behavior

We recently switched from ts-jest to @swc/jest and would now like to get rid of ts-jest dependency.

When uninstalled, we run into this error:

> nx run ui:test --watch

 >  NX   ● Validation Error:

     Module ts-jest in the transform option was not found.
            <rootDir> is: /home/user/Webdevelopment/Freelancing/org/repo/apps/ui

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

   Pass --verbose to see the stacktrace.

Expected Behavior

It should be fine to use whatever tool for test transformation without being restricted by validation errors.

GitHub Repo

No response

Steps to Reproduce

  1. remove ts-jest from nx monorepo
  2. run jest test executor
  3. expect validation error to pop up

Nx Report

>  NX   Report complete - copy this into the issue template

   Node : 18.14.1
   OS   : linux x64
   pnpm : 7.27.1

   nx                      : 15.7.2
   @nrwl/jest              : 15.7.2
   @nrwl/linter            : 15.7.2
   @nrwl/workspace         : 15.7.2
   @nrwl/cli               : 15.7.2
   @nrwl/cypress           : 15.7.2
   @nrwl/devkit            : 15.7.2
   @nrwl/eslint-plugin-nx  : 15.7.2
   @nrwl/js                : 15.7.2
   @nrwl/nest              : 15.7.2
   @nrwl/next              : 15.7.2
   @nrwl/node              : 15.7.2
   @nrwl/react             : 15.7.2
   @nrwl/storybook         : 15.7.2
   @nrwl/tao               : 15.7.2
   @nrwl/webpack           : 15.7.2
   @nrwl/nx-cloud          : 15.0.3
   typescript              : 4.9.5

Failure Logs

No response

Additional Information

No response

FrozenPandaz commented 1 year ago

It appears that the ui project is still configured to use ts-jest which is the cause of that error. Could you post the jest.config.ts?

mcmxcdev commented 1 year ago

It appears that the ui project is still configured to use ts-jest which is the cause of that error. Could you post the jest.config.ts?

Searching through the whole project, there is no reference to ts-jest anymore.


jest.config.ts

export default {
  displayName: 'shared-ui',
  preset: '../../../jest.preset.js',
  transform: {
    '^.+\\.(t|j)sx?$': [
      '@swc/jest',
      {
        jsc: {
          transform: {
            react: {
              runtime: 'automatic',
            },
          },
        },
      },
    ],
  },
  coverageDirectory: '../../../coverage/libs/shared/ui',
  setupFilesAfterEnv: ['../../../jest.setup.js'],
};

jest.preset.js

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

module.exports = {
  ...nxPreset,
  moduleFileExtensions: ['js', 'ts', 'tsx', 'html'],
  moduleNameMapper: {
    '@next/font/(.*)': require.resolve(
      'next/dist/build/jest/__mocks__/nextFontMock.js'
    ),
  },
  setupFilesAfterEnv: [
    '@testing-library/jest-dom/extend-expect',
    'jest-extended/all',
  ],
  restoreMocks: true,
  testTimeout: 60000,
};

jest.setup.js is not relevant, since it just uses mocks.

barbados-clemens commented 1 year ago

The default nx preset contains ts-jest as the default transformer. Most likely jest is validating that first even though it's never used. Try deleting the transformer property on the nx preset in the root jest.preset.js file and see if it goes away.

mcmxcdev commented 1 year ago

Good catch! It starts working when I do:

...nxPreset,
  transform: undefined,

so @nrwl/jest/preset code needs to be updated to not hard require ts-jest

barbados-clemens commented 1 year ago

While I do see that it would work best for you to not have the preset define a default transformer, it's the job of the nx jest preset to define the default behaviors so I don't think we plan on changing the preset.

Since removing the default transformer for you workspace works, I'm going to find this as a valid work around for anyone else wanting to do this.

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.