Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare test dependency using Npm.depends

Tags:

node.js

meteor

I would like to know how to declare a dependency on an Npm module in Meteor only in test.

While testing a package, I can easily declare a Npm dependency in package.js like this:

Npm.depends({
  ...
  'sinon': '1.15.3'
  ...
});

But I am only using sinon in test, and I want to make it explicit.

I tried the following with no success.

Package.onTest(function(api) {
  // # 1
  // can't do this because it is not a meteor module
  api.use('sinon');

  // # 2
  // can't because I have other production Npm dependencies
  // and Meteor only allows one `Npm.depends` call per `package.js`.
  // Also, not sure if nesting Npm.depends() is allowed at all.
  Npm.depends({
    'sinon': '1.15.3'
  });


});

Any suggestions?

like image 718
Sung Cho Avatar asked Nov 16 '25 23:11

Sung Cho


1 Answers

The only way to do this is to wrap sinon into a package and api.use it. You can do the following:

$ meteor create --package sinon

Replace the contents of packages/sinon with the following:

package.js

Package.describe({
  summary: 'Standalone test spies, stubs and mocks for JavaScript.'
});

Package.onUse(function(api) {
  api.versionsFrom('1.0.4');
  api.export('sinon', 'server');
  api.addFiles('sinon.js');
  api.addFiles('post.js');
});

post.js

sinon = this.sinon;

sinon.js

Download the latest version from here.

Finally in the package you are testing, you can add api.use('sinon'); in your Package.onTest.


As an alternative to making your own package, you can just use one of the community versions available here.

like image 53
David Weldon Avatar answered Nov 19 '25 12:11

David Weldon