Brian M. Hunt Brian M. Hunt - 4 months ago 28
Javascript Question

Add files to watch-list in karma preprocessor


Here is a repository created to demonstrate the issue:



brianmhunt/karma-rollup-preprocessor-issue-3




I'm trying to get karma-rollup-preprocessor working with Karma's builtin watch i.e. solve showpad/karma-rollup-preprocessor#3

In other words, in a preprocessor, I want to add files to Karma's watch list.

It's easy to get the list of files Rollup uses to compile. Rollup returns a list of files that it reads (ones one wants to watch), so in the preprocessor I am trying to add files to the list karma watches.

Basically I want to add this (or the working equivalent) to the preprocessor:

bundle.modules.forEach((module) => {
files.unshift({
pattern: module.id, /* The full file path, from Rollup */
watched: true,
included: false,
nocache: false,
served: false,
})
})


Where
files
is Karma's
config.files
or
fileList
or whatever place one needs to put the files being watched.

Doing the above with
config.files
, the files are indeed being added to the watcher, but
.on(fileList.changeFile)
fails the
_isIncluded
.

So it looks like the files also (or alternatively) must be added to the
fileList
.

Unfortunately when I try to add the
fileList
to the
$inject
, I get the error:


Error: Can not resolve circular dependency! (Resolving: preprocess -> preprocessor:rollup -> fileList -> preprocess).


I've looked at basically all the other preprocessors that look like they could also add includes, but I have found no indication of how to do it.

Is there a canonical way to add files Karma should watch from a preprocessor? Or otherwise how might one do this? This seems pretty clutch for a preprocessor in Karma, so it's surprising that it's not documented, apparent, or problematic in the other preprocessors.

EDIT Here's some more attempts:

I tried to add the watched patterns to the
config.files
in
karma.conf
i.e.

files: [
"spec/**/*.js",
{pattern:"src/**/*.js", included: false, watched: true}
]


But the
src/*
doesn't recompile when changed. The tests just re-run.

So I tried chokidar like this:

var server = new karma.Server(options)...

chokidar.watch("src/**/*.js")
.on('add', server.refreshFiles.bind(server))
.on('change', server.refreshFiles.bind(server))


I also tried it with a debounce, in case Karma was slower on the refresh, but it seems the tests won't re-run.

I rooted around karma-browserify for inspiration but it was a bit too convoluted to pick up without delving in.

Answer

I've issued a pull request to resolve this.

Until it is merged one can use my repo i.e. put "karma-rollup-preprocessor": "brianmhunt/karma-rollup-preprocessor" in package.json dependencies or devDependencies.

EDIT: Superceded by https://github.com/Kflash/karma-rollup-plugin

Comments