反应onsenui mocha测试错误"错误:无效状态"

hai*_*ang 2 mocha.js reactjs onsen-ui

在我将react-onsenui从0.7.3更新到1.0.0之后.我用mocha来测试我的webapp.发生错误,如下所示:

Error: Invalid state
at /Users/*****/node_modules/onsenui/js/onsenui.js:581:11
Run Code Online (Sandbox Code Playgroud)

onsenui.js的代码是这样的:

throw new Error('Invalid state');
Run Code Online (Sandbox Code Playgroud)

Ali*_*i H 5

你是如何运行摩卡测试的?我使用Mocha和JSDOM看到了这个错误.这只是众多错误中的一个,因为OnsenUI需要一个真正的浏览器环境,而JSDOM不是一个.

我的方法是在我定义DOM的browser.js文件中存根OnsenUI所需的一切.

第581行调用的代码在结果中查找键"transitionDuration" window.getComputedStyle(document.documentElement, ''),并在找不到它时发生错误.我把它添加到我的browser.js文件中:

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

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

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

JSDOM window元素的属性不能用作OnsenUI的全局变量,所以我看到了很多错误Element is not defined,Node is not defined等等.我通过将它们与window属性(如果存在)匹配来解决这些问题,或者将空函数存根,如下所示:

// 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() {} } }
Run Code Online (Sandbox Code Playgroud)

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

//shims.js
import './shims/mutationobserver';
global['MutationObserver'] = window.MutationObserver;
Run Code Online (Sandbox Code Playgroud)

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

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);
  }); 
});
Run Code Online (Sandbox Code Playgroud)

以下是全文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;
Run Code Online (Sandbox Code Playgroud)