vaadin-connect icon indicating copy to clipboard operation
vaadin-connect copied to clipboard

Avoid accessing browser APIs in the frontend client constructor

Open platosha opened this issue 7 years ago • 1 comments

Suppose I have a module that depends on a method from an annotated service:

import {method} from './generated/MyService.js';

export class MyController {
  async myAction() {
    return await method();
  }
}

When doing frontend unit tests for that module, I want to be able to import it a Node.js environment and stub the dependencies. Currently, this is blocked because:

  • importing the unit imports the generated client, there is no straightforward way to stub ES module dependencies to prevent that;
  • the generated client imports the default client;
  • the default client instance is created upon import;
  • the client constructor attempts to restore the refresh token from localStorage synchronously, which throws.

Suggested solution: postpone restoring the refresh token to the first call instead of doing that right in the client constructor. That should enable stubbing the method before it calls browser APIs.

platosha avatar Dec 04 '18 13:12 platosha

perhaps vaadin-connect could check whether window.locaStorage is available in the runtime before trying to use it.

manolo avatar Dec 05 '18 07:12 manolo