karma-runner / karma-runner/karma

Overriding vs Appending to config.plugins

Open
#2,772 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

* By default the plugins are defined ( in [config.js:332](https://github.com/karma-runner/karma/blob/483beddf6c7a6fd3866e6796890e8d80d5c6492a/lib/config.js#L332)) as:
```javascript
this.plugins = ['karma-*']
```
* The [config.set](https://github.com/karma-runner/karma/blob/483beddf6c7a6fd3866e6796890e8d80d5c6492a/lib/config.js#L288) method will **override** array properties.
```javascript
// karma.conf.js
module.exports = function(config) {
config.set({
basePath: '../..',
frameworks: ['jasmine'],
//...
});
};
```
* The example code snippets in the [middleware](https://github.com/karma-runner/karma/blob/master/docs/config/01-configuration-file.md#middleware) & [plugins](https://github.com/karma-runner/karma/blob/master/docs/config/05-plugins.md#loading-plugins) docs do not specify 'karma-*' in the plugins array.

What this means is that users implementing a custom inline middleware or plugins are likely
to encounter errors such as:
> No provider for "framework:mocha"!

As anytime a plugins array is explicitly specified, the **karma's dependency injection system is effectively disabled**.

This can of course be easily fixed by manually adding "karma-*" to the plugins array.
But this requires knowledge of karma's internals...

This behavior does not make much sense, as a user who wants to add a custom inlined plugin
does not automatically want to disable the dependency injection...
If such a capability is even relevant it should be handled explicitly via it's own configuration
property.

```javascript
config.set({
disableDI : true
})
```

### Solutions
* Improve docs to specify "karma-*" in all examples.
- This is just a mitigation and does not fix the root cause.
* Always **append** "karma-*" to the plugins array.
* Separate plugins property and default DI behavior.

Which option is preferable to the project maintainers?
I can try to implement it...

Contributor guide

Open the contributing guide

Research direction

Start with lib/config.js at config.set and the default plugins definition around lines 288 and 332. Compare those behaviors with the middleware and plugins examples in docs/config/01-configuration-file.md and docs/config/05-plugins.md. Done means the maintainers' selected plugin-loading behavior is implemented and the affected documentation examples are consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.