Uma U
Uma U

Reputation: 51

mock router module jest

In apiFile.js,

import router from './router';

myFn(){
const url = router.app.$appSettings.redirectUrl 
}

./router file,

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{...}]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

in unit test, how do I mock router file, to mock value of router.app.$appSettings.redirectUrl

Upvotes: 1

Views: 548

Answers (2)

SakisTsalk
SakisTsalk

Reputation: 875

in jest you can mock a module in your directory using jest.mock(path, moduleFactory).

In your case something like that will work.

jest.mock('{`${relativePath}`/Router.js}', () => {
      return {
        router: {
          app: {
            $appSettings: {
              redirectUrl: `your mock value`
            }
          };
        });
    }

Upvotes: 2

Fernando
Fernando

Reputation: 788

You can create an object that mocks your route

const ROUTE_MOCK = {
  router: {
    app: {
      $appSettings: {
        redirectUrl: 'your url here'
      }
    }
  }
}

and then call jest to mock

jest.mock('./router', () => ROUTE_MOCK)

Upvotes: 0

Related Questions