karma-runner / karma-runner/karma

Feature request: functions/URLs as file sources

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

Description

I'd like to propose allowing functions or URLs as file sources:
```
files: [
(done) => promisifiedHttpGet( 'http://localhost:8080/bundle.js').then(resp => done(resp))
(done) => done(' console.log('dynamic script content') </script)')
]
```

```
files: [
'http://localhost:8080/vendor.js'
'http://localhost:8080/bundle.js'
]
```

Currently this is not possible. Each entry in the files array must correspond to a real file in the file system.

If such a feature was to be added, a plugin for karma could dynamically fill in the files array upon compilation (when it knows what files were output), and notify Karma that it needs to reload.

This would help with a problem I've been having while trying to improve the performance of https://github.com/webpack-contrib/karma-webpack, in which an entry might originate multiple bundles.

The multiple bundles are important for build performance. Automatically making a bundle for vendor files massively cuts down the recompile time of tests proper.

I did something similar in https://github.com/angular/angular-cli/pull/6160, and observed 15x smaller build times (16.5s to 0.9s) on a medium sized project.

Since there was no way of adding the Webpack URLs directly to the files array, instead I added a [custom karma-context.html](https://github.com/filipesilva/angular-cli/blob/083fc112c8eda1d634cb3d19b4536691184d0ba9/packages/%40angular/cli/plugins/karma-context.html) that had hardcoded script tags. Then I added a [custom handler](https://github.com/filipesilva/angular-cli/blob/083fc112c8eda1d634cb3d19b4536691184d0ba9/packages/%40angular/cli/plugins/karma.ts#L183-L205) to serve those files from the webpack middleware.

The one big problem with this approach is that the hardcoded files are not processed by Karma, which at least means that sourcemaps become problematic (https://github.com/angular/angular-cli/issues/6583).

Another problem is that the possible list of files has to be known at library build time, and that using another custom context file will break functionality.

I feel it would be a worthwhile addition to Karma.

There's a couple of open questions about it though:
- under what name will these files be served? (perhaps a random name)
- can the files array be changed between karma runs? (different compilations might yield different bundles)
- how can these files be watched? (might need custom watch function)

Please let me know what you think of this proposal. If there is a better way of achieving the dynamic 'virtual' files list, I'm all for it.

/cc @d3viant0ne

Contributor guide

Open the contributing guide

Research direction

Start by reviewing how Karma currently requires each files entry to map to a filesystem file, then compare that behavior with the custom karma-context.html and custom handler in the linked Angular CLI examples. Investigate the proposal's open questions around serving names, changing files between runs, and watching generated files. Done would require a concrete design covering function and URL sources without losing processing or sourcemap support.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.