Mock AsyncStorage for react-native jest unit tests

Jest Mock AsyncStorage for react-native

Its a mock of react-native AsyncStorage for jest tests.

Install

NPM

  • Install: npm install --save mock-async-storage
  • Module: require('mock-async-storage')
  • Editions

    This package is published with the following editions:

    • mock-async-storage/src/index.js is Source + ESNext + Import + Flow
    • mock-async-storage aliases mock-async-storage/lib/index.js
    • mock-async-storage/lib/index.js is Babel Compiled + ES2015 + Require

    Older environments may need Babel's Polyfill or something similar.

    mock-async-storage@2.x.x

    Whats the main difference?

    I removed the jest specific part from the mock lib. In the next version the mocking method is not predefined. You can use any kind of library (sinon) for use this mock.

    Usage

    Manual mocks

    I suggest to use jest manual mocks Jest Manual Mocks For demonstrate this solution you can find an example in examples folder.

    Another mocking solution

    import MockAsyncStorage from 'mock-async-storage';
    // or import { mock, release } from 'mock-async-storage';
    // mock();
    // release();
    
    const mock = () => {
      const mockImpl = new MockAsyncStorage()
      jest.mock('AsyncStorage', () => mockImpl)
    }
    
    const release = () => jest.unmock('AsyncStorage')
    
    mock();
    
    // For unmock
    mockStorage.release();
    

    Working example:

    import 'react-native';
    import MockAsyncStorage from 'mock-async-storage'
    import React from 'react';
    import Index from '../index.android.js';
    
    // Note: test renderer must be required after react-native.
    import renderer from 'react-test-renderer';
    
    const mock = () => {
      const mockImpl = new MockAsyncStorage()
      jest.mock('AsyncStorage', () => mockImpl)
    }
    
    mock();
    
    import { AsyncStorage as storage } from 'react-native'
    
    it('renders correctly', () => {
      const tree = renderer.create(
        <Index />
      );
    });
    
    it('Mock Async Storage working', async () => {
      await storage.setItem('myKey', 'myValue')
      const value = await storage.getItem('myKey')
      expect(value).toBe('myValue')
    })
    

    mock-async-storage@1.x.x

    Usage

    In your test codes:

    const mockStorage = require('mock-async-storage');
    // or import { mock, release } from 'mock-async-storage';
    // mock();
    // release();
    
    // For mock AsyncStorage
    mockStorage.mock();
    
    // For unmock
    mockStorage.release();
    

    Working example:

    import 'react-native';
    import { mock, release } from 'mock-async-storage'
    import React from 'react';
    import Index from '../index.android.js';
    
    // Note: test renderer must be required after react-native.
    import renderer from 'react-test-renderer';
    
    mock()
    
    import { AsyncStorage as storage } from 'react-native'
    
    it('renders correctly', () => {
      const tree = renderer.create(
        <Index />
      );
    });
    
    it('Mock Async Storage working', async () => {
      await storage.setItem('myKey', 'myValue')
      const value = await storage.getItem('myKey')
      expect(value).toBe('myValue')
    })
    

    GitHub