Jest encountered an unexpected token

We use the purecloud SDK in a react/trypescript project
"purecloud-platform-client-v2": "^204.0.0",

A lot of our unit test produce the following error:
Jest encountered an unexpected token

    C:\Users\me\src\project\node_modules\purecloud-platform-client-v2\node_modules\axios\index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import axios from './lib/axios.js';
SyntaxError: Cannot use import statement outside a module

I tried a few remedies as suggested by jest, but no success. Would anyone have any insights how to fix this?

Thanks

This isn't a problem with the SDK; it's a problem with some settings for your app/runtime. The fix depends on how you want your project settings. The general concept of the problem is that you are importing the modern-style (es6+ modules) source code from the package but your configuration is set to only expect non-module syntax. The fix is to either import a pre-transpiled package or change your project's settings.

If this is only an issue with jest and your react app works fine with the SDK already, you might be able to simply tweak your jest project settings. You'll likely find some prior art for fixing this if you search for the packages you're using and the error message.

If this is an issue for the react app itself, there's a good chance you're going to have to eject it (if you used create-react-app) or use a webpack config shim in order to make the config changes described above. I don't recommend doing this because you can never un-eject and you become responsible for maintaining the build process. There's a blog post about using the SDK with react, but it has a note about compatible versions at the top: https://developer.genesys.cloud/blueprints/react-app-with-genesys-cloud-sdk/.

As an alternative, the SDK is open source and you are free to copy the whole source or just the parts you need into your own project. Here's the repo: GitHub - MyPureCloud/platform-client-sdk-javascript: Javascript platform client SDK, and here's an example of taking what you need: org-chart-explorer/app/src/helpers/GenesysCloudAPI.ts at main · GenesysCloudBlueprints/org-chart-explorer · GitHub

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.