如何测试服务器端的debugOnly包

时间:2015-12-08 13:51:38

标签: testing meteor tinytest

我不明白如何测试一个debugOnly的包。 我的package.js非常简单:

 Package.describe({
  name: 'lambda',
  version: '0.0.1',
  debugOnly: true // Will not be packaged into the production build
});

Package.onUse(function(api) {
  api.versionsFrom('1.2.1');
  api.addFiles('lambda.js');
  api.export("Lambda", 'server');
});

Package.onTest(function(api) {
  api.use('tinytest');
  api.use('lambda');
  api.addFiles('lambda-tests.js', 'server');
});

我的lambda-test.js

Tinytest.add('example', function (test) {
  test.equal(Lambda.func(), true);
});

我的lambda.js

Lambda = {
     func: function() {
         return "Christmas";
     }
}

当我运行meteor test-packages时,它就会失败:未定义Lambda。如果我删除debugOnly: true测试通行证。那么如何使用tinytest测试我的包呢? 或者这是一个错误!

1 个答案:

答案 0 :(得分:1)

我有同样的问题!事实证明测试工作正常。 Lambda也没有在项目中导出。

来自https://github.com/meteor/meteor/blob/0f0c5d3bb3a5492254cd0843339a6716ef65fce1/tools/isobuild/compiler.js

// don't import symbols from debugOnly and prodOnly packages, because
// if the package is not linked it will cause a runtime error.
// the code must access them with `Package["my-package"].MySymbol`.

尝试:

Tinytest.add('example', function (test) {
  //also changed expected value from true to Christmas to make test pass
  test.equal(Package['lambda']['Lambda'].func(), "Christmas");
  //you can use Package['lambda'].Lambda as well, but my IDE complains
});

现在你可以这样做:

if (Package['lambda']) {
  console.log("we are in debug mode and we have lamda");
  console.log("does this say Christmas? " + Package['lambda']["Lambda"]['func']());

} else {
  console.log("we are in production mode, or we have not installed lambda");
}