Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: Expression expected #40

Closed
schoening opened this issue Nov 17, 2021 · 8 comments · Fixed by #49
Closed

error: Expression expected #40

schoening opened this issue Nov 17, 2021 · 8 comments · Fixed by #49

Comments

@schoening
Copy link

schoening commented Nov 17, 2021

We are currently upgrading to NextJS 12, which is using SWC rather than babel.

I wanted to give @swc/jest a try because of ESM module issues with babel-jest:

SyntaxError: Cannot use import statement outside a module

After switching from
'^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest'
to
'^.+\\.(t|j)sx?$': '@swc/jest'
I am getting the following errors:

FAIL  stories/stories.test.js
  ● Test suite failed to run

    error: Expression expected
      
       |
    36 |       <RouterContext.Provider value={mockRouter}>
       |       ^

    error: Expected ',', got 'value'
      
       |
    36 |       <RouterContext.Provider value={mockRouter}>
       |                               ^^^^^



    Caused by:
        0: failed to process js file
        1: Syntax Error

Is @swc/jest not supposed to work with jsx/tsx files? The regrex implies that it does work with it. Any suggestions?

@bodaso
Copy link

bodaso commented Nov 18, 2021

What Jest version are you on?

@schoening
Copy link
Author

@bodazhao

"jest": "^26.5.2" I believe. At least in that particular folder. We are using a monorepo style with rush and pnpm so there are a few other versions. But this particular one uses the one I mentioned.

@schoening
Copy link
Author

I got the babel-jest working and that is good enough for me. Would like to switch to swc tho

@ghost
Copy link

ghost commented Nov 26, 2021

@bodazhao It works on several of our projects but fails on one that uses jsx instead of tsx and has some enzyme tests

  ● Test suite failed to run

    error: Expression expected
     
      |
    9 |     <React.Suspense fallback={<LinearProgress />}>
      |     ^

All of them use "jest": "^27.3.1"

Shall we reopen this issue?

@kdy1
Copy link
Member

kdy1 commented Nov 26, 2021

I think #44 cause the issue?

@kdy1 kdy1 reopened this Nov 26, 2021
@maxhilliard
Copy link

Currently porting a test suite and had this issue. I had to set jsx / tsx to true to resolve this issue

[
  {
    "test": ".tsx?$",
    "jsc": {
      "parser": {
        "syntax": "typescript",
        "tsx": true
      },
      "target": "es5",
      "loose": false
    }
  },
  {
    "test": ".jsx?$",
    "jsc": {
      "parser": {
        "syntax": "ecmascript",
        "jsx": true,
        "decorators": false,
        "dynamicImport": false
      }
    }
  }
]

@sanchezg7
Copy link

I ran into Expression Expected Syntax error with a vanilla install. I am only working with jsx, in my case; I've slightly tweaked @maxhilliard 's response which worked for me. Here is my transform in jest.config.js:

transform: {

        "^.+\\.(t|j)sx?$": [

            "@swc/jest",

            {

                "jsc": {

                    "parser": {

                        "syntax": "ecmascript",

                        "jsx": true,

                        "decorators": false,

                        "dynamicImport": false

                    }

                }

            },

        ],

    },

@KeithChasen
Copy link

@sanchezg7 God bless you, my dear savior :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

6 participants