小编Yas*_*arg的帖子

Jest单元测试中的全局变量

我是Jest的新手,并试图为我现有的React App编写一些单元测试.我有一个全局变量window.CONFIG,它存储一些在app中不同位置使用的配置.此变量在登录HTML页面的脚本标记中初始化

现在我正在尝试编写一个依赖于此的辅助函数的测试,window.CONFIG并且在访问时始终未定义

这是代码:

config.js

export default window.CONFIG;
Run Code Online (Sandbox Code Playgroud)

应用程序/ helper.js

import config from "../config";

export default  {
  getCompanyURL(company) {
    return config.baseUrl + "/companies/" + company.id;
  },
}
Run Code Online (Sandbox Code Playgroud)

_ tests _/helpers-test.js

jest.dontMock('../app/helpers.js');

var helper = require('../app/helpers.js').default;

describe('Get company URL', function() {

 it('returns company url with company id appended', function() {
   expect(companies.getCompanyURL({id:     1})).toBe('test_base_url/companies/1');
 });
});
Run Code Online (Sandbox Code Playgroud)

config因为Get Company Url总是未定义.由于未加载浏览器登录页面window.CONFIG未初始化.如何config在Jest的单元测试中模拟这个模块?

提前致谢!!

unit-testing global-variables jestjs

5
推荐指数
1
解决办法
6106
查看次数

标签 统计

global-variables ×1

jestjs ×1

unit-testing ×1