Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 使用jest测试nuxt.js应用程序时访问'process.env'属性_Unit Testing_Vue.js_Jestjs_Nuxt.js_Babel Jest - Fatal编程技术网

Unit testing 使用jest测试nuxt.js应用程序时访问'process.env'属性

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

我想用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"

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')
我添加了测试文件源代码。看起来此错误是在方法调用之前强制执行的。我想当第三次导入执行时。