Date of slack thread: 4/12/24
Anonymous:
Hi Statsig team, After adding `statsig-js: 4.52.0` in `package.json` for an Angular application, the unit tests started failing with: ```
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: /home/jferreira/git/omsurvey/aapps-ui-service/app/node_modules/statsig-js/node_modules/uuid/dist/esm-browser/index.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js'; ^^^^^^ SyntaxError: Unexpected token 'export' at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14) at Object.require (node_modules/statsig-js/dist/ErrorBoundary.js:43:14) ```
Statbot: It appears that Jest is having trouble parsing the `statsig-js` module because it contains ECMAScript Modules (ESM) syntax, which Jest may not be configured to handle by default. Here are some steps you can take to resolve this issue:
Transform ESM Modules: Ensure that your Jest configuration is set up to transform ESM modules. You may need to adjust the `transformIgnorePatterns` option to include the `statsig-js` package or use a custom transformer for ESM.
Babel Configuration: If you're using Babel, make sure that it is configured to transpile the ESM syntax. You might need to include the appropriate Babel plugins or presets that handle ESM.
Mocking: If the module does not need to be transformed for your tests to run, you can mock the `statsig-js` module using Jest's `moduleNameMapper` configuration option.
Check Jest Documentation: Refer to the Jest documentation for more details on how to handle ECMAScript Modules, TypeScript, and custom transformations (<https://jestjs.io/docs/configuration> and <https://jestjs.io/docs/code-transformation>).
If you've tried these steps and are still encountering issues, it might be helpful to look into the specific Jest and Babel configurations for your project. Since the error is related to Jest's handling of module syntax, the solution will involve configuring Jest and possibly Babel to properly handle the syntax used by the `statsig-js` package.