Integration Testing with JS Client

I want to do some integration tests by creating a test node client (without browser) by using javascript client nakama-js and testing authentication, testing RPC calls, websocket etc. However when I tried to run client.authenticateEmail() for example, it uses XMLHttpRequest which is not available in Node and only as a browser. I am wondering if this will also be a problem with other SDK methods which will require browser.

I don’t want to implement this client via browser, only node runtime in order to run integration tests.
So my questions are:

  1. Is there a way to run nakama-js client with node runtime only (no browser) in order to run integration tests
  2. If not, is there a standard way to be able to run integration tests (without browser) by other means? Preferably with JS/TS.

Thank you

Details:

  1. Versions: Docker nakama:3.15.0, nakama-js 2.6.0
  2. Server Framework Runtime language (If relevant) TS

I actually realized looking at the source code, the error of XMLHttpRequest probably was not intentional. I get this error with Node runtime because XMLHttpRequest is only available via browser

const descriptor = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, "withCredentials");
                                                     ^
ReferenceError: XMLHttpRequest is not defined

but reading the source code, it seems like it wasn’t supposed to happen:

packages/nakama-js/utils.ts:

 const descriptor = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, "withCredentials");

    // in Cocos Creator, XMLHttpRequest.withCredentials is not writable, so make the fetch
    // polyfill avoid writing to it.
    if (!descriptor?.set) {
      fetchOptions.credentials = 'cocos-ignore'; // string value is arbitrary, cannot be 'omit' or 'include
    }

the check for !descriptor?.set wouldn’t run if XMLHttpRequest isn’t available - maybe i should raise it on GitHub

I raised a PR here: Fix XMLHttpRequest for Node Runtime by dragonlobster · Pull Request #150 · heroiclabs/nakama-js · GitHub I tested it and it seems to work for me

1 Like

Thanks @dragonlobster, PR accepted.