1

我不明白如何测试 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 测试我的包?或者这是一个错误!

4

1 回答 1

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");
}
于 2016-01-31T13:33:45.997 回答