未定义导出“ MyModule”-在Jest测试中

时间:2019-06-12 01:03:22

标签: javascript reactjs jestjs

我正在尝试有选择地导出此模块,该模块在测试中失败,并以其他方式导出空白模块(以便测试不会出错)。遵循此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};

如果我删除了导出,那么在运行开玩笑的测试时,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)

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

1 个答案:

答案 0 :(得分:1)

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

subscribe