简体   繁体   English

模拟路由器模块笑话

[英]mock router module jest

In apiFile.js ,在 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在单元测试中,我如何模拟路由器文件,模拟 router.app.$appSettings.redirectUrl 的值

in jest you can mock a module in your directory using jest.mock(path, moduleFactory) .在开玩笑中,您可以使用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` } }; }); }

I hope this solves your issue.我希望这能解决你的问题。

You can create an object that mocks your route您可以创建一个模拟您的路线的 object

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

and then call jest to mock然后调用 jest 来模拟

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM