ECMAScript module error when running react-scripts test

Viewed 156

I am having trouble getting my test runner to run properly within my React/Typescript application. When I try and run it, I get the following error:

Watch Usage
 › Press a to run all tests.
 FAIL  src/components/InvestmentStyleSlider/index.test.tsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • 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/en/configuration.html

    Details:

    D:\projects\cult-wines-repos\CultWines.Onboarding.Ui\node_modules\react-bootstrap\esm\Button.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import _extends from "@babel/runtime/helpers/esm/extends";
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import React from 'react';
 FAIL  src/components/InvestmentStyleSlider/index.test.tsx

I am a tad confused as I have another application that is built in the same way and this has no issues whatsoever.I have tried amending the jest config to the following in my package.json but no matter what I do it still spits out the same error

 "version": "0.1.0",
  "private": true,
  "jest": {
    "testEnvironment": "jest-environment-node",
    "transform": {}
  },
  "dependencies": {

Has anyone had a similar error before or can advise as to what is happening here? Again, I am using the react-scripts version of jest which I presumed has ecmascript module support out of the box. Here is my setupTest.js file in case that gives further clues:

import '@testing-library/jest-dom/extend-expect';
import 'jest-styled-components';
import { configure } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import { createSerializer } from 'enzyme-to-json';

configure({ adapter: new Adapter() });

expect.addSnapshotSerializer(createSerializer({ mode: 'deep' }));

And the very simple test file that fails:

/* eslint-disable @typescript-eslint/no-unused-vars */
import React from 'react';
import { render } from 'enzyme';
import InvestmentStyleSlider from './index';

describe('Investment Style Slider', () => {
  it('renders correctly', () => {
    const rendering = render(<InvestmentStyleSlider />);

    expect(rendering).toMatchSnapshot();
  });
});

Thanks

0 Answers
Related