concept spy in category javascript

appears as: spies, spy, spy, The spy
Testing JavaScript Applications

This is an excerpt from Manning's book Testing JavaScript Applications.

  • Unit tests are designed to run against functions. They are essential to assert the quality of your software at the most granular level possible, providing quick and precise feedback. These tests import your functions, feed them input, and check the output against what you expected.
  • Spies record data related to the usage of a function without interfering in its implementation.
  • Now that you have a logger, modify the addToInventory function so that it logs whenever an item is added to the inventory.

    Listing 3.13. inventoryController.js
    const logger = require("./logger");
    
    const inventory = new Map();
    
    const addToInventory = (item, quantity) => {
      if (typeof quantity !== "number")
        throw new Error("quantity must be a number");
      const currentQuantity = inventory.get(item) || 0;
      const newQuantity = currentQuantity + quantity;
      inventory.set(item, newQuantity);
      logger.logInfo({ item, quantity }, "item added to the inventory");  #1
      return newQuantity;
    };
    
    module.exports = { inventory, addToInventory };

    You can see that the logging works by running `node’s REPL and executing the following code:

    JavaScript Application Design: A Build First Approach

    This is an excerpt from Manning's book JavaScript Application Design: A Build First Approach.

    Sometimes it’s not enough to mock values by hand, and in those more advanced case scenarios, using a library such as Sinon.js might come in handy. Sinon helps you easily test setTimeout delays, dates, XHR requests, and even set up fake HTTP servers to use in your tests. Using Sinon, it’s trivial to create functions called spies. Spies are functions that are prepared to tell you whether they’ve been called, how many times, and what arguments they were invoked with. As it turns out, you’ve already used a custom flavor of spies in listing 8.9, where we had a listener function that kept track of how many times it was called. Let’s see how using spies helps assert function calls.

    Listing 8.14. Creating spies with Sinon
    var test = require('tape'); var sinon = require('sinon'); test('user mapper returns a subset of user', function (t) { // Arrange var clock = sinon.useFakeTimers(); var cb = sinon.spy(); // Act mapper(123, cb); clock.tick(0); #1 var result = cb.args[0][1]; var actual = Object.keys(result).sort(); var expected = ['name', 'email'].sort(); // Assert t.plan(2); t.ok(cb.calledOnce); t.deepEqual(actual, expected); }); #1: This call will fire any setTimeout functions that had a delay of 0 milliseconds.

    8.1.11. Mocks, spies, and proxies

    Sometimes you want greater isolation, even though two parts of an application can’t be decoupled any further. The application might need to query a real database, fetch data using a service, or connect together different modules, or there may be some other reason why you can’t decouple the implementation. You can use a variety of different tools, such as mocks, spies, and proxies, to circumvent the testing issues introduced by tight coupling. Figure 8.2 depicts the issue and the solution provided by these stubs.