codymikol / karma-webpack

Karma webpack Middleware
MIT License
830 stars 222 forks source link

Add webpack_done event. #430

Closed Backfighter closed 2 years ago

Backfighter commented 4 years ago

This PR contains a:

Motivation / Use-Case

This allows to perform certain actions once a compilation is done, when using the karma api. Example use case: Run tests every time a compilation has finished.

const { Server, runner } = require('karma');
const karma = new Server(config);
karma.start();
karma.on(
  'webpack_done', 
  () => runner.run(config, () => {})
);

Breaking Changes

None.

Additional Info

There is currently a workaround to still allow running test on every webpack compile, with webpack-karma:

const { Server, runner } = require('karma');

const karma = new Server(config);
karma.start();

let ready = false;
karma.once('browsers_ready', () => (ready = true));
executeWhenReady = func =>
  ready ? func() : emitter.once('browsers_ready', func);

executeWhenReady(startKarmaAutoRun.bind(null, config, karma));

function startKarmaAutoRun(config, emitter) {
  // We need to use once and reregister the event,
  // because the run itself triggers the file_list_modified
  // event.
  // Note that the file_list_modified event is not part of karmas public api, 
  // but it's the only way to detect the webpack rebuild until
  // https://github.com/webpack-contrib/karma-webpack/pull/430 is merged.
  KarmaRunner.run(config, () =>
    emitter.once(
      'file_list_modified',
      startKarmaAutoRun.bind(null, config, emitter)
    )
  );
}
jsf-clabot commented 4 years ago

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.

codecov[bot] commented 4 years ago

Codecov Report

Merging #430 into master will decrease coverage by 0.19%. The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #430      +/-   ##
==========================================
- Coverage   42.46%   42.27%   -0.20%     
==========================================
  Files           3        3              
  Lines         219      220       +1     
  Branches       48       48              
==========================================
  Hits           93       93              
- Misses        103      104       +1     
  Partials       23       23              
Impacted Files Coverage Δ
src/karma-webpack.js 46.73% <0.00%> (-0.24%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 3cc35b3...b2e4de2. Read the comment docs.

codymikol commented 3 years ago

Are you still interested in this?

Backfighter commented 3 years ago

Personally no. I have moved on from the project this would have been beneficial for. Nonetheless I still consider it to potentially provide value to others.