2017-05-03 42 views
1

我在main.js以下功能(装有node.exe):酶引用错误 - 无法访问窗口功能

window.onload =() => { 
    window.getString = function() { 
     <Do something> 
     return value; 
    }; 
} 

以下是我的测试情况下,使用酶和摩卡

import "jsdom-global/register"; 
import React from "react"; 
import {mount} from "enzyme"; 
import Sessions from "./Sessions"; 
describe("Testing Sessions Page",() => { 
    it('should work',() => { 
     let wrapper = mount(<Sessions/>); 
    }); 
}); 

在使用React框架编写的Sessions组件中,我使用getString方法。当我运行我的测试时,它给出ReferenceError: getString is not defined。如何使测试代码中的窗口对象可访问?

回答

0

由于您使用jsdom-global,窗口对象的定义,但的getString不大,所以无论是:

window.getString =() => {};

一)在测试中的描述块之前定义getString方法

二)存根getStringsinon,如果你想测试它的调用

sinon.stub(window, 'getString');

C)只是包括main.js在您的测试:

import './main.js';