【问题标题】:Acces to global namespace with ava testing使用 ava 测试访问全局命名空间
【发布时间】:2016-04-22 13:45:18
【问题描述】:

我正在使用“ava”框架进行react测试,这是一段package.json:

  "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)

我应该怎么做才能让我的测试访问全局命名空间(窗口、历史等)? 谢谢

【问题讨论】:

    标签: javascript unit-testing ava


    【解决方案1】:

    您似乎正在尝试使用浏览器模块,该模块尝试使用浏览器全局history,而实际上并未在浏览器中运行测试。 AVA 尚未在浏览器中运行,但您可以使用jsdom 提供假的浏览器环境。我们有一个recipe for this

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 2020-09-07
      • 2021-04-19
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 2019-10-22
      相关资源
      最近更新 更多