Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating logging functions for spying after framework is loaded #917

Closed
cha55son opened this issue Mar 7, 2021 · 1 comment · Fixed by #918
Closed

Updating logging functions for spying after framework is loaded #917

cha55son opened this issue Mar 7, 2021 · 1 comment · Fixed by #918

Comments

@cha55son
Copy link
Contributor

cha55son commented Mar 7, 2021

Tangentially related to #592, I propose we update how we call the logging methods console.error and console.log so they can be properly spied upon during tests after the framework has loaded.

For context i'm using Angular CLI to generate an app. I then pull in boardgame.io and start writing tests. Angular CLI is using Typescript 4.1.2 and jasmine 3.6.0. When writing a test it looks something like this:

import { Client } from 'boardgame.io/client';
...


describe('MyGame', () => {
  beforeEach(() => {
    spyOn(console, 'error');
    game = Client({ game: MyGame });
    ...
  });

  it('should not do a thing', () => {
    game.moves.someMove();
    expect(console.error).toHaveBeenCalledWith('ERROR:', `disallowed move: someMove`);
  });
});

At the moment the expectation will fail because the boardgame code is being loaded before the spy can be created. Per #592 there is no other good way to determine if an invalid move has been made (without in depth state checking) so spying on the error logs is the only other approach. Although I suspect spying on the logs can provide other test benefits besides just invalid move testing. Since the boardgame code is binding directly to the console.error function reference there is no way to spy on it after the fact.

IF spying on logging functions after the codebase has been loaded is a valid use-case then I can think of two possible approaches:

  1. Allowing the client to provide both a log and error function which defaults to console.XYZ method, respectively.
  2. Update the logger implementation to reference a function that calls the console.XYZ methods.
@cha55son
Copy link
Contributor Author

cha55son commented Mar 7, 2021

Just want to point out that the reason these tests are working is because the logger is loaded after the console spies are created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant