【发布时间】:2017-10-09 15:27:54
【问题描述】:
这是我的__tests__/App.js 文件:
import React from 'react';
import ReactDOM from 'react-dom';
import App from '../src/containers/App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
// sanity check
it('one is one', () => {
expect(1).toEqual(1)
});
这是我运行yarn test时得到的输出:
FAIL __tests__/App.js
● renders without crashing
ReferenceError: document is not defined
at Object.<anonymous>.it (__tests__/App.js:6:15)
✕ renders without crashing (1ms)
✓ one is one
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 0.128s, estimated 1s
Ran all test suites related to changed files.
我是否需要导入另一个模块才能在此处使用document?
感谢您的帮助!
【问题讨论】:
-
文档在节点环境中不可用。你应该检查酶以测试反应成分airbnb.io/enzyme
-
你也可以使用 karma 来测试karma-runner.github.io/1.0/index.html
-
我无法具体回答,因为我从未尝试在 Jest 测试中使用文档对象,但 github.com/airbnb/enzyme 通常是测试 React 组件的 goto 工具,绝对值得一试。
-
太棒了 - 需要使用酶才能使其发挥作用!谢谢大家。
标签: javascript reactjs jestjs create-react-app