title: React Native + React Native Keychain + Jest: How to correctly mock React Native Keychain?
body: <p>I'm trying to write unit tests for my helper functions for React-Native-Keychain.</p>
  
  <p>Here are these helper functions:</p>
  
  <pre><code>import {
    setGenericPassword,
    getGenericPassword,
    resetGenericPassword
  } from "react-native-keychain";
  
  export const setToken = token =&gt; setGenericPassword("session", token);
  export const getToken = () =&gt; getGenericPassword().then(creds =&gt; creds.password);
  export const clearToken = () =&gt; resetGenericPassword();
  </code></pre>
  
  <p>And here are my tests:</p>
  
  <pre><code>import * as keyChainFunctions from "react-native-keychain";
  import { setToken, getToken, clearToken } from "./secureStorage";
  
  const token = "abcdefghijklmnopqrstuvwxyz0123456789";
  
  jest.mock("react-native-keychain", () =&gt; {
    const token = "abcdefghijklmnopqrstuvwxyz0123456789";
    const credentials = {
      username: "session",
      token: token
    };
    return {
      setGenericPassword: jest.fn(
        (username, password) =&gt; new Promise((resolve, reject) =&gt; resolve(true)) // eslint-disable-line no-unused-vars
      ),
      getGenericPassword: jest.fn(() =&gt; new Promise((resolve, reject) =&gt; resolve(credentials))), // eslint-disable-line no-unused-vars
      resetGenericPassword: jest.fn(() =&gt; new Promise((resolve, reject) =&gt; resolve(true))) // eslint-disable-line no-unused-vars
    };
  });
  
  describe("set token", () =&gt; {
    it("saves the token in the keychain", () =&gt; {
      expect.assertions(1);
      return setToken(token).then(() =&gt; {
        expect(keyChainFunctions.setGenericPassword).toHaveBeenCalledWith("session", token);
      });
    });
  });
  
  describe("get token", () =&gt; {
    it("retrieves the token from keychain", () =&gt; {
      expect.assertions(1);
      return getToken().then(() =&gt; {
        expect(keyChainFunctions.getGenericPassword).toHaveBeenCalled();
      });
    });
  });
  
  describe("clear token", () =&gt; {
    it("removes the token from the keychain", () =&gt; {
      expect.assertions(1);
      return clearToken().then(() =&gt; {
        expect(keyChainFunctions.resetGenericPassword).toHaveBeenCalled();
      });
    });
  });
  </code></pre>
  
  <p>As you can see I tried to mock the functions from React-Native-Keychain. But these tests fail, as they say no assertions happened. But I have to write these assertions since the helper functions are (/should be) asynchrous.</p>
  
  <p>How am I supposed to mock the keyChainFunctions? Or is there a better way to test these helper functions?</p>
  
  <h2>Edit</h2>
  
  <p>Just as Xarvalus requested, here are the errors:</p>
  
  <pre><code>npm test -t secureStorage
  
  &gt; ordersome@0.0.1 test /Users/jan/Startup/react-native/ordersome
  &gt; jest "secureStorage"
  
   FAIL  app/api/secureStorage/secureStorage.test.js
    set token
      ✕ saves the token in the keychain (60ms)
    get token
      ✕ retrieves the token from keychain (1ms)
    clear token
      ✕ removes the token from the keychain (1ms)
  
    ● set token › saves the token in the keychain
  
  
      expect(jest.fn())[.not].toHaveBeenCalledWith()
  
      jest.fn() value must be a mock function or spy.
      Received: undefined
  
        23 |     expect.assertions(1);
        24 |     return setToken(token).then(() =&gt; {
      &gt; 25 |       expect(keyChainFunctions.setGenericPassword).toHaveBeenCalledWith("session", token);
           |                                                    ^
        26 |     });
        27 |   });
        28 | });
  
        at app/api/secureStorage/secureStorage.test.js:25:52
        at tryCallOne (node_modules/promise/lib/core.js:37:12)
        at node_modules/promise/lib/core.js:123:15
        at flush (node_modules/asap/raw.js:50:29)
  
    ● set token › saves the token in the keychain
  
      expect.assertions(1)
  Expected one assertion to be called but received zero assertion calls.
  
        21 | describe("set token", () =&gt; {
        22 |   it("saves the token in the keychain", () =&gt; {
      &gt; 23 |     expect.assertions(1);
           |            ^
        24 |     return setToken(token).then(() =&gt; {
        25 |       expect(keyChainFunctions.setGenericPassword).toHaveBeenCalledWith("session", token);
        26 |     });
  
        at Object.&lt;anonymous&gt; (app/api/secureStorage/secureStorage.test.js:23:12)
  
    ● get token › retrieves the token from keychain
  
  
      expect(jest.fn())[.not].toHaveBeenCalled()
  
      jest.fn() value must be a mock function or spy.
      Received: undefined
  
        32 |     expect.assertions(1);
        33 |     return getToken().then(() =&gt; {
      &gt; 34 |       expect(keyChainFunctions.getGenericPassword).toHaveBeenCalled();
           |                                                    ^
        35 |     });
        36 |   });
        37 | });
  
        at app/api/secureStorage/secureStorage.test.js:34:52
        at tryCallOne (node_modules/promise/lib/core.js:37:12)
        at node_modules/promise/lib/core.js:123:15
        at flush (node_modules/asap/raw.js:50:29)
  ● get token › retrieves the token from keychain
  
      expect.assertions(1)
  
      Expected one assertion to be called but received zero assertion calls.
  
        30 | describe("get token", () =&gt; {
        31 |   it("retrieves the token from keychain", () =&gt; {
      &gt; 32 |     expect.assertions(1);
           |            ^
        33 |     return getToken().then(() =&gt; {
        34 |       expect(keyChainFunctions.getGenericPassword).toHaveBeenCalled();
        35 |     });
  
        at Object.&lt;anonymous&gt; (app/api/secureStorage/secureStorage.test.js:32:12)
  
  
    ● clear token › removes the token from the keychain
  
      expect(jest.fn())[.not].toHaveBeenCalled()
  
      jest.fn() value must be a mock function or spy.
      Received: undefined
  
        41 |     expect.assertions(1);
        42 |     return clearToken().then(() =&gt; {
      &gt; 43 |       expect(keyChainFunctions.resetGenericPassword).toHaveBeenCalled();
           |                                                      ^
        44 |     });
        45 |   });
        46 | });
  
        at app/api/secureStorage/secureStorage.test.js:43:54
        at tryCallOne (node_modules/promise/lib/core.js:37:12)
        at node_modules/promise/lib/core.js:123:15
        at flush (node_modules/asap/raw.js:50:29)
  ● clear token › removes the token from the keychain
  
      expect.assertions(1)
  
      Expected one assertion to be called but received zero assertion calls.
  
        39 | describe("clear token", () =&gt; {
        40 |   it("removes the token from the keychain", () =&gt; {
      &gt; 41 |     expect.assertions(1);
           |            ^
        42 |     return clearToken().then(() =&gt; {
        43 |       expect(keyChainFunctions.resetGenericPassword).toHaveBeenCalled();
        44 |     });
  
        at Object.&lt;anonymous&gt; (app/api/secureStorage/secureStorage.test.js:41:12)
  
  Test Suites: 1 failed, 1 total
  Tests:       3 failed, 3 total
  Snapshots:   0 total
  Time:        1.961s
  Ran all test suites matching /secureStorage/i.
  npm ERR! Test failed.  See above for more details.
  </code></pre>