nrwl / nx

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

Decorators are not allowed anymore after updating nx #29010

Closed amin3mej closed 21 hours ago

amin3mej commented 1 day ago

Current Behavior

Getting this error while trying to run tests:

Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    SyntaxError: /path/to/nx-project/libs/examplelib/src/lib/dto/some-class.dto.ts: Decorators are not enabled.
    If you are using ["@babel/plugin-proposal-decorators", { "version": "legacy" }], make sure it comes *before* "@babel/plugin-transform-class-properties" and enable loose mode, like so:
        ["@babel/plugin-proposal-decorators", { "version": "legacy" }]
        ["@babel/plugin-transform-class-properties", { "loose": true }]
      4 |
    > 5 | export class SomeClass {
        |        ^
      6 |   @IsNotEmpty()

Expected Behavior

It should not trigger any error like before the upgrade.

GitHub Repo

No response

Steps to Reproduce

  1. I updated at nx from 19.5.7 to 20.1.2 and the tests that have using decorators started failing.

Similar to https://github.com/nrwl/nx/discussions/27533

Nx Report

❯ nx report              

 NX   Report complete - copy this into the issue template

Node           : 18.19.1
OS             : darwin-arm64
Native Target  : aarch64-macos
pnpm           : 8.15.9

nx                 : 20.1.2
@nx/js             : 20.1.2
@nx/jest           : 20.1.2
@nx/eslint         : 20.1.2
@nx/workspace      : 20.1.2
@nx/cypress        : 20.1.2
@nx/devkit         : 20.1.2
@nx/eslint-plugin  : 20.1.2
@nx/nest           : 20.1.2
@nx/next           : 20.1.2
@nx/node           : 20.1.2
@nx/playwright     : 20.1.2
@nx/plugin         : 20.1.2
@nx/react          : 20.1.2
@nx/storybook      : 20.1.2
@nx/vite           : 20.1.2
@nx/web            : 20.1.2
@nx/webpack        : 20.1.2
nx-cloud           : 19.1.0
typescript         : 5.5.4
---------------------------------------
Registered Plugins:
nx/plugins/package-json
@nx/eslint-plugin
@nx/webpack/plugin
@nx/eslint/plugin
---------------------------------------
Community plugins:
@nx-aws-plugin/nx-aws-cache : 3.2.0
@nx-tools/nx-prisma         : 5.1.0

Failure Logs

the class:
import { IsDefined, IsNotEmpty, IsString } from 'class-validator';

export class CartDto {
  @IsNotEmpty()
  @IsString()
  @IsDefined()
  id: string;

  constructor(id: string) {
    this.id = id
  }
}

Package Manager Version

No response

Operating System

Additional Information

No response

amin3mej commented 21 hours ago

I managed to fix it in this way: I had a have a jest.config.ts like this:

export default {
  displayName: 'my-app-name,
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['../../jest.setup.ts'],
  transform: {
    '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
    '^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/next/babel'] }],
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/apps/my-app-name',
}

and I changed it to this:

export default {
  displayName: 'my-app-name,
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['../../jest.setup.ts'],
transform: {
    '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
    '^.+\\.[tj]sx?$': [
      'babel-jest',
      {
        presets: ['@nx/next/babel'],
        plugins: [
          ['@babel/plugin-proposal-decorators', { legacy: true }],
          ['@babel/plugin-transform-class-properties', { loose: true }],
        ],
      },
    ],
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/apps/my-app-name',
}