如何使用 peerDependencies 测试 npm 模块? [英] How to test an npm module with peerDependencies?
问题描述
我刚开始了解 peerDependencies,我已经阅读了以下参考资料,寻求如何测试 npm 模块在其 package.json 中包含 peerDependencies:
I am new to understand peerDependencies, and I have read the following references seeking as to how to test an npm module contains peerDependencies within its package.json:
但是,我还没有找到用 peerDependencies 测试 npm 的明确解决方案.有些人建议将 peerDependencies 添加为全局变量,有些人建议将 peerDependencies 包含在 devDependencies 中,但这似乎都不对.
However, I have not found a clear solution for testing npm with peerDependencies. Some recommend adding the peerDependencies as globals, and some reference to include peerDependencies within devDependencies, and neither seems right.
例如,我有一个具有对等依赖项的包,一个自定义记录器,并且该记录器需要由其主机包配置才能使用.
For example, I have a package that has a peer dependency, a custom logger, and this logger needs to be configured by its host package before it can be used.
这是我使用此 Gulp 任务执行大多数脚本测试的方式:
This is how I perform most tests scripted in using this Gulp task:
function testRunner() {
return (
gulp
.src('./tests/**/*.js', { read: false })
.pipe(
mocha({
exit: true,
timeout: 10000
})
)
.on('error', console.error)
);
}
我确实收到了一个有用的建议(见下面的评论,@estus)使用 npm-install-peers,但是,我还不确定它是否可以在使用前配置对等依赖项,因为它将由主机包执行.
I did receive a helpful suggestion (see comments below, @estus) to use npm-install-peers, however, I am not yet certain if it can configure a peer dependency before usage, as it would be performed by the host package.
非常感谢反馈和建议.
推荐答案
就我而言,我上次开发了一个使用 ioredis
作为对等依赖项的库.我的解决方案是将该库也置于开发依赖项中.
In my case, I developed a library last time that use ioredis
as peer dependency. My solution was to put that library as well in dev dependency.
// package.json
"peerDependencies": {
"ioredis": "4.x"
},
"devDependencies": {
"ioredis": "4.x"
}
它运行良好,到目前为止使用这种方法没有问题.
it worked well and no issue so far using this approach.
这篇关于如何使用 peerDependencies 测试 npm 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!