title: Jest basics: Testing function from component
body: <p>I have a basic function:</p>
  
  <p><em>components/FirstComponent:</em></p>
  
  <pre><code>sayMyName = (fruit) =&gt; {
      alert("Hello, I'm " + fruit);
      return fruit;
  }
  </code></pre>
  
  <p>When I try to test it with Jest inside <em>FirstComponent.test.js:</em></p>
  
  <pre><code>import FirstComponent from '../components/FirstComponent';
  
  describe('&lt;FirstComponent /&gt;', () =&gt; {
      it('tests the only function', () =&gt; {
          FirstComponent.sayMyName = jest.fn();
          const value = FirstComponent.sayMyName('orange');
          expect(value).toBe('orange');
      });
  });
  </code></pre>
  
  <p>Test says: <em>Comparing two different types of values. Expected string but received undefined.</em></p>
  
  <p>Apparently I'm not importing the function to test the right way?</p>
  
  <p>I was not smart enough to understand the Jest documents how to test functions from components..</p>
  
  <p>Is there some simple way to import function from component and test it?</p>
  
  <p><strong>Edit:</strong>
  This works now using the 'react-test-renderer'</p>
  
  <pre><code>import FirstComponent from '../components/FirstComponent';
  import renderer from 'react-test-renderer';
  
  describe('&lt;FirstComponent /&gt; functions', () =&gt; {
          it('test the only function', () =&gt; {
              const wrapper = renderer.create(&lt;FirstComponent /&gt;);
              const inst = wrapper.getInstance();
              expect(inst.sayMyName('orange')).toMatchSnapshot();
          });
  })
  </code></pre>