ReferenceError: VUE_APP_CONFIG 未定义 Jest 单元测试失败

时间:2021-03-15 08:15:04

标签: javascript typescript vue.js

export interface VueAppConfig {
    API_URL: string;
    API_URL_V2: string;
}

declare const VUE_APP_CONFIG: VueAppConfig;

export const APP_CONFIG = { ...VUE_APP_CONFIG } as const;

在上面的代码中出现引用错误。

2 个答案:

答案 0 :(得分:0)

declare tells the compiler that the variable already exists somewhere。它实际上并不是在创建变量。

如果您要创建变量,请删除 declare

鉴于名称 (VUE_APP_CONFIG),我猜您正在尝试读取环境变量,其中 VUE_APP_ prefix makes the variable avaiable in the production build。在这种情况下,请确保通过 process.env:

引用它
const VUE_APP_CONFIG = JSON.parse(process.env.VUE_APP_CONFIG) as VueAppConfig

答案 1 :(得分:0)

问题是:jest 没有收到这个值不知道为什么,但在更新 jest.config.js 以接受之后

globals: {
    VUE_APP_CONFIG: true,
},

一切开始正常