Search code examples
javascriptreactjstypescriptjestjsreact-testing-library

Jest encountered an unexpected token. - React and Typscript with Jest and React-testing-Library


Jest encountered an unexpected token. - React and Typscript with Jest and React-testing-Library

E:\Git\node_modules@mui\x-date-pickers\internals\demo\index.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,jest){export { DemoContainer, DemoItem } from './DemoContainer';

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:

E:\Git\node_modules\@mui\x-date-pickers\internals\demo\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { DemoContainer, DemoItem } from './DemoContainer';
                                                                                  ^^^^^^

SyntaxError: Unexpected token 'export'

   6 | import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
   7 | import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
>  8 | import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
     | ^
   9 | import { useTranslation } from 'react-i18next';
  10 | import { Checked, Unchecked } from '../../../assets/images';
  11 | import dayjs, { Dayjs } from 'dayjs';

  at Runtime.createScriptFromCode (node_modules/jest/node_modules/jest-runtime/build/index.js:1796:14)
  at Object.require (src/components/common/weekTimeComponent/index.tsx:8:1)
  at Object.require (src/pages/manageLocation/component/ManageLocationAddEditComponent.tsx:3:1)
  at Object.require (src/pages/manageLocation/component/manageLocationDetailComponent.tsx:7:1)
  at Object.<anonymous> (tests/pages/manageLocation/manageLocationDetailComponent.test.tsx:8:1)

Don't know why this line is giving error: import { DemoContainer } from '@mui/x-date-pickers/internals/demo';

jest.config.js:

module.exports = {
globals: {
    'ts-jest': {
        diagnostics: false,
    },
},
setupFiles: ['<rootDir>/tests/setup/test-shim.js', '<rootDir>/tests/setup/test-setup.js'],
moduleFileExtensions: ['ts', 'tsx', 'js'],
preset: 'ts-jest/presets/js-with-ts',
transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
},
moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
        '<rootDir>/tests/setup/fileMock.js',
    '\\.(css|less)$': '<rootDir>/tests/setup/styleMock.js',
    uuid: require.resolve('uuid'),
},
moduleDirectories: ['node_modules', 'src'],
testMatch: ['**/tests/**/*.test.(ts|tsx|js)'],
collectCoverage: false,
testEnvironment: 'jsdom',

`

the file causing the error:

import * as React from 'react';
import { SxProps, Theme } from '@mui/material/styles';
interface DemoGridProps {
    children: React.ReactNode;
    components: string[];
    sx?: SxProps<Theme>;
}
interface DemoItemProps {
    label?: React.ReactNode;
    component?: string;
    children: React.ReactNode;
}
export declare function DemoItem(props: DemoItemProps): React.JSX.Element;
export declare function DemoContainer(props: DemoGridProps): React.JSX.Element;

Solution

  • Judging from the output you provided, it seems like your line

    import { DemoContainer } from '@mui/x-date-pickers/internals/demo'
    

    tries to access an index.js:

    E:\Git\node_modules@mui\x-date-pickers\internals\demo\index.js

    If I understand correctly, this file does therefore not get parsed correctly as a Typescript file and cannot export the TypeScript definitions like DemoContainer correctly.

    Renaming this file to index.ts, index.tsx, or main.d.ts might do the trick.