codymikol / karma-webpack

Karma webpack Middleware
MIT License
830 stars 221 forks source link

Support for custom entry points #510

Closed sechel closed 10 months ago

sechel commented 3 years ago

Feature Proposal

Let webpack bundle custom entries along with the karma-webpack default bundles.

Feature Use Case

I tried to upgrade from version 4 to version 5 and noticed that it's no more possible to have custom webpack entries with karma-webpack. My test setup relies on these entries in addition to the main test bundle as I test different parts of the application loaded into iframes that in turn should load their respective bundle.

lk77 commented 1 year ago

It's possible to actually override webpack config :

let DefaultWebpackOptionsFactory = require('karma-webpack/lib/webpack/defaults');

DefaultWebpackOptionsFactory.create = () => {
    return webpackConfig;
};

require.cache[require.resolve('karma-webpack/lib/webpack/defaults')].exports = DefaultWebpackOptionsFactory;

it's a hack, but it allow the setting of entries and outputs, by overriding the default config factory of karma-webpack

sechel commented 1 year ago

Thanks @lk77 for the input. @rorymichele could you try and see if that is an option for us?

rorymichele commented 1 year ago

Sorry for the late reply! I had tried the hack from @lk77 but didn't suceed. I used a different workaround instead, and as the problem seems to persist, I would like to share it here:

for the tests affected I first run webpack with a specific output directory

webpack.config.js

...
if (process.env.npm_lifecycle_event.startsWith('test:sdk')) {
    PATH = 'dist-test';
}
...

with test:sdk being the name of my npm script, and tell karma to serve this directory

karma.conf.js

...
if (process.env.npm_lifecycle_event.startsWith('test:sdk')) {
  webpackConfig.output.publicPath = 'base/dist-test/';
}
...
module.exports = function(config) {
  ...,
  config.set({
    ...
    files: [
      'src-test/index.ts',
      {
        pattern: 'dist-test/**/*',
        included: false
      }
    ],
    ...

. In order to achieve hot reloading for test driven development, I use https://github.com/open-cli-tools/concurrently in a js starter script run-sdk-test.js

const concurrently = require('concurrently');
const { result, commands } = concurrently(
  [
    { command: 'rm -rf dist-test; NODE_OPTIONS=--no-deprecation webpack --watch;', name: 'webpack' },
    { command: 'karma start  --browsers ChromeCI --singleRun false', name: 'karma'},
  ],
  {
    prefix: 'name',
    killOthers: ['failure', 'success'],
    successCondition: 'first',
    restartTries: 3
  }
);

commands[1].kill('SIGTSTP');
console.log(`paused ${commands[1].name} process until ${commands[0].name} compile successfully`);
commands[0].stdout.subscribe(o => {
  if (o.toString().match(/compiled .{0,10}successfully/)) {
    console.log(`resuming ${commands[1].name} process`)
    commands[1].kill('SIGCONT');
  }
  if (o.toString().match(/compiled with .{0,10} error/)) {
    console.log(`kill ${commands[1].name} process`)
    commands[1].kill('SIGKILL');
  }
});

result.then(res => console.log(`exited with ${JSON.stringify(res.map(p => p.exitCode))}`));

For completeness my package.json scripts section:

  "scripts": {
    ...
    "test:sdk": "rm -rf dist-test; NODE_OPTIONS=--no-deprecation webpack; karma start",
    "test:sdk-tdd": "node run-sdk-test.js",
    ...
  },

I hope this might help someone and that I haven't forgotten anything

codymikol commented 10 months ago

As karma is now deprecated and coming up on EOL, we are no longer planning on any significant enhancements to this project and are instead going to focus on security updates, stability, and a migration path forward as karma's lifecycle comes to an end.

Thank you for supporting and using this project!

sirSayed98 commented 1 month ago

I have solved this issue by upgrading karma dependencies for example:

"karma": "^2.0.0",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^1.1.1",
"karma-coverage-istanbul-reporter": "^2.1.1",
"karma-firefox-launcher": "^0.1.6",
"karma-jasmine": "^1.1.0",
"karma-ng-html2js-preprocessor": "^0.1.2",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "^4.0.2",
to 

"karma": "^6.3.4",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.3",
"karma-coverage-istanbul-reporter": "^3.0.3",
"karma-firefox-launcher": "^2.1.2",
"karma-jasmine": "^4.0.2",
"karma-ng-html2js-preprocessor": "^1.0.0",
"karma-sourcemap-loader": "^0.3.8",
"karma-spec-reporter": "^0.0.34",
"karma-webpack": "5.0.1",

in addition to update frameworks in karma.config.js to be

frameworks: ['jasmine', 'webpack'],