目前在我的项目中,我正在使用业力和摩卡
我目前有大约1200次测试,其中大部分是我的React组件库。
当这些测试进行时,镀铬通常超过2GB的存储器,其CPU使用率超过30%
我的大多数测试看起来像这样 -
const React = require('react');
const TestUtils = require('react-dom/test-utils');
const ReactDOM = require('react-dom');
const expect = require('chai').expect;
const Users = require('./../../../../../../client/components/contentComponents/Example.jsx');
const componentProps = () => {
return {
exampleProp: 'ExampleProp'
};
};
it('Example Test Block', () => {
it('Component should be rendered on the page', () => {
const objComponent = TestUtils.renderIntoDocument(React.createElement(Users, componentProps()));
const objComponentHtmlElement = ReactDOM.findDOMNode(objComponent);
expect(objComponentHtmlElement).to.not.be.undefined;
});
it('Example Test 1', () => {
const objComponent = TestUtils.renderIntoDocument(React.createElement(Example, compnentProps()));
expect(ExampleAssertion).to.equal(true);
});
});
这里有什么明显的会导致CPU和内存使用量如此之高吗?我是否看到此数量的测试的用法?
我可以看到,测试运行时,Chrome窗口一次填充了许多不同的渲染组件,似乎没有卸载它们,我可能会在测试中错过一步,在我的测试中,渲染的组件需要未贴上或破坏?
什么都没有立即跳出来,但是我确实想知道您的组件是否有一些零件在您的测试后剩下。这是福布斯文章中推荐的测试清理程序,可在角度测试中解决此方面的一个方面。它也可能适用于反应:
export function cleanStylesFromDOM(): void {
const head: HTMLHeadElement = document.getElementsByTagName('head')[0];
const styles: HTMLCollectionOf<HTMLStyleElement> | [] = head.getElementsByTagName('style');
for (let i: number = 0; i < styles.length; i++) {
head.removeChild(styles[i]);
}
}
afterAll(() => {
cleanStylesFromDOM();
});