reactjs - react onsenui Mocha 测试错误 "Error: Invalid state"

标签 reactjs mocha.js onsen-ui

在我将react-onsenui从0.7.3更新到1.0.0之后。 我使用 mocha 来测试我的网络应用程序。错误发生如下:

Error: Invalid state
at /Users/*****/node_modules/onsenui/js/onsenui.js:581:11

onsenui.js的代码是这样的:

throw new Error('Invalid state');

最佳答案

您如何运行 Mocha 测试?我在使用 Mocha 和 JSDOM 时看到了这个错误。这只是发生的众多错误之一,因为 OnsenUI 需要真实的浏览器环境,而 JSDOM 不是其中之一。

我的方法是在我定义 DOM 的 browser.js 文件中 stub OnsenUI 所需的所有内容。

第 581 行调用的代码正在 window.getCompulatedStyle(document.documentElement, '') 的结果中查找关键“transitionDuration”,如果找不到,则会出错。我将其添加到我的 browser.js 文件中:

//** Polyfill for values missing from JSDOM
window.getComputedStyle = function(el1, el2) {
  return [
   "transitionDuration"
  ]
}

这解决了这个特定的错误,但还有更多错误。

继续阅读有关使用 OnsenUI 组件运行 Mocha 测试的详细信息

JSDOM window 元素的属性无法用作 OnsenUI 的全局变量,因此我看到了很多错误,例如 Element is not DefinedNode未定义等等。我通过将它们与窗口属性(如果存在)匹配或 stub 空函数来解决这些问题,如下所示:

// browser.js
global['Element'] = window.Element;
global['HTMLElement'] = window.HTMLElement;
global['WebComponents'] = function() {};
global['Node'] = window.Node;
global['Window'] = window;
global['Viewport'] = function() { return { setup: function() {} } }

这仍然不足以让它运行。为了解决与 Web 组件和自定义元素相关的错误,我安装了 document-register-element,并将其导入到测试的顶部。我还需要从 https://github.com/megawac/MutationObserver.js 导入 MutationObserver ,像这样:

//shims.js
import './shims/mutationobserver';
global['MutationObserver'] = window.MutationObserver;

最后我的测试文件如下所示:

import 'babel-polyfill'
import React from 'react';
import { mount, shallow } from 'enzyme';
import {expect} from 'chai';
import document from './helpers/browser';
import './helpers/shims';
import 'document-register-element';

import Frame from '../react-app/components/frame';

describe('<Frame />', function () {
  it('renders without problems', function () {
    var wrapper = shallow(<Frame />);
    expect(wrapper.find('iframe')).to.have.length(1);
  }); 
});

以下是 browser.js 的全文:

 import { jsdom } from 'jsdom';

 //** Create a fake DOM to add the tests to
 const document = jsdom('<!doctype html><html><body></body></html>');
 const window = document.defaultView;

 //** Push the window object properties to the Mocha global object- no idea why it doesn't work for all of the properties
 Object.keys(window).forEach((key) => {
   if (!(key in global)) {
     global[key] = window[key];
   }
 });

 //** These ones need to be done manually
 global['Element'] = window.Element;
 global['HTMLElement'] = window.HTMLElement;
 global['WebComponents'] = function() {};
 global['Node'] = window.Node;
 global['Window'] = window;
 global['Viewport'] = function() { return { setup: function() {} } }

 //** Polyfill for values missing from JSDOM
 window.getComputedStyle = function(el1, el2) {
   return [
     "transitionDuration"
   ]
 }

 global.document = document;
 global.window = window;

关于reactjs - react onsenui Mocha 测试错误 "Error: Invalid state",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39843192/

相关文章:

javascript - React useState 重新渲染次数过多

reactjs - Props 父组件到父子组件

javascript - before() 不在后续的destroy() 之前执行?

html - 如何使 onsen ui 按钮栏在页面上居中

javascript - Node.js - 如何检查请求数据?

javascript - React/Redux 访问 Prop

javascript - 在使用 Mocha/Chai 的函数中使用某些参数时如何正确抛出 TypeError

javascript - 在使用 mocha 的测试脚本中从 webhook 接收通知事件

css - Ons-List-Item 自动高度调整

html - 如何在 Onsen UI 工具栏中有多个 p 标签?