Can't use purecloud-platform-client-v2 with node.js/typescript

I used yarn add to install purecloud-platform-client-v2 and it's in my node_modules. (This particular project was built using yarn rather than npm).

If I add this line to my code:
import { ApiClient } from 'purecloud-platform-client-v2';

I will get this error when the file is accessed in a Jest unit test:

ReferenceError: You are trying to import a file after the Jest environment has been torn down. Followed by
TypeError: ConfigParser is not a constructor.

/xxx/node_modules/purecloud-platform-client-v2/dist/node/purecloud-platform-client-v2.js:291
var configparser = new ConfigParser();

If I comment out the import { ApiClient } line the problem goes away. Everything else relating to purecloud has been commented out.

I've also tried this instead:
import { ApiClient } from 'purecloud-platform-client-v2';

Is there some problem using typescript with purecloud? Is there some other way to do this?

All I want to do is to authenticate, get the queues, and then do api-v2-conversations-callbacks. But I can't even get past the import.

Thanks for any help.

1 Like

Hi jluncford,

Thanks for reporting the issue. We are looking into this and will get back to you as soon as it's investigated or fixed.

Meanwhile, can you try changing the ApiClient inside the import bracket to ApiClientClass and see if the issue persists? I am trying to reproduce the error, and I wrote a dummy program, compiled it from ts to js then ran it using node. It works for me, but maybe I'm doing things differently or missing something that might cause the error.

Thanks

I tried that, it still behaves the same way.

Thanks for confirming that. Does this import line only fail in Jest test, or it doesn't work in the actual program either? Also, does this error happen intermittently or it fails every time? The reason I ask is from the error message you mentioned, it says the "Jest environment" has been torn down. I have tried the SDK in a node program and it works fine. Then I tried to import and use the SDK in a Jest testcase, and after I tried a few dozen times, it failed only once with exactly the error message you described. I couldn't reproduce it again, which is strange and made me wonder if it's related to Jest.

Thanks

Thanks for continuing to look at this. For me it happens every time. I've been stepping through it with a debugger and watched it instantiate your code via the import, go through the one test I haven't commented out, and then do the tear down. Jest is going through all the the outstanding awaits as it's doing its teardown. I'm thinking there's some async behavior in your constructor that isn't getting resolved and when Jest is trying to shut everything down it's going back to that and then it's trying to do that new on the ConfigParser again. I'd be surprised if this is a Jest issue because it only happens when your code is imported.

Also, our system requires that all unit tests pass before deploying to an environment so I haven't been able to see if it works otherwise. I'll remove this unit test class temporarily to get it into a test environment to see what happens.

It crashes while running this line:

const client = ApiClient.instance;

Hi,

This is what I had and works for me.

import { ApiClientClass } from 'purecloud-platform-client-v2';

var client = new ApiClientClass();
client = client.instance;

Thanks

In the case I originally reported all I had to do to cause the problem is to import purecloud. There was no code to actually use it.

I tried this code which is from https://www.npmjs.com/package/purecloud-platform-client-v2 after setting it up as you suggested in your last post:

const client = platformClient.ApiClient.instance;
client.loginClientCredentialsGrant(clientId,clientSecret)
.then(()=> {
  // Do authenticated things
})
.catch((err) => {
 // Handle failure response
 console.log(err);
});

I stepped through it in a debugger and found that inside of loginClientCredentialsGrant it never returned from the request that it generated to the callback location in your code. Neither the .then or .catch were ever executed, instead execution returned to the caller of this code. I also deployed it to a test environment and saw via console logging that it was behaving the same way.

At this point I'm giving up on your SDK and am going to attempt this method:
https://developer.genesys.cloud/api/tutorials/oauth-client-credentials/#language=nodejs&step=0

I appreciate the time you've spent trying to help me with this. I'm interested to know if any other developers have experienced these issues and how they worked around them.

1 Like

I got the same error you reported in your original post

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down
TypeError: ConfigParser is not a constructor

after some investigation I found that this error is being caused by a feature called Live Reload in purecloud-platform-client-v2 that doesn't play nicely with Jest.

You can see here that it is watching for a file change, and will reload the configuration.

Problem is after jest has finished running your tests, the file watcher callback is triggered causing the reload code to run after jest has completed. I believe this is what the error message above is telling us, there is code running after jest has been "torn down".

To resolve this issue, I used jest to disable the fs.watchFile function to prevent it from triggering. So I put this code:

import fs from 'fs'
jest.spyOn(fs, 'watchFile').mockImplementation()

inside my jest setup file (jest.config setupFilesAfterEnv property). After doing this I didn't get the error message anymore.

Hey David,

What you are describing is related to how we reload our SDK configuration. Several months ago we implemented a "live" reload of the SDK configuration. I am going to open a ticket with our SDK group and see if we can resolve this or at the very least give you the ability to disable the live config. Thanks for your patience. This has been a subtle item to track down.

Thanks for everyone's patience.
John Carnell
Manager, Developer Engagement

Hi David,

Using client.config.live_reload_config = false will also disable the config file reloading feature.
I'll add a warning about this issue to the README to avoid headaches for future SDK users.

Also, I'll make a change to avoid the error: TypeError: ConfigParser is not a constructor.

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