2016-04-22 49 views
-1

我使用 「AVA」 的框架反應測試,這裏是一片的package.json的:查看在與AVA測試的全局命名空間

"babel": { 
    "presets": [ 
     "es2015", 
     "stage-2", 
     "react" 
    ] 
    }, 
    "ava": { 
    "babel": "inherit", 
    "require": [ 
     "babel-register" 
    ] 
    } 

測試代碼:

import store from '../../app/redux/store'; 
import test from 'ava'; 
import * as c from '../../app/constants' 

test('languageReducer() should correctly change the dictionary', t => { 
    store.dispatch({ 
    type: c.LOCALIZE_CHANGE_DICTIONARY, 
    dictionary: { 
     a: 1, 
     b: 2, 
     c: 3 
    } 
    }); 

    t.DeepEqual(store.getState().localizeReducer.dictionary, {a: 1, b: 2, c: 3}); 

    t.is(10, 10); 
}); 

和有錯誤的聲音,如:

unsubscribeHistory = history.listen(function (location) { 
          ^

TypeError: Cannot read property 'listen' of undefined 
    at middleware  (/Users/nikita/WebstormProjects/crm_panel/node_modules/redux-simple- router/lib/index.js:72:33) 

我該怎麼做才能讓我的測試訪問全局命名空間(窗口,歷史等)? 謝謝

回答

0

看來你試圖使用瀏覽器模塊,它試圖使用瀏覽器全球history,而沒有實際在瀏覽器中運行測試。 AVA尚未在瀏覽器中運行,但您可以使用jsdom來提供虛假的瀏覽器環境。我們有一個recipe for this