title: React Native how to mock PixelRatio on 1 specific test
body: <p>I am having trouble testing my snapshot in react-native, to be more specific it's the PixelRatio I am having troubles with.</p>
  
  <p>Code speaks more than words - I've simplified the code and removed all the noise:</p>
  
  <p><strong>The component:</strong></p>
  
  <pre><code>const Slide = (props) =&gt; (
    &lt;Image
      source={props.source}
          style={PixelRatio.get() === 2 ?
              { backgroundColor: 'red' } :
              { backgroundCoor: 'green' }}
    /&gt;
  );
  </code></pre>
  
  <p><strong>The Snapshot test</strong>:</p>
  
  <pre><code>import { Platform } from 'react-native';
  describe('When loading the Slide component', () =&gt; {
    it('should render correctly on ios', () =&gt; {
      Platform.OS = 'ios';
      const tree = renderer.create(
        &lt;Provider store={store}&gt;
          &lt;Slide /&gt;
        &lt;/Provider&gt;,
      ).toJSON();
      expect(tree).toMatchSnapshot();
    });
  
    describe('on devices with a pixelRatio of 2', () =&gt; {
      it('it should render correctly', () =&gt; {
        jest.mock('PixelRatio', () =&gt; ({
          get: () =&gt; 2,
          roundToNearestPixel: jest.fn(),
        }));
  
        const tree = renderer.create(
          &lt;Provider store={store}&gt;
            &lt;Slide /&gt;
          &lt;/Provider&gt;,
        ).toJSON();
        expect(tree).toMatchSnapshot();
      });
    });
  });
  </code></pre>
  
  <p>But this doesn't work and after some digging I found a <a href="https://github.com/facebook/jest/issues/2582" rel="nofollow noreferrer">bug on github</a> that was already resolved - apparently you need to use <a href="https://github.com/facebook/jest/issues/2582#issuecomment-284514827" rel="nofollow noreferrer">beforeEach</a>. But that seems also not to be working or I am doing it wrong?</p>
  
  <p><strong>The Snapshot test with the suggested solution of github</strong>:</p>
  
  <pre><code>import { Platform } from 'react-native';
  describe('When loading the Slide component', () =&gt; {
    it('should render correctly on ios', () =&gt; {
      Platform.OS = 'ios';
      const tree = renderer.create(
        &lt;Provider store={store}&gt;
          &lt;Slide /&gt;
        &lt;/Provider&gt;,
      ).toJSON();
      expect(tree).toMatchSnapshot();
    });
  
    describe('on devices with a pixelRatio of 2', () =&gt; {
      it('it should render correctly', () =&gt; {
        beforeEach(() =&gt; {
          jest.mock(pxlr, () =&gt; ({
            get: () =&gt; 2,
            roundToNearestPixel: jest.fn(),
          }));
          const pxlr = require('react-native').PixelRatio;
        }
  
        const tree = renderer.create(
          &lt;Provider store={store}&gt;
            &lt;Slide /&gt;
          &lt;/Provider&gt;,
        ).toJSON();
        expect(tree).toMatchSnapshot();
      });
    });
  });
  </code></pre>