Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test an npm module with 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:

  • Peer Dependencies
  • Understanding the npm dependency model
  • Common npm mistakes
  • StackOverflow: npm peerDependencies during development

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.

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

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.

Feedback and Suggestions are most appreciated.

like image 367
Jeff Tanner Avatar asked Jan 18 '19 00:01

Jeff Tanner


People also ask

What is peerDependencies npm?

Peer Dependencies are used to specify that our package is compatible with a specific version of an npm package. Good examples are Angular and React. To add a Peer Dependency you actually need to manually modify your package.json file.

Does npm install peerDependencies?

UPDATE: npm versions 1, 2, and 7 will automatically install peerDependencies if they are not explicitly depended upon higher in the dependency tree.

How do I test npm before publishing?

With your NPM package local to your machine, you'll need a way to reference/install it in the test application. Inside of the original NPM package directory, run npm link from the command line. This command will allow us to simulate installing this NPM package without it actually being published.


2 Answers

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.

like image 102
deerawan Avatar answered Oct 13 '22 06:10

deerawan


I had connected with the authors of npm-install-peers, and the response from one of the authors,

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.

like image 43
Jeff Tanner Avatar answered Oct 13 '22 06:10

Jeff Tanner