【问题标题】:ReferenceError: VUE_APP_CONFIG is not defined Jest unit tests failingReferenceError: VUE_APP_CONFIG 未定义 Jest 单元测试失败
【发布时间】:2021-06-12 12:11:29
【问题描述】:
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;

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

【问题讨论】:

    标签: javascript typescript vue.js


    【解决方案1】:

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

    globals: {
        VUE_APP_CONFIG: true,
    },
    

    一切正常

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

      • 我的代码在浏览器上运行,但运行 jest unit 时失败
      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 2018-02-26
      • 2019-07-09
      • 2018-07-20
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2015-10-04
      相关资源
      最近更新 更多