为什么我必须在 Meteor 的 Package.onTest 中复制我在 Package.onUse 中指定的包?

Why do I have to duplicate packages that I specified in Package.onUse in Package.onTest in Meteor?

在以下命令中,出现以下错误

$ meteor test-packages --driver-package practicalmeteor:mocha rocketchat:spotify

控制台输出

=> Errors prevented startup:

   While building package local-test:rocketchat:spotify:
   error: No plugin known to handle file 'spotify.test.coffee'. If you want this file to be a static asset, use
   addAssets instead of addFiles; eg, api.addAssets('spotify.test.coffee', 'client').

我很困惑,因为我在 Package.onUse 下指定了 coffeescript 包。

rocketchat-spotify/package.js

Package.describe({
    name: 'rocketchat:spotify',
    version: '0.0.1',
    summary: 'Message pre-processor that will translate spotify on messages',
    git: ''
});

Package.onUse(function(api) {
    api.versionsFrom('1.0');

    api.use([
        'coffeescript', # Coffeescript is included here?
        'templating',
        'underscore',
        'rocketchat:oembed@0.0.1',
        'rocketchat:lib'
    ]);

    api.addFiles('lib/client/widget.coffee', 'client');
    api.addFiles('lib/client/oembedSpotifyWidget.html', 'client');

    api.addFiles('lib/spotify.coffee', ['server', 'client']);
});

Package.onTest(function (api) {
    api.use([
        'rocketchat:spotify'
    ]);
    api.addFiles('spotify.test.coffee');
});

按如下方式添加 coffeescript 包可解决问题

Package.onTest(function (api) {
    api.use([
        'coffeescript',
        'rocketchat:spotify'
    ]);
    api.addFiles('spotify.test.coffee');
});

=> Modified -- restarting.
=> Meteor server restarted
=> Started your app.

控制台输出

=> App running at: http://localhost:3000/
I20160602-17:55:02.867(9)? Updating process.env.MAIL_URL
I20160602-17:55:04.528(9)? MochaRunner.runServerTests: Starting server side tests with run id aXdi2H3kBS8M8Fuhx
W20160602-17:55:04.577(9)? (STDERR) MochaRunner.runServerTests: failures: 10

版本信息

$ meteor --version
Meteor 1.2.1

根据Package.onTest documentation,您需要单独指定测试的依赖项。因此,如果您的单元测试使用 CoffeeScript,那么您需要在 onTest 回调中明确指定它。

您可以将包的单元测试视为构建在您正在测试的包之上的单独包。

为什么?

MDG 做到了,因为有时您的测试与您正在测试的包有不同的依赖关系。例如:你在 CoffeeScript 上编写了包,但你的测试是在普通 JavaScript 上编写的。然后 CoffeeScript 依赖项对于测试来说是多余的。 API 的当前版本允许您单独指定这些依赖项。