【问题标题】:Export "MyModule" is not defined - on Jest tests未定义导出“MyModule” - 在 Jest 测试中
【发布时间】:2019-06-12 01:03:22
【问题描述】:

我正在尝试有选择地导出此模块,该模块在测试中失败,并在其他情况下导出一个空白模块(以便测试不会抱怨)。按照这个 SO 回答:Skip import when react component created by jest

let Analytics;
let Segment;
if (process.env.JEST_WORKER_ID == undefined){
  Analytics = require('@segment/analytics.js-core');
  Segment = require('@segment/analytics.js-integration-segmentio');
}
else {
  // If we're testing...
  Analytics = {
    page() {},
    track() {},
    identify() {},
    initialize() {},
    addIntegration() {},
  };
  Segment = {};
}    
console.log(Analytics);
export {Analytics};

如果我删除导出,当我运行 jest 测试时,console.log() 行会得到这个:

{ page: [Function: page],
  track: [Function: track],
  identify: [Function: identify],
  initialize: [Function: initialize],
  addIntegration: [Function: addIntegration] }

但是,当我进行导出时,我遇到了这个问题:

SyntaxError: index.js: Export 'Analytics' is not defined (36:8)

还有其他我应该导出的方式吗?知道为什么我会收到此语法错误吗?我会提到,当我在本地运行我的应用程序时,一切正常。

【问题讨论】:

  • 你是如何导入的?
  • 从'path/to/file'导入{Analytics};

标签: javascript reactjs jestjs


【解决方案1】:

导出时尝试明确命名导出,所以

export { Analytics as Analytics };

【讨论】:

  • 谢谢!这正是我需要的:)
猜你喜欢
  • 2022-06-30
  • 1970-01-01
  • 2021-04-24
  • 2020-07-13
  • 2016-07-04
  • 1970-01-01
  • 2017-07-20
  • 2018-02-26
  • 1970-01-01
相关资源
最近更新 更多