tajo / react-movable

🔀 Drag and drop for your React lists and tables. Accessible. Tiny.
https://react-movable.pages.dev
MIT License
1.55k stars 51 forks source link

Jest encountered an unexpected token #84

Closed rabbl closed 2 years ago

rabbl commented 2 years ago

We have implemented react-movable successfully in our react-typescript codebase and it works great.
Thanks for this great library.

Running the tests with jest are failing with the following error

  ● 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:

    .../node_modules/react-movable/lib/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import List from './List.js';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import React, {SyntheticEvent, useEffect, useState} from 'react';
      2 |
    > 3 | import {List, arrayMove} from 'react-movable';
        | ^
      4 | import {Checkbox} from 'semantic-ui-react';
      5 | import {IColumnOrder, IFielddefinitionBaseProperties} from '../../../../../common/types';
      6 | import {CheckboxProps} from 'semantic-ui-react/dist/commonjs/modules/Checkbox/Checkbox';

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (src/app/view/components/DataTable/utils/DragAndDrop/DragAndDropMenu.tsx:3:1)

here is our jest.config

import type {Config} from '@jest/types';
import * as path from 'path';

// Or async function
export default async (): Promise<Config.InitialOptions> => ({
  verbose: true,
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: [
    '<rootDir>/jest.setup.ts',
  ],
  collectCoverage: true,
  testMatch: ['**/?*.test.ts', '**/?*.test.tsx'],
  coverageReporters: ['text', 'html', 'html-spa'],
  coverageDirectory: './coverage',
  coverageThreshold: {
    global: {
      branches: 50,
      functions: 60,
      lines: 60,
      statements: 60,
    },
  },
  moduleDirectories: ["node_modules", path.join(__dirname, "src")],
  moduleNameMapper: {
    "\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
    "\\.(css|less)$": "<rootDir>/__mocks__/fileMock.js",
    "react-i18next": "<rootDir>/__mocks__/reacti18nextMock.js",
  }
});

Do you have experienced this error? Is there a workaround to fix this issue somehow?

Thanks :)

rabbl commented 2 years ago

I could fix this problem, adding a babel.config.js

module.exports = api => {
  api.cache(true);

  return {
    presets: ["@babel/preset-env", "@babel/preset-react"],
    plugins: [
      "@babel/plugin-transform-runtime",
      ["@babel/plugin-proposal-class-properties"],
    ]
  };
};

and adding the transformIgnorePatterns to jest.config.ts

  transformIgnorePatterns: [
    'node_modules/(?!react-movable)'
  ],
tajo commented 2 years ago
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 need to set your Jest to support ESM.

rabbl commented 2 years ago

I think it's solved for this usecease and I'll close this issue.

RameshDesai77 commented 1 year ago

@tajo Unable to fix this issue can you help me to solve this issue jest.config.s const customJestConfig = { roots: [''], setupFiles: ['/node_modules/jest-offline'], setupFilesAfterEnv: ['/jest.setup.js'], testEnvironment: 'jsdom', moduleFileExtensions: ['ts', 'tsx', 'js', 'json', 'jsx'], testPathIgnorePatterns: [ '/\\[/\\]', ], transformIgnorePatterns: [ '[/\\]node_modules[/\\].+\.(ts|tsx)$', 'node_modules/(?!react-movable)', ], transform: { "\.m?tsx?$": "jest-esm-transformer" }, watchPlugins: [ 'jest-watch-typeahead/filename', 'jest-watch-typeahead/testname', ], moduleNameMapper: { '\.(css|less|sass|scss)$': 'identity-obj-proxy', '\.(gif|ttf|eot|svg|png)$': '/test/mocks/fileMock.js', }, modulePaths: ['/src'], coverageThreshold: { global: { branches: 37, functions: 55, lines: 67, statements: 68, }, },