gpt4 book ai didi

javascript - 使用 JEST 对 Vue.js 路由器进行单元测试 - 如何模拟页面?

转载 作者:行者123 更新时间:2023-11-30 19:20:07 24 4
gpt4 key购买 nike

我正在尝试对我的路由器路由进行单元测试,并且根据各种示例,我提出了一些测试用例。我已将我的路由器拆分为路由、守卫和路由器本身的单独模块,以便于测试,因此我实际上只在此处测试路由定义。这是我的路线示例...

import Guards from './guards'
import Views from '@/views'

export default [
{
path: '/',
props: true,
components: {
default: Views.Pages.Wizard,
header: Views.Headers.Wizard
},
children: [
{
path: '',
name: 'Welcome',
component: Views.Welcome
},
{
path: 'verify-guest',
name: 'ReCaptcha',
component: Views.ReCaptcha
}
]
},
{
path: '/:postcode/:id/:address',
props: true,
components: {
default: Views.Pages.Main,
header: Views.Headers.Main
},
children: [
{
path: '',
name: 'MyHome',
props: true,
component: Views.MyHome,
beforeEnter: Guards.requireSelectedProperty
},
{
path: 'improvements',
name: 'Improvements',
props: true,
component: Views.Improvements,
beforeEnter: Guards.requireSelectedProperty
}
]
}
]

我已经编写了一些测试和测试来加载登陆页面(欢迎)通过,但我尝试的其他一切似乎都失败了 - 就好像路由器只加载登陆页面一样。

测试看起来像这样......

import Helper from '../../test-helper'
import { mount, createLocalVue } from '@vue/test-utils'

import VueRouter from 'vue-router'

import App from '../../templates/app-bare.vue'
import Views from '@/views'

import routes from '@/router/routes.js'
import MockGuards from '@/router/guards.js'

jest.mock('@/router/guards.js', () => ({
requireUser: jest.fn(),
requireFullUser: jest.fn(),
requireCurrentProject: jest.fn(),
requireSelectedProperty: jest.fn()
}))

jest.mock('@/views/headers/main.vue', () => ({
name: 'MainHeader',
render: h => h('div')
}))

jest.mock('@/views/headers/wizard.vue', () => ({
name: 'WizardHeader',
render: h => h('div')
}))

jest.mock('@/views/welcome.vue', () => ({
name: 'Welcome',
render: h => h('span')
}))

jest.mock('@/views/my-home.vue', () => ({
name: 'MyHome',
render: h => h('section')
}))

const localVue = createLocalVue()
localVue.use(VueRouter)

describe('router', () => {
let router
let wrapper

beforeEach(() => {
router = new VueRouter({ base: '/', routes })
wrapper = mount(App, {
localVue,
router,
mocks: Helper.vueMockPlugins()
})
})

describe('can load the landing page', () => {

it('via a path', () => {
router.push('/')
expect(wrapper.find(Views.Welcome).exists()).toBeTruthy()
expect(wrapper.find(Views.Headers.Wizard).exists()).toBeTruthy()
})

it('via a name', () => {
router.push({ name: 'Welcome' })
expect(wrapper.find(Views.Welcome).exists()).toBeTruthy()
expect(wrapper.find(Views.Headers.Wizard).exists()).toBeTruthy()
})
})

/*

None of these work - can only ever make the router render 'Welcome'

describe('can load the root of my home', () => {

it('via a path', () => {
router.push('/KT18%207LJ/1/3%20The%20Crescent/')
console.log(wrapper.html())
expect(wrapper.find(Views.MyHome).exists()).toBeTruthy()
expect(wrapper.find(Views.Headers.Main).exists()).toBeTruthy()
})

it('via a name with parameters', () => {
router.push({ name: 'MyHome', params: {
id: 1,
postcode: 'KT18 7LJ',
address: '3 The Crescent'
} })
console.log(wrapper.html())
expect(wrapper.find(Views.MyHome).exists()).toBeTruthy()
expect(wrapper.find(Views.Headers.Main).exists()).toBeTruthy()
})
})

*/
})

有什么想法吗?我对使用 Vue 进行 JEST 和单元测试还很陌生——我之前对 Jasmine 和 Mocha/Chai/Sinon 有更多的经验。

最佳答案

最后我自己解决了这个问题。答案不是书页,而是我的守卫的 mock (当你看到它时很简单)。而不是这个...

jest.mock('@/router/guards.js', () => ({
requireUser: jest.fn(),
requireFullUser: jest.fn(),
requireCurrentProject: jest.fn(),
requireSelectedProperty: jest.fn()
}))

我需要用这个替换它们......

jest.mock('@/router/guards.js', () => ({
requireUser: jest.fn((_to, _from, next) => {
next()
}),
requireCurrentProject: jest.fn((_to, _from, next) => {
next()
}),
requireFullUser: jest.fn((_to, _from, next) => {
next()
}),
requireSelectedProperty: jest.fn((_to, _from, next) => {
next()
})
}))

因为,如果没有调用模拟守卫的“下一个”函数,就会被命中,然后路由不会执行超过该点。需要“下一个”调用才能实际通过路由的呈现。

“欢迎”似乎一直呈现,因为它是着陆页,因此在守卫失败的情况下默认返回。

关于javascript - 使用 JEST 对 Vue.js 路由器进行单元测试 - 如何模拟页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57558032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com