为了账号安全,请及时绑定邮箱和手机立即绑定

Jest 如何修改每个单元测试的全局对象值

Jest 如何修改每个单元测试的全局对象值

GCT1015 2021-09-04 17:25:55
我正在编写需要定义 window.location.href 的单元测试 第一个单元测试创建如下  describe('myMethod()', () => {    beforeEach(() => {      global.window = Object.create(window);      Object.defineProperty(window, 'location', {        configurable: true,        value: {          href: 'http://localhost:7777/mainPoint?param=val1',        },      });    });    it('should call with val1', () => {      myMethod();      expect(myService.detectURLCall).toHaveBeenCalledWith('http://localhost:7777/mainPoint?param=val1'); // passes    });  describe('mySecondMethod()', () => {    beforeEach(() => {      global.window = Object.create(window);      Object.defineProperty(window, 'location', {        configurable: true,        value: {          href: 'http://localhost:7777/mainPoint?param=val2',        },      });    });    it('should call with val2', () => {      myMethod();      expect(myService.detectURLCall).toHaveBeenCalledWith('http://localhost:7777/mainPoint?param=val2'); // fails, the received call was ending with =val1    })我知道 jest 单元测试并行运行,我尝试在 myMethod() 测试套件内的 afterEach() 中执行 clearMocks() 但它没有帮助,我的第二个测试套件仍然检测到我们在第一个测试套件中定义的内容..
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 301 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号