No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    How to unit test a bootfile with Jest?

    Help
    2
    2
    180
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      Ceriel last edited by Ceriel

      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()
          }
        })
      }
      
      
      
      1 Reply Last reply Reply Quote 1
      • D
        DavidGo last edited by

        I also want to test my router guards. @Ceriel have you figured out how to do this?

        1 Reply Last reply Reply Quote 0
        • First post
          Last post