Unit testing 使用jest测试nuxt.js应用程序时访问'process.env'属性
我想用Jest为我的numxt.js应用程序编写单元测试。但是我的一些组件使用了Unit testing 使用jest测试nuxt.js应用程序时访问'process.env'属性,unit-testing,vue.js,jestjs,nuxt.js,babel-jest,Unit Testing,Vue.js,Jestjs,Nuxt.js,Babel Jest,我想用Jest为我的numxt.js应用程序编写单元测试。但是我的一些组件使用了numxt.config.js文件中声明的process.env属性。运行测试时,我收到以下错误: 测试文件示例: import Config from "../nuxt.config" import { mount } from "@vue/test-utils" import CleanMapButton from "../components/UI/buttons/CleanMapButton.vue" b
numxt.config.js
文件中声明的process.env
属性。运行测试时,我收到以下错误:
测试文件示例:
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)
})
})
你可以先把它们放进去
beforeAll(() => {
process.env = Object.assign(process.env, { get_settings: 'get_settings' });
});
因此,import
语句都在process.env
之前运行,并在beforeAll
中设置
如果导入-ed模块需要设置全局变量,则必须在测试开始运行之前设置该变量,方法是在设置模块中设置该变量,然后配置Jest以使用类似的方式运行该设置模块
另一方面,调用
require
会在需要时运行代码,因此另一种方法是重构测试代码,在before
之后调用require('../components/UI/buttons/CleanMapButton.vue')
我添加了测试文件源代码。看起来此错误是在方法调用之前强制执行的。我想当第三次导入执行时。