testingmeteortinytest

How to test a server side debugOnly package


I don't understand how it is possible to test a package that is debugOnly. My package.js is quite simple :

 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');
});

My lambda-test.js :

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

My lambda.js :

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

When I run meteor test-packages, it just fails : Lambda is not defined. If I remove the debugOnly: true the test pass. So how can I test my package using tinytest ? Or this is a bug !


Solution

  • I had the same issue! It turns out the tests are working fine. The Lambda is not getting exported in the project either.

    from 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`.
    

    Try:

    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
    });
    

    Now you can do something like this:

    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");
    }