vue.js - 如何在 Jest 测试中使用 nuxt 运行时配置变量

标签 vue.js jestjs vue-test-utils nuxtjs

我在 nuxt.config.js 中定义了私有(private)运行时配置。作为 Jest 测试的一部分,当我尝试测试具有运行时配置变量的方法时,它会抛出 undefined variable 错误。有人可以帮我知道如何在 Jest 测试中使用 nuxt 运行时配置吗?

最佳答案

只需提供模拟,如下所示:

import { mount } from '@vue/test-utils'
import MyComponent from '@/components/MyComponent.vue'

describe('MyComponent', () => {
    test('is a Vue instance', () => {
        const wrapper = mount(MyComponent, {
            mocks: {
                $config: {
                    MyProp: 'some value'
                }
             }
        })
        expect(wrapper).toBeTruthy()
    })
})

关于vue.js - 如何在 Jest 测试中使用 nuxt 运行时配置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63616768/

相关文章:

javascript - '命令未找到 : jest'

reactjs - Jest/ enzyme : Error: Uncaught [TypeError: Cannot read property 'query' of undefined] on component wrapped in withRouter

vue.js - @Vue/test-utilsshallowMount 为 vuetify 组件输出 "[Vue warn]: Unknown custom element"

vue.js - 使用 moxios 和 fakeTimers 测试去抖异步请求

vue.js - 是否可以在 VueJS 中指定应该在 router.go() 上使用哪个组件

javascript - Nuxt JS 如何正确使用 Vuex 模块模式?

vue.js - 像对象这样的 Prop 的默认值不起作用

javascript - Vue.js 随机播放组件

javascript - jest-resolve 嵌套模块解析错误路径 : Cannot find module 'react-dnd' from 'reactTags.js'

javascript - vue测试如何让localStorage正常工作