How to unit test a bootfile with Jest?



  • I would like to test axios response interceptors and routeguard, for which I have created boot files.

    How do I test these type of files in Jest? Since the logic lives in the export default callback.

    import {
      Notify
    } from 'quasar'
    
    import {
      i18n
    } from 'boot/i18n.js'
    import {
      axiosInstance
    } from 'boot/axios'
    
    export default ({
      router,
      store
    }) => {
      axiosInstance.interceptors.response.use(response => {
        return response
      }, error => {
        if (error.response.status === 401) {
          Notify.create({
            progress: true,
            message: i18n.t('response.status-code-401'),
            icon: 'error',
            color: 'white',
            textColor: 'primary'
          })
    
          setTimeout(() => {
            store.dispatch('auth/logout')
          }, 5000)
    
          return Promise.reject(error)
        }
        // TODO: Notify for other status codes
      })
    
      /**
       * Set route guard
       */
      router.beforeEach((to, from, next) => {
        const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
    
        if (requiresAuth) {
          store.dispatch('auth/fetchUser').then(() => {
            if (!store.getters['auth/isLoggedIn']) {
              next({
                path: '/login',
                query: {
                  redirect: to.fullPath
                }
              })
            } else {
              next()
            }
          })
        } else {
          next()
        }
      })
    }
    
    
    

Log in to reply