vaadin-connect
vaadin-connect copied to clipboard
Avoid accessing browser APIs in the frontend client constructor
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
localStoragesynchronously, 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.
perhaps vaadin-connect could check whether window.locaStorage is available in the runtime before trying to use it.