How to test an npm module with peerDependencies?
Asked Answered
N

2

35

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:

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.

Naturalism answered 18/1, 2019 at 0:52 Comment(7)
What exactly do you mean by 'testing'? Are you looking to write automated tests? Are you talking about manually testing something? Are you talking about something else?Polyhymnia
@pedro-a Thank you for your response, I appreciate that I should add clarity as to what I mean by "testing". I have just edited my question with Gulp task that performs a mocha test. So, what I mean by testing is first localized followed by CI automated tests.Naturalism
Possibly github.com/spatie/npm-install-peersAlleris
@estus Thank you for your suggestion, I am checking it out now and connecting with its author. I forgot one detail, the peer dependency also needs to be configured by the peer's parent. Adding that detail to my initial question.Naturalism
I am currently checking out another npm package: npm install-peerdepsNaturalism
One of the authors of npm-install-peers is provided an answer below.Naturalism
I case you are asking yourself how to test multiple version of a peer dependency, it's explained in this article, TLDR: You can explicitly install different versions of a package by using "dep-v1": "npm:dep@1", "dep": "2" in your devDependenciesAdahadaha
P
42

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.

Petta answered 22/1, 2019 at 10:5 Comment(1)
Also, to test your package on different peer versions, you can temporarily change that dev dependency to specific versions, re-install dependencies (I use npm ci), and run your tests.Nahum
N
8

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.

Naturalism answered 21/1, 2019 at 19:17 Comment(1)
I added the same devDependency as is in my peerDependencies so that my unit tests run. These seems like the most appropriate way to do this.Frigidaire

© 2022 - 2024 — McMap. All rights reserved.