title: How to test JSON.parse in JEST
body: <p>i was trying to write the unit test using JEST for a react component which gets a JSON Object from locaStorage in componentWillMount function,</p>
  
  <h2>React component</h2>
  
  <pre><code>import React from 'react';
  
  export default class Test extends React.Component {
      constructor(props) {
          super(props);
          this.state = {
              sampJSON: null,
              username: null
          }
      }
      componentWillMount(){
          this.setState({
              sampJSON: JSON.parse(localStorage.getItem('JSONResponse') || '{}');
          });
          this.setState({
              username: sampJSON.username
          });
      }
      render(){
          return(){
              &lt;div&gt;
                  &lt;h1&gt; Hi {this.state.username} &lt;/h1&gt;
              &lt;/div&gt;
          }
      }
  }   
  </code></pre>
  
  <p>and here is my test code, </p>
  
  <pre><code>import React from 'react';
  import sinon from 'sinon';
  import Testing from './Testing.js';
  import TestUtils from 'react-addons-test-utils';
  
  jest.dontMock('Testing');
  jest.dontMock('sinon');
  
  describe('Testing Testing component', () =&gt; {
      var JSONData = {
          "username" : "Testing",
          "surName" : "surName",
          "email": "test@test.com"
      }
      beforeEach(function() {
          // window.localStorage.setItem
          var spy = sinon.spy(window.localStorage, "setItem");
  
          // You can use this in your assertions
          spy.calledWith('aKey', JSONData)
      });
      it('renders the Testing',() =&gt; {
          var stub = sinon.stub(window.localStorage, "getItem");
          stub.returns(JSONData);     
          var testCmp = TestUtils.renderIntoDocument(&lt;Testing /&gt;);
          expect(testCmp).toBeDefined();
      });
  });
  </code></pre>
  
  <p>when i run the this test i get an error as below,</p>
  
  <pre><code>- SyntaxError: Unexpected token o
          at Object.parse (native)
  </code></pre>