34

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.

Jeff
  • 1,917
  • 1
  • 25
  • 43
  • 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? – Pedro A Jan 18 '19 at 01: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. – Jeff Jan 18 '19 at 01:34
  • 1
    Possibly https://github.com/spatie/npm-install-peers – Estus Flask Jan 18 '19 at 08: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. – Jeff Jan 18 '19 at 16:15
  • I am currently checking out another npm package: [npm install-peerdeps](https://www.npmjs.com/package/install-peerdeps) – Jeff Jan 18 '19 at 17:27
  • One of the authors of npm-install-peers is provided an answer below. – Jeff Jan 21 '19 at 19:16
  • I case you are asking yourself how to test multiple version of a peer dependency, it's explained [in this article](https://dev.to/joshx/test-your-npm-package-against-multiple-versions-of-its-peer-dependency-34j4), TLDR: You can explicitly install different versions of a package by using `"dep-v1": "npm:dep@1", "dep": "2"` in your `devDependencies` – karfau Jun 20 '20 at 08:45

2 Answers2

41

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.

deerawan
  • 8,002
  • 5
  • 42
  • 51
  • 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. – Robert Sep 09 '21 at 14:25
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.

Jeff
  • 1,917
  • 1
  • 25
  • 43
  • 2
    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. – Catfish May 15 '19 at 21:50