title: How to unit test a function that returns nothing (but assigns values to variables)?
body: <p>How do you unit test functions that <strong>don't return</strong> anything, but <strong>do</strong> change variables' values?</p>
  <p>I've looked through many S.O. questions to try and find an answer; here are a couple slightly-educated guesses:</p>
  <ol>
  <li>Inside Jest's test(), run the function and then check to see if <code>variable = expectedValue</code></li>
  <li>This use-case doesn't require unit testing, it's a simple variable assignment</li>
  <li>Rewrite the function specifically so that it returns something</li>
  <li>Function shouldn't exist if it's not returning anything, just put the initialization code in parent function</li>
  </ol>
  <p>1 seems like the best answer; 2 could be right - I'm new and still figuring out <em>what exactly</em> needs to be tested; 3 feels like overkill for some use-cases; 4 can't always be right, sometimes you just need to split out functionality if only for readability.</p>
  <hr />
  <p><strong>My specific use-case:</strong></p>
  <p><code>initializeGame</code> is called within the <code>playGame</code> function. It doesn't return anything; it just sets some variables to their start conditions (player X's turn, empty board, etc.) How can I test that <code>gameTracker</code> was set to <code>[]</code>, for example?</p>
  <pre><code>let gameTracker = [], turn;
  const turnX = 'X', turnO = 'O';
  
  function playGame() {
    $(document).ready(function () {
      initializeGame();
  
      $('.square').click(function () {
        makeMove($(this));
        endTurn();
      });
    });
  }
  function initializeGame() {
    $('.start').css('display', 'none');
    $('.square').children('p').text('');
    gameTracker = [];
    turn = turnX;
    $('h1').text('Player ' + turn + ' Turn');
  }
  </code></pre>
  <p><strong>And the test:</strong></p>
  <pre><code>const tictactoe = require('./tictactoe');
  
  test('gameTracker should equal [] after running initializeGame()', () =&gt; {
      tictactoe.initializeGame();
      expect(gameTracker).toBe([]);
  })
  </code></pre>