You need to enable JavaScript to run this app. ReactJS项目Jest Puppeteer测试报document未定义排查指南
优惠活动
大模型
产品
解决方案
定价
更多

ReactJS项目Jest Puppeteer测试报document未定义排查指南

问题场景
  • npm test此前可正常运行。间隔约一个月未维护测试后,执行npm test启动Jest-Puppeteer测试时,触发ReferenceError: document is not defined报错。
  • 即便移除测试代码中所有document相关逻辑,该报错仍会出现,因此疑似是Puppeteer相关配置问题,但暂时无法确定触发原因。
  • 检出一个月前的历史代码分支后测试可正常运行,由于当前代码变更量过大,难以直接追溯实际问题根源。
已尝试的解决方案
  • 升级Node版本
  • 重装npm依赖包
  • 将jest-puppeteer.config.js回退到历史版本
  • 为测试用例添加@jest-environment jsdom注解——该操作虽解决了document未定义问题,但会触发新的ReferenceError: page is undefined报错
核心诉求

希望找到无需从零重构项目的排查思路,若确实需要从零重构也可接受。

测试用例代码示例

这是一个基础的Jest测试文件

import "core-js/stable";
import "regenerator-runtime/runtime";
import {Provider} from "react-redux"
import mockState from "./mocks/mockState"
import configureStore from "redux-mock-store"
import ShallowRenderer from 'react-test-renderer/shallow'
import API from '../src/API'
import getNavigationResponse from '../src/nocks/getNavigation'
import expectedNavigationState from "./static/expectedNavigationState"
import pageObjects from "./static/pageObjects"
import utils from './utils'
import constants from '../src/constants'


describe('API tests', () => {
    beforeEach(async() => {
        await page.goto('http://localhost:3000');
        await page.setViewport({ width: 900, height: 600 });
        await page.goto('http://localhost:3000/');
        await page.evaluate(() => {
            document.getElementById('root').classList.add('animated-test');
        });
        await page.waitForSelector(pageObjects.navFactory);
    });

    // PASS
    test('API data to be in store', async () => {
        await page.waitForSelector(pageObjects.primaryNavLink);

        // get state from root
        const store = await utils.getStore();

        expect(store[0].navigation.urlHashMap).toEqual(expectedNavigationState);
    });

    test.todo('Make sure content==true on vanity urls (home)')

    test.todo('Make sure content==false on url items with children (visitor)')

    // PASS
    test('API cancel should cancel the API request', async () => {
        API.dispatch = () => {

        };
        API.fetch(constants.NAVIGATION_HREF, 'API_FETCH_TYPE_NAVIGATION');
        const promiseCanceled = API.cancel('API_FETCH_TYPE_NAVIGATION');
        expect(promiseCanceled).hasOwnProperty('promise');
        expect(promiseCanceled).hasOwnProperty('cancel');
    });
});
排查进展

第一次补充说明

根据排查线索,该ReferenceError疑似是Babel层报错,触发原因是Babel无法正确识别执行环境中的document对象。经追踪定位,问题根源来自项目引入的某第三方插件内部,目前已在该插件的GitHub仓库提交反馈;当前临时方案是注释对应测试用例,待后续时间充足时再查找正式解决方案。

第二次补充说明

若将<rootDir>/node_modules/react-json-view/dist/main.js添加到Babel配置的transformIgnorePatterns数组中,会触发新的报错:

ReferenceError: regeneratorRuntime is not defined

该现象较为异常,因为测试文件顶部已显式引入import "regenerator-runtime/runtime"。此排查方向看似接近问题根因,但仍有不确定项。尝试切换到已废弃的babel-polyfill方案验证时,又触发了TypeError: jest: failed to cache transform results报错,目前排查暂卡在该阶段。


内容的提问来源于stack exchange,提问作者Jacksonkr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.12.04 13:36:03