我是React的单元测试世界的新手,并且正在关注这个主题的egghead视频。在尝试浅渲染时,测试仅适用于我的一些组件。
但是,我的一些组件包含使用react-chartist和chartist库制作的图表。尝试对这些组件运行测试时,我收到此错误:
> kindred-web-healthreader@0.0.1 test /Users/HilaryMBP/Kindred/kindred-web-healthreader
> mocha './src/**/*.spec.js' --compilers js:babel/register
/Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/chartist/dist/chartist.js:958
}(window, document, Chartist));
^
ReferenceError: window is not defined
at /Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/chartist/dist/chartist.js:958:3
at Chartist.version (/Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/chartist/dist/chartist.js:11:22)
at Object.<anonymous> (/Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/chartist/dist/chartist.js:15:2)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:214:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/HilaryMBP/Kindred/kindred-web-healthreader/src/js/utils/ChartistUtils.js:13:17)
at Module._compile (module.js:425:26)
at normalLoader (/Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:199:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:216:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at /Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/mocha/lib/mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (/Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/mocha/lib/mocha.js:216:14)
at Mocha.run (/Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/mocha/lib/mocha.js:468:10)
at Object.<anonymous> (/Users/HilaryMBP/Kindred/kindred-web-healthreader/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
at node.js:974:3
这仅适用于其子项导入Chartist或react-chartist的组件。
这是我的测试文件无效,仅供参考:
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import expect from 'expect';
import expectJSX from 'expect-jsx';
import Pain from '../components/Pain/Pain.jsx';
expect.extend(expectJSX);
describe('Pain', () => {
it('should render', () => {
const renderer = TestUtils.createRenderer();
renderer.render(<Pain />);
const actual = renderer.getRenderOutput();
const expected = '';
expect(actual).toIncludeJSX(expected);
});
});
这里可能会发生什么?