title: jest mock: how to mock random output with react-native-randombytes
body: <p>I'm trying to write a jest unit test by replacing <code>react-native-randombytes</code> as it uses some <code>NativeModules</code>.</p>
  
  <p>My error message:</p>
  
  <pre><code>Test suite failed to run
  
  TypeError: Cannot read property 'seed' of undefined
  
  &gt; 1 | const RandomBytes = jest.genMockFromModule('react-native-randombytes');
      |                          ^
    2 | 
    3 | const randomBytes = (l) =&gt; {
    4 |   let uint8 = new Uint8Array(l);
  
    at seed (node_modules/react-native-randombytes/index.js:15:21)
    at Object.init (node_modules/react-native-randombytes/index.js:57:1)
    at Object.genMockFromModule (__mocks__/react-native-randombytes.js:1:26)
  </code></pre>
  
  <p>I place a file <code>react-native-randombytes</code> inside <code>__mocks__</code> folder beside <code>node_modules</code></p>
  
  <pre><code>const RandomBytes = jest.genMockFromModule('react-native-randombytes');
  
  const randomBytes = (l) =&gt; {
    let uint8 = new Uint8Array(l);
    uint8 = uint8.map(() =&gt; Math.floor(Math.random() * 90)+10);
    return uint8;
  };
  
  const seed = randomBytes(4096);
  
  RandomBytes.randomBytes = randomBytes;
  RandomBytes.seed = seed;
  
  export default RandomBytes;
  </code></pre>
  
  <p>I opened up the library I would like to mock and I found that instead of a class, it has the following section of code get executed at the end of it's index.js file. <a href="https://github.com/mvayngrib/react-native-randombytes/blob/60f9767ab49afa88257a7eecf35d5962c60fc917/index.js#L57" rel="nofollow noreferrer">link</a></p>
  
  <pre><code>function init () {
    if (RNRandomBytes.seed) {
      let seedBuffer = toBuffer(RNRandomBytes.seed)
      addEntropy(seedBuffer)
    } else {
      seedSJCL()
    }
  }
  </code></pre>
  
  <p>It seems that using jest.genMockFromModule will trigger the init function so the whole mocking failed. What are the considerations that I should have to choose which ways of mocking methods to use? In the documentation, it lists out various methods, but when to do which methods is not clearly recommended.</p>
  
  <p>Should I use jest.fn()?</p>
  
  <p>Please advice.</p>
  
  <p>UPDATE 1:</p>
  
  <p>I tried the following out in test file</p>
  
  <pre><code>jest.mock('react-native-randombytes');
  const randomBytes = jest.fn().mockImplementation((l) =&gt; {
      let uint8 = new Uint8Array(l);
      uint8 = uint8.map(() =&gt; Math.floor(Math.random() * 90)+10);
      return uint8;
    });
  </code></pre>
  
  <p>result: it doesn't work. It had same error.</p>
  
  <p>UPDATE 2: change my file inside <strong>mock</strong> as followed</p>
  
  <pre><code>const R = jest.genMockFromModule('react-native-randombytes');
  
  R.randomBytes = (l) =&gt; {
    let uint8 = new Uint8Array(l);
    uint8 = uint8.map(() =&gt; Math.floor(Math.random() * 90)+10);
    return uint8;
  };
  
  R.init = () =&gt; {};
  
  export default R;
  </code></pre>
  
  <p>result: same error message. It still goes to original react-native-randombytes.</p>
  
  <p>UPDATE 3: just like update 1 but with some twist
  inspired by this <a href="https://bambielli.com/til/2018-01-07-mocking-constructors/" rel="nofollow noreferrer">post</a></p>
  
  <pre><code>jest.genMockFromModule('react-native-randombytes');
  // eslint-disable-next-line import/first
  import randomBytes from 'react-native-randombytes';
  
  randomBytes.mockImplementation((l) =&gt; {
      let uint8 = new Uint8Array(l);
      uint8 = uint8.map(() =&gt; Math.floor(Math.random() * 90)+10);
      return uint8;
    });
  </code></pre>
  
  <p>result: same error message.</p>