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

基思·杰克逊

我正在尝试对路由器的路由进行单元测试,并在各种示例之后,提出了一些测试用例。我已将路由器分为用于路由,防护和路由器本身的单独模块,以使其更易于测试,因此我实际上仅在此处测试路由定义。这是我的路线的一个示例...

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()
        })
    })

    */
})

有任何想法吗?我对JEST和Vue进行单元测试非常陌生-我在Jasmine和Mocha / Chai / Sinon方面拥有更多的经验。

基思·杰克逊

最后我自己解决了这个问题。答案不在于页面,而是嘲笑我的警卫(当您看到它时很简单)。代替这个...

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()
    })
}))

因为,如果没有“下一步”功能来调用模拟警戒线,那么该路线将无法执行。实际上需要通过“下一个”调用来传递路径。

“ Welcome”似乎一直在渲染,因为它一直是着陆页,因此如果守卫失败则默认返回。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Jest对Vue.js组件中的方法进行单元测试

在单元测试期间,如何使用vue-test-utils和jest模拟Vue Mixins?

在 Jest 单元测试期间,Vue 路由器的注入失败

如何在与Jest的react-native中使用模拟的fetch()对API调用进行单元测试

如何模拟Web服务器以使用Java进行单元测试?

如何使用Jasmine(Angular JS)对自定义装饰器进行单元测试

如何使用 Jest 对该控制器进行正确的单元测试

如何使用Mockito模拟静态方法以进行单元测试

如何使用mocha.js模拟依赖项类进行单元测试?

如何使用Jest对类星体应用进行单元测试?

如何使用Jest在Node.js中对uncaughtException和unhandledRejection进行单元测试

如何使用 CodeceptJS 对 JS 函数进行单元测试

Blazor服务器页面单元测试-如何模拟Blazor服务器页面中使用的辅助signalR客户端连接

Vue,Vuex:如何使用命名空间存储和模拟对组件进行单元测试?

如何使用Jest测试JSX Vue组件

如何使用Vue和Jest测试拖放

春季-使用模拟单元测试-如何在服务单元测试中模拟自定义收集器

如何对使用nuxt-i18n的Vue.js组件进行单元测试

使用模拟路由器进行测试时出现根未定义错误

如何为使用Vuex存储的Vue表单组件编写Jest单元测试?

如何在Node.js中对使用Promise和事件发射器的函数进行单元测试?

如何使用模拟框架模拟龙卷风协程函数进行单元测试?

如何使用将访问DB的bean对路由进行单元测试?

如何使用vue路由器进行分步导航?

使用Apache Mina作为模拟/内存SFTP服务器进行单元测试

如何使用 gorm 进行单元测试

如何使用Vue路由器更改页面图标+标题

我应该如何为角度ui路由器运行单元测试

如何使用 Jest 在单元测试中模拟 NODE_ENV