title: How to spy on constructor using Jasmine and TypeScript
body: <p>Is there a way how to spy on constructor using Jasmine and Typescript?</p>
  
  <p>Proposed solutions in question <a href="https://stackoverflow.com/questions/9347631/spying-on-a-constructor-using-jasmine">Spying on a constructor using Jasmine</a> do not work because <a href="https://github.com/Microsoft/TypeScript/issues/19816" rel="nofollow noreferrer">TypeScript does not expose global declarations on <code>window</code></a>.</p>
  
  <p>Here is my use case:</p>
  
  <pre><code>// higher-order function returns validation function with max length
  const maxLengthValidator = maxLength =&gt; value =&gt; value.length &gt; maxLength;
  
  class Book {
    // will be a different function instance each time the Book object is
    // created causing the following test to fail
    nameValidator = maxLengthValidator(42);
    name = null;
    constructor (name) { this.name = name; }
  }
  
  class Library {
    persons = [];
    storeBook (book) { this.persons.push(book); }
    createBook (name) { this.storeBook(new Book(name)); }
  }
  </code></pre>
  
  <p>Failing test:</p>
  
  <pre><code>it('createBook passes new book with provided name to storeBook', () =&gt; {
    const lib = new Library();
    spyOn(lib, 'storeBook');
    lib.createBook('Start with Why');
    expect(lib.storeBook).toHaveBeenCalledWith(new Book('Start with Why'));
    // Result: Expected spy storeBook to have been called with
    // [ Book({ name: 'Start with Why', nameValidator: Function }) ]
    // but actual calls were
    // [ Book({ name: 'Start with Why', nameValidator: Function }) ]
  });
  </code></pre>
  
  <p>In the test, I don't really care what constructor does. I only need to verify that it has been called with the right parameter. That even sounds like a right use case for a mock.</p>