Jest: Jest not working with decorators

Created on 8 Mar 2017  ·  25Comments  ·  Source: facebook/jest

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
Test suite failed to run
Decorators are not officially supported yet in 6.x pending a proposal update.
However, if you need to use them you can install the legacy decorators transform with:

npm install babel-plugin-transform-decorators-legacy --save-dev

and add the following line to your .babelrc file:

{
  "plugins": ["transform-decorators-legacy"]
}

The repo url is: https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy.

    22 | @inject('vendorStore')
    23 | @observer
  > 24 | class SignUpForm extends Component {

What is the expected behavior?
Expected jest run properly and take a snapshot of the component.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

  • "jest": "^19.0.2"
  • "babel-jest": "^19.0.0",
  • Linux 16
  • Npm 4.0.5
  • Node v7.4.0

.babelrc
{
"presets": ["react","es2015", "stage-0"],
"plugins": ["syntax-flow",
"transform-flow-strip-types",
"transform-decorators",
"transform-class-properties",
"transform-async-to-generator",
"transform-decorators-legacy"]
}

Most helpful comment

I added in my package.json

  "scripts": {
    ...
    "test": "jest --no-cache",
  },
  "jest": {
    "rootDir": ".",
    "testMatch": [
      "**/__tests__/**/*test.js?(x)"
    ],
    "transform": {
      ".*": "<rootDir>/node_modules/webpack-babel-jest"
    }
  },

Run: npm install webpack-babel-jest babel-jest.

npm test.

In .babelrc:

{
  "presets": ["es2015", "react"],
  "plugins": ["transform-decorators-legacy", "transform-class-properties"]
}

This worked for me.

All 25 comments

Try using --no-cache once.

@Hosar Any good news? Having the same problem, --no-cache doesnt' work for me

up, Having the same issue…

same here

Same here

same here, plugin is included in .babelrc, but babel-jest seems not to consider it

any updates on this?

having the same issue…

since I was using create-react-app along with react-scripts I temporally fixed the issue by switching the babelrc boolean to true in the ./node_modules/react-scripts/config/jest/babelTransform.js file

any update on this?

I added in my package.json

  "scripts": {
    ...
    "test": "jest --no-cache",
  },
  "jest": {
    "rootDir": ".",
    "testMatch": [
      "**/__tests__/**/*test.js?(x)"
    ],
    "transform": {
      ".*": "<rootDir>/node_modules/webpack-babel-jest"
    }
  },

Run: npm install webpack-babel-jest babel-jest.

npm test.

In .babelrc:

{
  "presets": ["es2015", "react"],
  "plugins": ["transform-decorators-legacy", "transform-class-properties"]
}

This worked for me.

@tabrindle anyone fix this issue? I use webpack-babel-jest fix decorators problem, but another problem arised! ===> "ReferenceError: regeneratorRuntime is not defined"
It looks like it needs to use regenerator-runtime dependency.

@Soyn I moved to https://github.com/timarney/react-app-rewired

Well worth it.

I'm using jest in the backend, and I'm having using with jest recognizing decorators. Any suggestions?

@DarrylD As fo now, react-app-rewired is warning of possible discontinuation. Fro CRA 2.0 check out customize-cra, which is still immature.

This issue is still affecting me. Any plans on fixing this?

I'm having the same issue. Jest is not recognizing decorators anymore. Please Help!

@cpojer I tried using the no-cache option, still complains.

+1 So, is this issue resolved??

There's no use commenting on 3 year old issue. Please open up a new issue with a minimal reproduction.

I'm 99.9% sure this is an issue with people's babel config and not an issue in Jest. The reason Christoph suggested a --no-cache run was to invalidate the cached transform in case the updated babel config was somehow not picked up.

@SimenB
Oh... Thanks a lot, you're right. When I do the minimal reproduction, I found that is caused by my wrong babel configuration actually.
I'm so sorry for mention this old issue. Jest is working very well, thanks. 🙏

Hi

I'm using jest.run and passing argument : '--no-cache', it doesn' change anything

any help ?

@evan-boissonnot
I think you can try --watchAll=false, --clearCache. Do you have any minimal reproduction?

same issue
any update?

{
  "babel": {
    "env": {
      "test": {
        "plugins": [
          [
            "@babel/plugin-proposal-decorators",
            {
              "legacy": true
            }
          ]
        ]
      }
    }
  }
}

@lake2 Try using this configuration and add --no-cache or --clearCache.

Was this page helpful?
0 / 5 - 0 ratings