我正在尝试开始学习最先进的Web开发学习React/Redux.
现在我坚持让测试运行.出于某种原因,jest失败了
Task :frontend:test
yarn jest v1.0.2
$ "/Users/gunnar/git/app.oakstair.se/frontend/node_modules/.bin/jest"
FAIL src/containers/App/App.test.js
? Test suite failed to run
ReferenceError: window is not defined
at Object.<anonymous> (config/polyfills.js:18:1)
at next (native)
at process._tickCallback (internal/process/next_tick.js:109:7)
Run Code Online (Sandbox Code Playgroud)
谷歌搜索了一段时间没有任何成功......
我有一个模块,它为 DOM 操作提供了一些方便的功能,我正在尝试使用 Jest 和 jsdom 对其进行测试,但是在实例化它时我似乎做错了。
通过阅读其他问题和答案(例如这个),我了解到 Jest 的测试环境会自动设置为使用 jsdom 并且您需要替换global.document,但它无法像我目前拥有的那样工作。
这是重现问题的最小测试用例:
// domUtils.js
function el(id) {
return document.getElementById(id);
}
Run Code Online (Sandbox Code Playgroud)
和
// domUtils.test.js
import { JSDOM } from "jsdom";
import * as domUtils from "../src/domUtils";
describe("DOM operations", () => {
// Workaround for jsdom issue 2304 - needed for later localStorage tests
const url = "http://localhost";
const documentHTML = '<!DOCTYPE html><div id="lorem" class="test">Lorem ipsum</div>';
global.document = new JSDOM(documentHTML, { url });
test("Get an element from …Run Code Online (Sandbox Code Playgroud)