How to resolve local storage not defined in vue.js?

1.2k views Asked by At

when i try to run test cases in vue.js it shows me the error local storage is not defined ,How to resolve this issue please help me to fix this issue.

[Login.spec.js]

import  Login from '../../src/Pages/Login.vue';
import{shallowMount} from '@vue/test-utils';
describe('Login.vue',()=>{
    describe('When Loaded',()=>{
        it('has the required elements',()=>{
            const wrapper =shallowMount(Login);
            expect(wrapper.find('#input-username').exists()).toBe(false);
            expect(wrapper.find('#input-password').exists()).toBe(false);
        });
    });
});

[axios.js]

import axios from 'axios'

axios.defaults.baseURL=process.env.VUE_APP_AXIOS_URL
axios.defaults.headers.common['Authorization']='Bearer'+ localStorage.getItem('token');

export default class AxiosService{
   postData(url,data){
       return axios.post(url,data).then(response =>{
           return response;
       }).catch(error=>{
           return error;
       })
   }
}
1

There are 1 answers

0
Giovanni On

This might be a good solution to mock your localStorage: How do I deal with localStorage in jest tests?

Basically, there is a store being used to mock the localStorage. This way you can set the token based on a fake or real authentication method, which is up to you.