boardgameio / boardgame.io

State Management and Multiplayer Networking for Turn-Based Games
https://boardgame.io
MIT License
9.99k stars 704 forks source link

Testing multiplayer in React Native throws Exception #554

Closed tonyhhart closed 4 years ago

tonyhhart commented 4 years ago

I am writing some tests for a game in React Native, and it seems the multiplayer testing does not work with debug enabled. I'm not using debug for anything in tests, but it me a while to figure it out. Maybe it should be on docs that debug is incompatible in tests?

It fails because of this line:

https://github.com/nicolodavis/boardgame.io/blob/79ebcc3e2bb0c08c28dff61615f56e8b297b8baf/src/client/client.js#L282

Error: document is not defined

Here's a test case thats shows the exception being thrown

test('debug true should throw error on react native', () => {
  function initGameWithDebug() {
    const spec = {
      game: RPSPoker,
      multiplayer: Local(),
      numPlayers: 2,
    };

    const p0 = Client({
      ...spec,
      playerID: '0',
      debug: true,
    });
    const p1 = Client({
      ...spec,
      playerID: '1',
      debug: true,
    });

    p0.start();
    p1.start();
  }

  expect(initGameWithDebug).toThrow(ReferenceError);

  function initGameWithoutDebug() {
    const spec = {
      game: RPSPoker,
      multiplayer: Local(),
      numPlayers: 2,
    };

    const p0 = Client({
      ...spec,
      playerID: '0',
      debug: false,
    });
    const p1 = Client({
      ...spec,
      playerID: '1',
      debug: false,
    });

    p0.start();
    p1.start();
  }

  expect(initGameWithoutDebug).not.toThrow(ReferenceError);
});
nicolodavis commented 4 years ago

Fixed in https://github.com/nicolodavis/boardgame.io/commit/aede3b6714cd0f6656fa86ed2926bee42900b9aa.

Available in v0.38. Let me know if this resolves the issue.

tonyhhart commented 4 years ago

It fixes. Thank you!