title: Testing with Jest and Typescript using Mocks
body: <p>I am working with Typescript and Jest to try to test some components for my Angular and Ionic application, but the issue is not limited to Angular or Ionic.  As such, I am trying to get the mock functionality of Jest to work.</p>
  
  <p>I am simply creating a dummy class that I want to try to mock the responses of functions to see if I can override the behaviour.</p>
  
  <p>jest-mock.ts</p>
  
  <pre><code>export class AClass {
      constructor() { }
  
      GetOne():any {
          return  1;
      }
  
      GetTwo():any {
          return 2;
      }
  }
  </code></pre>
  
  <p>jest-mock.spec.ts</p>
  
  <pre><code>import { AClass } from './jest-mock';
  
  // const mockGet = jest.fn( () =&gt; { return 3; } );  // Tried this to return 3?
  const mockGet = jest.fn();
  jest.mock('./jest-mock', () =&gt; {
      return jest.fn().mockImplementation( () =&gt; {
          return { GetOne: mockGet };
      });
  });
  
  describe('Testing Jest Mock is working', () =&gt; {
      it('should support mocking out the component', () =&gt; {
          expect(mockGet).toBeTruthy();
          expect(mockGet).toBe(3);                // Mocked Value
      });
  });
  </code></pre>
  
  <p>I am simply trying to create a test that can change the result of the function, so that my mock will be used by other real test code to provide results for testing.</p>
  
  <p>When I try to create a class from the mock <code>TestObject = new AClass();</code></p>
  
  <pre><code>TypeError: _jestMock.AClass is not a constructor
  </code></pre>
  
  <p>With the test defined above, I get the following error:</p>
  
  <pre><code>expect(received).toBe(expected)
      Expected value to be (using Object.is):
        3
      Received: 
        [Function mockConstructor]
      Difference:
         Comparing two different types of values. Expected number but received function.
  </code></pre>