我想用Jest为我的Nuxt.js应用程序编写单元测试。但是,我的一些组件使用在process.env文件中声明的nuxt.config.js属性。当我运行测试时,我收到了以下错误:

测试文件示例:
import Config from "../nuxt.config"
import { mount } from "@vue/test-utils"
import CleanMapButton from "../components/UI/buttons/CleanMapButton.vue"
beforeAll(() => {
process.env = Config.env
})
describe("Clean map button tests", () => {
it ('Always true test', () => {
expect(true).toBe(true)
})
})发布于 2019-03-02 04:12:30
进口货物悬挂,因此import语句都在beforeAll中设置process.env之前运行。
如果import-ed模块需要全局变量集,那么必须在测试开始运行之前设置它,方法是在安装模块中设置它,并配置Jest以使用类似于setupFilesAfterEnv的方法运行该安装模块。
另一方面,调用require在需要的时候运行代码,因此可以选择在beforeAll设置process.env之后重构您的测试代码来调用process.env。
发布于 2019-03-01 14:50:59
您可以在beforeAll中设置它们
beforeAll(() => {
process.env = Object.assign(process.env, { get_settings: 'get_settings' });
});https://stackoverflow.com/questions/54946221
复制相似问题