How do I deal with localStorage in jest tests?

后端 未结 17 1685
长情又很酷
长情又很酷 2020-11-28 02:01

I keep getting \"localStorage is not defined\" in Jest tests which makes sense but what are my options? Hitting brick walls.

相关标签:
17条回答
  • 2020-11-28 02:55

    Riffed off some other answers here to solve it for a project with Typescript. I created a LocalStorageMock like this:

    export class LocalStorageMock {
    
        private store = {}
    
        clear() {
            this.store = {}
        }
    
        getItem(key: string) {
            return this.store[key] || null
        }
    
        setItem(key: string, value: string) {
            this.store[key] = value
        }
    
        removeItem(key: string) {
            delete this.store[key]
        }
    }
    

    Then I created a LocalStorageWrapper class that I use for all access to local storage in the app instead of directly accessing the global local storage variable. Made it easy to set the mock in the wrapper for tests.

    0 讨论(0)
  • 2020-11-28 02:55
        describe('getToken', () => {
        const Auth = new AuthService();
        const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Ik1yIEpvc2VwaCIsImlkIjoiNWQwYjk1Mzg2NTVhOTQ0ZjA0NjE5ZTA5IiwiZW1haWwiOiJ0cmV2X2pvc0Bob3RtYWlsLmNvbSIsInByb2ZpbGVVc2VybmFtZSI6Ii9tcmpvc2VwaCIsInByb2ZpbGVJbWFnZSI6Ii9Eb3Nlbi10LUdpci1sb29rLWN1dGUtbnVrZWNhdDMxNnMtMzExNzAwNDYtMTI4MC04MDAuanBnIiwiaWF0IjoxNTYyMzE4NDA0LCJleHAiOjE1OTM4NzYwMDR9.YwU15SqHMh1nO51eSa0YsOK-YLlaCx6ijceOKhZfQZc';
        beforeEach(() => {
            global.localStorage = jest.fn().mockImplementation(() => {
                return {
                    getItem: jest.fn().mockReturnValue(token)
                }
            });
        });
        it('should get the token from localStorage', () => {
    
            const result  = Auth.getToken();
            expect(result).toEqual(token);
    
        });
    });
    

    Create a mock and add it to the global objectt

    0 讨论(0)
  • 2020-11-28 02:56

    To do the same in the Typescript, do the following:

    Setup a file with the following contents:

    let localStorageMock = (function() {
      let store = new Map()
      return {
    
        getItem(key: string):string {
          return store.get(key);
        },
    
        setItem: function(key: string, value: string) {
          store.set(key, value);
        },
    
        clear: function() {
          store = new Map();
        },
    
        removeItem: function(key: string) {
            store.delete(key)
        }
      };
    })();
    Object.defineProperty(window, 'localStorage', { value: localStorageMock });
    

    Then you add the following line to your package.json under your Jest configs

    "setupTestFrameworkScriptFile":"PATH_TO_YOUR_FILE",

    Or you import this file in your test case where you want to mock the localstorage.

    0 讨论(0)
  • 2020-11-28 02:58

    or you just take a mock package like this:

    https://www.npmjs.com/package/jest-localstorage-mock

    it handles not only the storage functionality but also allows you test if the store was actually called.

    0 讨论(0)
  • 2020-11-28 03:03

    I found this solution from github

    var localStorageMock = (function() {
      var store = {};
    
      return {
        getItem: function(key) {
            return store[key] || null;
        },
        setItem: function(key, value) {
            store[key] = value.toString();
        },
        clear: function() {
            store = {};
        }
      }; 
    })();
    
    Object.defineProperty(window, 'localStorage', {
     value: localStorageMock
    });
    

    You can insert this code in your setupTests and it should work fine.

    I tested it in a project with typesctipt.

    0 讨论(0)
提交回复
热议问题