如何使用 peerDependencies 测试 npm 模块?

How to test an npm module with peerDependencies?

我刚开始了解 peerDependencies,并且我阅读了以下参考资料以寻求如何测试 npm 模块在其 package.json 中包含 peerDependencies:

但是,我还没有找到使用 peerDependencies 测试 npm 的明确解决方案。一些人建议将 peerDependencies 添加为全局变量,一些人建议将 peerDependencies 添加到 devDependencies 中,但似乎都不对。

例如,我有一个具有对等依赖项的包,一个自定义记录器,并且此记录器需要由其主机包配置才能使用。

这就是我使用此 Gulp 任务执行大多数脚本化测试的方式:

function testRunner() {
  return (
    gulp
      .src('./tests/**/*.js', { read: false })
      .pipe(
        mocha({
          exit: true,
          timeout: 10000
        })
      )
      .on('error', console.error)
  );
}

我确实收到了使用 npm-install-peers 的有用建议(请参阅下面的评论,@estus),但是,我还不确定它是否可以在使用前配置对等依赖项,因为它将由主机包。

非常感谢反馈和建议。

我联系了 npm-install-peers 的作者,其中一位作者的回复,

When it comes to testing your package/project on CI, I believe the correct way to do things is manually adding your peer dependencies to your dev dependencies. No need for this little tool.

就我而言,我上次开发了一个使用 ioredis 作为对等依赖的库。我的解决方案是将该库也置于开发依赖中。

// package.json
"peerDependencies": {
    "ioredis": "4.x"
},
"devDependencies": {
    "ioredis": "4.x"
}

它运行良好,到目前为止使用这种方法没有问题。