title: Angular testing function.bind()
body: <p>I'm having a rough time trying to test if some function is bound to a component after the component has been initialized.</p>
  
  <p>This is my ngOnInit() function:</p>
  
  <pre><code>ngOnInit() {
     this.someFunction = this.someFunction.bind(this);
  }
  </code></pre>
  
  <p>And this is the function that I want to bind to the component::</p>
  
  <pre><code>someFunction() {
     // this empty function is not called yet but should be bound to the component
  }
  </code></pre>
  
  <p>And this is my before each:</p>
  
  <pre><code>beforeEach(async(() =&gt; {
     fixture = TestBed.createComponent(ComponentName);
     component = fixture.componentInstance;
     fixture.detectChanges();
  }));
  </code></pre>
  
  <p>And this is my describe function:</p>
  
  <pre><code>describe('ngOnInit', () =&gt; {
    it('someFunction has been bound to the component.', () =&gt; {
      let bindFunctionSpy = spyOn(component.someFunction, 'bind').and.callThrough();
  
      component.ngOnInit();
  
      expect(bindFunctionSpy).toHaveBeenCalledWith(component);
    });
  });
  </code></pre>
  
  <p>The problem that I'm facing here is that there is a typescript error in the spyOn function preventing me from compiling the test cases, it says :</p>
  
  <blockquote>
    <p>error TS2345: Argument of type '"bind"' is not assignable to parameter of type 'never'.</p>
  </blockquote>
  
  <p>So what exactly am I not doing right here?</p>
  
  <p>The same thing happens if I try spying on any of the prototype functions for a component function like apply or call for example.</p>
  
  <p>Yet if I tried to spy on a prototype function for a component variable like length or toLowerCase it doesn't throw such error!</p>
  
  <p>Another note is that this test actually sometimes gets compiled successfully and actually passes, and sometimes it throws the error while compilingm, but it happens only when I make any random changes like adding a space then saving them  so that Karma can detect that a change happened and recompile the tests, but if I closed the terminal and then started it again and ran ng test I get the error again.</p>