添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Getting Support for the experimental syntax 'decorators' isn't currently enabled (1:1) Syntax error when trying to run npm test

Ask Question

I have looked around on numerous forums and for some reason no matter what i try nothing will work. My files are all over the place but i will try to provide as much detail as i can.

The Full error message i get from my app.test.js includes:

● 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>\314\src\components\Navbar\navStyles.css: Support for the experimental syntax 'decorators' isn't currently enabled (1:1):
    > 1 | @font-face {
      2 |   font-family: 'nunito_extralightregular';
      3 |   src: url('Navbar.js') format('woff2'),
      4 |        url('Navbar.js') format('woff');
    Add @babel/plugin-proposal-decorators (https://github.com/babel/babel/tree/main/packages/babel-plugin-proposal-decorators) to the 'plugins' section of your Babel config to enable transformation.
    If you want to leave it as-is, add @babel/plugin-syntax-decorators (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-decorators) to the 'plugins' section to enable parsing.
       5 | import { useContext, useState } from "react";
       6 | import { Box, IconButton, Typography, useTheme } from "@mui/material";
    >  7 | import "./navStyles.css";
       9 | const Navbar = () => {
      at instantiate (node_modules/@babel/parser/src/parse-error/credentials.ts:62:21)
      at instantiate (node_modules/@babel/parser/src/parse-error.ts:60:12)
      at Parser.toParseError [as raise] (node_modules/@babel/parser/src/tokenizer/index.ts:1490:19)
      at Parser.raise [as expectOnePlugin] (node_modules/@babel/parser/src/tokenizer/index.ts:1550:18)
      at Parser.expectOnePlugin [as parseDecorator] (node_modules/@babel/parser/src/parser/statement.ts:731:10)
      at Parser.parseDecorator [as parseDecorators] (node_modules/@babel/parser/src/parser/statement.ts:710:28)
      at Parser.parseDecorators [as parseStatementLike] (node_modules/@babel/parser/src/parser/statement.ts:415:25)
      at Parser.parseStatementLike [as parseModuleItem] (node_modules/@babel/parser/src/parser/statement.ts:354:17)
      at Parser.parseModuleItem [as parseBlockOrModuleBlockBody] (node_modules/@babel/parser/src/parser/statement.ts:1359:16)
      at Parser.parseBlockOrModuleBlockBody [as parseBlockBody] (node_modules/@babel/parser/src/parser/statement.ts:1333:10)
      at Parser.parseBlockBody [as parseProgram] (node_modules/@babel/parser/src/parser/statement.ts:226:10)
      at Parser.parseProgram [as parseTopLevel] (node_modules/@babel/parser/src/parser/statement.ts:208:25)
      at Parser.parseTopLevel [as parse] (node_modules/@babel/parser/src/parser/index.ts:45:10)
      at parse (node_modules/@babel/parser/src/index.ts:67:38)
      at parser (node_modules/@babel/core/src/parser/index.ts:28:19)
          at parser.next (<anonymous>)
      at normalizeFile (node_modules/@babel/core/src/transformation/normalize-file.ts:51:24)
          at normalizeFile.next (<anonymous>)
      at run (node_modules/@babel/core/src/transformation/index.ts:38:36)
          at run.next (<anonymous>)
      at transform (node_modules/@babel/core/src/transform.ts:29:20)
          at transform.next (<anonymous>)
      at evaluateSync (node_modules/gensync/index.js:251:28)
      at sync (node_modules/gensync/index.js:89:14)
      at fn (node_modules/@babel/core/src/errors/rewrite-stack-trace.ts:97:14)
      at transformSync (node_modules/@babel/core/src/transform.ts:66:52)
      at Object.require (src/components/Navbar/Navbar.js:7:1)
      at Object.require (__tests__/app.test.js:3:1)

babel.config.json file:

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

jest.config.js file:

module.exports = {
    testEnvironment: 'jsdom',
    testEnvironmentOptions: {
      userAgent: 'node.js',
      resources: 'usable',
      runScripts: 'dangerously',
    rootDir: '.',
    modulePaths: [ '<rootDir>' ],
    moduleDirectories: [ 'node_modules', 'src' ],
    setupFilesAfterEnv: [ '<rootDir>/setupJest.js' ],
    transformIgnorePatterns: [ '/node_modules/' ],

webpack.config.js file:

module.exports = {
    mode: 'production',
    module: {
      rules: [{ test: /\.js$/, use: ['babel-loader'] }],

Within my package.json file, this is the babel and jest dependencies;

      "devDependencies": {
        "@babel/core": "^7.21.8",
        "@babel/plugin-proposal-decorators": "^7.21.0",
        "@testing-library/jest-dom": "^5.16.5",
        "@testing-library/react": "^14.0.0",
        "jest": "^29.5.0",
        "jest-environment-jsdom": "^29.5.0",
        "jest-fetch-mock": "^3.0.3"

And my current app.test.js file:

import { render, fireEvent } from "@testing-library/react";
import React from 'react';
import NavBar from '../src/components/Navbar/Navbar';
// Import component files 
describe( NavBar , () => {
    test("user clicks on home button, refreshing page, or, returning home", () => {
      const { getByTestId } = render(<NavBar />);
      // eslint-disable-next-line testing-library/prefer-screen-queries
      const homeLink = getByTestId('home');
      fireEvent.click(homeLink);
      expect(homeLink).toBeInTheDocument();

I have tried to uninstall and reinstall all packages making sure to have jest and jest-environment-jsdom be the same version which fixed one error. I have tried changing the { "legacy": true } to { "decoratorsBeforeExport": true }. Im not sure what else im doing wrong

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.