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

Posted

技术标签:

【中文标题】如何在 Jest 测试中使用 nuxt 运行时配置变量【英文标题】:How to use nuxt runtime config variables in Jest tests 【发布时间】:2020-12-16 09:12:05 【问题描述】:

我在 nuxt.config.js 中定义了私有运行时配置。作为开玩笑测试的一部分,当我尝试测试具有运行时配置变量的方法时,它会引发 undefined variable error 。有人可以帮我知道如何在开玩笑测试中使用 nuxt 运行时配置。

【问题讨论】:

【参考方案1】:

只提供模拟,像这样:

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

【讨论】:

我已经成功地模拟了像 $fireStore 这样的其他全局属性,但是 $config 由于某种原因似乎不起作用。还是会抛出“can't read environment of undefined”的错误。

以上是关于如何在 Jest 测试中使用 nuxt 运行时配置变量的主要内容,如果未能解决你的问题,请参考以下文章