karma icon indicating copy to clipboard operation
karma copied to clipboard

Compatibility with pnpm

Open mjeanroy opened this issue 1 year ago • 0 comments

Hi everyone,

I'm a big fan of pnpm, and I wanted to use it with a project using karma as test runner and I noticed that karma was not compatible with pnpm.

I debugged my config and I found that:

  • When karma is loading plugins:
    • It try to guess the plugin directory. It works by using the lib/plugin.js directory and go two directories up to retrieve the node_modules directory (here).
    • It works with npm (and probably with yarn too) because karma is usually installed inside the project node_modules directory.
  • Unfortunately, with pnpm, it does not work like this:
    • Karma is not directly installed in the project node_modules, it is installed in a .pnpm directory inside the project node_modules directory. The directory tree will look like this:
[project]
  node_modules
    .pnpm
      [email protected]
        node_modules
          karma
            lib
              plugin.js
      [email protected]
        node_modules
          karma-chrome-launcher
    karma -> .pnpm/[email protected]/node_modules/karma
    karma-chrome-launcher -> .pnpm/[email protected]/node_modules/karma-chrome-launcher

➡️ By going two directories up, the plugin directory is then [project]/.pnpm/[email protected]/node_modules, so obviously no plugins will be detected.

A (probably very naive) fix would to use process.cwd() instead of __dirname of karma/lib/plugin.js, this way you would get the project root directory. But, as said, it's probably very naive, and I'm sure I probably miss a lot of use cases.

In any case, I'd be happy to submit PR to fix this issue if you think it's worth it.

mjeanroy avatar Mar 11 '23 14:03 mjeanroy