Stubbing the DefenderRelayProvider token

In my Autotask unit tests, it looks like the DefenderRelayProvider constructor calls some async methods to get a token for the API key. I get four of these:

Failed to get a token for the API key testKey {
  code: 'UserNotFoundException',
  name: 'UserNotFoundException',
  message: 'User does not exist.'

How can I stub the DefenderRelayProvider to return a fake token? I can't quite tell which method is responsible.

Hey @dtp5! My suggestion would be to, instead of stubbing the authenticate method, run the tests using an ethers or web3js provider connected to your local dev network (ganache or hardhat network), or a full mock object. See the relay example in the autotask-examples repo: the Autotask is split into the Defender entrypoint (that sets up the DefenderRelayProvider) and the main logic (that uses the provider).

The tests then create a different provider, and call the main function directly:

Alternatively, if you do want to stub out the function that authenticates the Relayer to the API, you need to stub out the DefenderRelayProvider.relayer.relayer.init() async method, which returns a Promise<void>.

1 Like