单元测试 jest-react 中未定义广播频道 API

问题描述 投票:0回答:0

我一直在尝试模拟这个 BroadcastChannel 并将其重新分配给我的测试文件中的 window.BroadcastChannel。但我一直说 undefined 尝试了 StackOverflow 的其他解决方案仍然是 undefined

有人遇到过吗?

我试过下面这段代码:

    const mockChannel = () => ({
        onmessage: jest.fn(),
        close: jest.fn()
    });
    window.BroadcastChannel = jest.fn().mockImplementation(mockChannel);

但我仍然得到同样的错误。

reactjs typescript unit-testing jestjs ts-jest
© www.soinside.com 2019 - 2024. All rights reserved.