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

SyntaxError: Unexpected token 'export' when running jest tests #7113

Closed
1 task done
pimmee opened this issue Dec 28, 2022 · 12 comments
Closed
1 task done

SyntaxError: Unexpected token 'export' when running jest tests #7113

pimmee opened this issue Dec 28, 2022 · 12 comments

Comments

@pimmee
Copy link

pimmee commented Dec 28, 2022

Reduced Test Case

https://stackblitz.com/edit/github-sgjd3r?file=src/demo.test.jsx&view=editor

Do you understand that if a reduced test case is not provided, we will intentionally delay triaging of your ticket?

  • I understand

Which connector are you using (React/Angular/etc)?

React

Bug Description

Since upgrading to FullCalendar v6, all my react test in jest are failing with the following error:

SyntaxError: Unexpected token 'export'

image

Here's the error when I run 'npm run test' in StackBlitz
image

Edit:
Here's a StackBlitz with v5.11.3 where the test is working. The following release (v6.0.0-beta.2) is not working.

@acerix
Copy link
Member

acerix commented Dec 28, 2022

Thanks, confirmed. I get the same error when running yarn test on the example project.

https://github.com/fullcalendar/fullcalendar-examples/tree/main/jest-react

@acerix acerix changed the title v6 with jest: SyntaxError: Unexpected token 'export' SyntaxError: Unexpected token 'export' when running jest tests Dec 28, 2022
@arshaw arshaw added this to the v5-upcoming milestone Jan 9, 2023
@adampana
Copy link

I am facing the same issue when I moved to 6.0.2 from 5.11.3

@arshaw
Copy link
Member

arshaw commented Jan 11, 2023

queued for release 8b0eb87

@arshaw arshaw closed this as completed Jan 11, 2023
@arshaw
Copy link
Member

arshaw commented Jan 11, 2023

released in v6.0.3

@pimmee
Copy link
Author

pimmee commented Jan 14, 2023

released in v6.0.3

@arshaw I'm still experiencing the issue in 6.0.3, please reopen the issue. See repro with 6.0.3 dependencies

@arshaw
Copy link
Member

arshaw commented Jan 17, 2023

I was using jest 27, which works fine now. I see the problem in jest 28 and above.

The problem is happening because jest now looks at the "browser" field in package.json for jsdom. Internally, preact provides a "browser" field, but the file is ESM, which jest does not natively support without a babel transform.

I'll look into this more. But a workaround would be ensuring an ESM babel transform is applied to node_modules packages (specifically preact).

@arshaw arshaw reopened this Jan 17, 2023
@arshaw
Copy link
Member

arshaw commented Jan 17, 2023

here's a nicer workaround. in your jest.config.js:

module.exports = {
  testEnvironment: 'jsdom',
  testEnvironmentOptions: {
    customExportConditions: [] // don't load "browser" field
  }
};

@pimmee
Copy link
Author

pimmee commented Jan 20, 2023

That workaround works, thanks!

@arshaw arshaw modified the milestones: v6.0.3, v6.0.4 Jan 24, 2023
@arshaw arshaw added the Docs label Jan 24, 2023
@arshaw arshaw modified the milestones: v6.1.0, v6.1.1 Jan 30, 2023
@arshaw
Copy link
Member

arshaw commented Jan 30, 2023

I've updated the repro with the latest version (v6.1.1) and added the above-mentioned fix. It works:

https://stackblitz.com/edit/github-sgjd3r-mmezji?file=package.json&view=editor

I've mentioned this workaround at the bottom of the fullcalendar-react docs and the example project.

@arshaw arshaw closed this as completed Jan 30, 2023
@rjdestigter
Copy link

I had to modify transformIgnorePatterns to not match node_modules/preact so that it would be included in the transformation: node_modules\/(?!preact). Additionally I had to provide a custom transformation option as my setup (preset ts-jest) was only targeting TypeScript files:

transform: {
  "node_modules/preact": "babel-jest",
},

@robbyjobber
Copy link

Setting this in your jest config also fixes this issue.

moduleNameMapper: {
    "^preact(/(.*)|$)": "preact$1",

@edukisto
Copy link

edukisto commented Mar 26, 2024

tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "esModuleInterop": true,
    "verbatimModuleSyntax": false
  }
}

jest.config.ts:

import type { JestConfigWithTsJest } from 'ts-jest';
import { defaultsESM } from 'ts-jest/presets';

const config: JestConfigWithTsJest = {
  ...defaultsESM,
  moduleNameMapper: {
    '^(.+)\\.js$': '$1',
  },
  preset: 'ts-jest/presets/js-with-ts-esm',
};

export default config;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

7 participants