Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用 jest 断言 windows 对象的值。例如:断言谷歌标签管理器的 window.dataLayer 值。
如何使用单元测试笑话框架来实现它?
如果你想调用一个你会在对象中找到的window.dataLayer对象,只需模拟你将调用的函数:
window.dataLayer
window.dataLayer = jest.fn(() => { push: jest.fn(), });
在你测试你的功能之前。
如果您想在多个测试中使用,您可以设置beforeAll
beforeAll