【发布时间】:2020-07-21 22:12:18
【问题描述】:
在过去的几个小时里,我一直在为开玩笑测试中的一个错误而苦苦挣扎。全局变量不会让我测试我的组件。我尝试以多种方式模拟变量,但没有任何效果。我尝试在 package.json 中将“全局”属性添加到我的 jest 配置中(没有 ork)。我尝试在我的测试文件中模拟变量(没有用)。有人可以帮我看看吗?
这是我遇到的错误
● Test suite failed to run
TypeError: Cannot read property 'raven_dsn' of undefined
20 | }
21 | }
> 22 | setExtraContext(extraContex){
23 | if (global.context.raven_dsn) {
24 | Raven.setExtraContext(extraContex);
25 | }
at new Logger (realistico/frontend/react/libraries/Logger.js:22:28)
at Object.<anonymous> (realistico/frontend/react/libraries/Track.js:14:14)
at Object.<anonymous> (realistico/realpano/react/components/Viewer.jsx:35:14)
at Object.<anonymous> (realistico/newcms/react/components/StartView.js:19:15)
at Object.<anonymous> (realistico/newcms/react/components/ViewManager.js:26:18)
at Object.<anonymous> (realistico/newcms/react/components/HouseDetail.js:36:20)
at Object.<anonymous> (__tests__/House.test.js:27:20)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.112s
Ran all test suites.
这就是我实际可以模拟的全局变量的样子
(global).gettext = jest.fn(); // ==> DOES WORK
(global).window.context = jest.fn(); // ==> DOES WORK
(global).onAddhouse = jest.fn(); // ==> DOES WORK
(global).context = jest.fn(() => {}); // ==> DOESN'T WORK
【问题讨论】:
-
你能提供一个最小的、完整的、可重现的代码吗?