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

Unable to run Jest test - Cannot use import statement outside a module #5570

Closed
qweluke opened this issue Jun 30, 2020 · 5 comments
Closed

Comments

@qweluke
Copy link

qweluke commented Jun 30, 2020

With latest v5.1.0 I am unable to run Jet tests.

    /Volumes/workdata/projects/fr/front/node_modules/@fullcalendar/react/dist/main.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { __assign, __extends } from "tslib";
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      2 | import OverlayLoader from './OverlayLoader';
      3 | import PropTypes from 'prop-types';
    > 4 | import FullCalendar from '@fullcalendar/react';
        | ^
      5 | import dayGridPlugin from '@fullcalendar/daygrid';
      6 | import listPlugin from '@fullcalendar/list';
      7 | import moment from 'moment';

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (src/components/RescuesSchedule.jsx:4:1)

I am using react-scripts (without ejecting). Is there any solution to get rid of this error?

@qweluke qweluke changed the title Unable to run Jest test Unable to run Jest test - Cannot use import statement outside a module Jun 30, 2020
@arshaw
Copy link
Member

arshaw commented Jul 1, 2020

my explanation on this comment addresses this: #5467 (comment)

@arshaw arshaw closed this as completed Jul 1, 2020
@qweluke
Copy link
Author

qweluke commented Jul 1, 2020

I've ended up with mocking fullcalendar like this

jest.mock('@fullcalendar/react', () => () => <div />);
jest.mock('@fullcalendar/daygrid', () => jest.fn());
jest.mock('@fullcalendar/list', () => jest.fn());

Note that this will prevent fullcalendar from being rendered but test should be working fine - ofc if you don't need to test FC

or even better, you can mock fullcalendar and it's API

jest.mock('@fullcalendar/react', () => {
  const React = require('react');
  class FullCalendar extends React.Component {
    getApi = () => ({
      view: {
        activeStart: '20202020',
      },
    });
    render = () => React.createElement('div');
  }
  return FullCalendar;
});
jest.mock('@fullcalendar/daygrid', () => jest.fn());
jest.mock('@fullcalendar/list', () => jest.fn());

@marcobeltempo
Copy link

Without mocking you can use transformIgnorePatterns to transpile fullcalendar

// jest.config.js
module.exports = {
    transform: {
        '^.+\\.[t|j]sx?$': 'babel-jest'
    },
    moduleNameMapper: {
        '\\.(css|less|sass|scss)$': 'identity-obj-proxy',
    },
    transformIgnorePatterns: [
        '/node_modules/(?!@fullcalendar/*).+\\.[t|j]sx?$'
    ],
};

@baileyandy
Copy link

I used the solution from @marcobeltempo but I also had to switch from .babelrc to babel.config.js since my project is using Babel 7 (see jestjs/jest#7578 (comment))

@Aerophite
Copy link

I started getting this after upgrading to jest v28 (from v26). None of the solutions in here worked for me...but I was able to get it to work using a solution from preactjs/enzyme-adapter-preact-pure#179 (comment)

TL;DR
Add this to your jest config

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

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

No branches or pull requests

5 participants