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.

7
  • 1
    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? Commented Jan 18, 2019 at 1:14
  • @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. Commented Jan 18, 2019 at 1:34
  • 1
    Possibly github.com/spatie/npm-install-peers Commented Jan 18, 2019 at 8:12
  • @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. Commented Jan 18, 2019 at 16:15
  • I am currently checking out another npm package: npm install-peerdeps Commented Jan 18, 2019 at 17:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.