ag-grid: Jest encountered an unexpected token

I’m submitting a … (check one with “x”)

[x] bug report => see 'Providing a Reproducible Scenario'
[] feature request => do not use Github for feature requests, see 'Customers of AG Grid'
[] support request => see 'Requesting Community Support'

Customers of AG Grid

If you are a customer you are entitled to use the AG Grid’s customer support system (powered by Zendesk). Please use that channel for guaranteed response from the AG Grid team with regards bugs, feature requests and support.

Requesting Community Support

If you are not a customer of AG Grid, ag-grid staff will label your issue as managed-by-the-community. This means that AG Grid staff is not going to be actively looking into it and it will get closed if inactive for more than one month. The community is welcome to help with this question/support issue.

Providing a Reproducible Scenario run ‘npx jest’ with
@ag-grid-community/client-side-row-model”: “^30.0.6”, “@ag-grid-community/core”: “^30.0.6”, “@ag-grid-community/styles”: “^30.0.6”,

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

C:\repo\project\node_modules\@ag-grid-community\core\dist\esm\es6\main.mjs:15
export { ColumnFactory } from "./columns/columnFactory.mjs";
^^^^^^

SyntaxError: Unexpected token 'export'

   5 |
   6 | import * as vscode from 'vscode';
>  7 | import { GridOptions, Grid } from "@ag-grid-community/core";
     | ^

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1496:14)
  at Object.<anonymous> (src/test/unit/GridUtils.test.ts:7:1)

Jest.config.js

module.exports = { “transform”: { ‘^.+\.(ts|tsx|js|jsx|mjs)$’: [‘ts-jest’, { diagnostics: false, }, ], }, transformIgnorePatterns: [ ‘/node_modules/(?!(@ag-grid-community|@ag-grid-enterprise|ag-grid-react|ag-grid-enterprise|ag-grid-community)/)’, ], testEnvironment: ‘node’, testRegex: ‘/src/test/unit/.*\.(test|spec)?\.(ts|tsx)$’, moduleFileExtensions: [‘ts’, ‘tsx’, ‘js’, ‘jsx’, ‘json’, ‘node’, ‘mjs’], reporters: [ ‘default’, [‘jest-junit’, { outputName: ‘unit-test-results.xml’ }], ], };

Current behavior Fail to run jest unit

Expected behavior Error is not thrown, and component functionality works as indended.

Please tell us about your environment:

Windows 10, VSCode, NPM

  • AG Grid version: X.X.X 30.0.6

  • Browser: VsCode WebWiew/IFrame

  • Language: [all | TypeScript X.X | ES6/7 | ES5] TypeScript:5.1.6 ES6

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 4
  • Comments: 22 (6 by maintainers)

Most upvoted comments

@seanlandsman This solution generates the error from the beginning when trying to import @ag-grid-community/react.

SyntaxError: Unexpected token 'export'

Isn’t the real issue here that @ag-grid-community/react doen’t have the cjs versions?

My solution above should take account of the fact you’re using a framework and default to that package

Are you saying this doesn’t work for react? I tried on a react project and it worked for me

Still, you could try this:

const frameworks = ['angular', 'react', 'vue', 'vue3'];

module.exports = (request, options) => {
    return options.defaultResolver(request, {
        ...options,
        packageFilter: pkg => {
            if(frameworks.some(framework => packageName.includes(framework)) {
                 return pkg;
            }

            const packageName = pkg.name;
            const agDependency = packageName.startsWith("@ag-grid"));
            return {
                ...pkg,
                // default to use the CommonJS ES5 entry point for Jest testing with AG Grid
                main: agDependency ? './dist/cjs/es5/main.js' : pkg.module || pkg.main,
            };
        },
    });

I’ve not tried this - perhaps you can and let me know if it works for you

As a community support I can tell you that mappings works for us in Ag grid 31.0.2

jest.config.js

 moduleNameMapper: {
    "@ag-grid-community/react":
      "<rootDir>/node_modules/@ag-grid-community/react/bundles/ag-grid-react.min.js",
    "@ag-grid-community/core":
      "<rootDir>/node_modules/@ag-grid-community/core/dist/cjs/es5/main.js",
    "@ag-grid-enterprise/core":
      "<rootDir>/node_modules/@ag-grid-enterprise/core/dist/cjs/es5/main.js",
    "@ag-grid-community/client-side-row-model":
      "<rootDir>/node_modules/@ag-grid-community/client-side-row-model/dist/cjs/es5/main.js",
  },

depends on your used modules list can be bigger

The solution proposed by seanlandsman does not work for React applications. The idea in the solution is that with custom Jest resolver we redirect the entry point for the module to a CJS version. But for the @ag-grid-community\react module there is no CJS version of the module provided. For other modules you have dist folder with cjs and esm subfolders that contain corresponding version of the module but for @ag-grid-community\react there is only lib folder containing esm version of the module.