title: How do I mock an exported typescript function in a jasmine test?
body: <p>I'm trying to mock a function exported from a typescript file in a Jasmine test. I expect the following to mock the imported <code>foo</code> and return the value 1 in the spec for bar.</p>
  
  <p>The mock appears to be uncalled, so I'm clearly missing something. How can I fix this example?</p>
  
  <p>demo.ts:</p>
  
  <pre><code>export function foo(input: any): any {
    return 2;
  }
  
  export function bar(input: any): any {
    return foo(input) + 2;
  }
  </code></pre>
  
  <p>demo.ts.spec:</p>
  
  <pre><code>import * as demo from './demo';
  
  describe('foo:', () =&gt; {
    it('returns 2', () =&gt; {
      const actual = demo.foo(1);
      expect(actual).toEqual(2);
    });
  });
  
  describe('bar:', () =&gt; {
    // let fooSpy;
    beforeEach(() =&gt; {
      spyOn(demo, 'foo' as any).and.returnValue(1); // 'as any' prevents compiler warning
    });
  
    it('verifies that foo was called', () =&gt; {
      const actual = demo.bar(1);
      expect(actual).toEqual(3); // mocked 1 + actual 2
      expect(demo.foo).toHaveBeenCalled();
    });
  });
  </code></pre>
  
  <p>Failures:</p>
  
  <ul>
  <li>Expected 4 to equal 3.</li>
  <li>Expected spy foo to have been called.</li>
  </ul>