nikku / karma-browserify

A fast Browserify integration for Karma that handles large projects with ease
MIT License
321 stars 50 forks source link

intellij karma runner doesn't use watchify for bundle rebuild #205

Closed pchudzik closed 7 years ago

pchudzik commented 7 years ago

When running karma tests from intellij autoWatch flag is always overwritten to false - https://github.com/JetBrains/intellij-plugins/blob/master/js-karma/src/js_reporter/karma-intellij/lib/intellij.conf.js#L56 (https://youtrack.jetbrains.com/issue/WEB-14437)

For small projects it doesn't really matter but in case project is big then bundling all js files can tak up to 20-30 seconds (with all the sources, and external dependencies).

karma-browserify depends on autoWatch = true which will trigger watchify. Now since idea always overwrite autoWatch to false then it's impossible to use watchify when running tests from idea. It means that every time I want to run tests I need to wait 20+ seconds for browserify to bundle from scratch :(

I've created ugly workaround for this - https://github.com/pchudzik/karma-browserify/commit/10ee655d8bf489cc2ff315815847e67410f0af3b With this bundle is built with watchiy in less than 1s. I can create proper PR with test and documentation but the question is will you accept this ugly workaround dedicated for intellij? It's basically the same as in https://github.com/nikku/karma-browserify/pull/24 but It wasn't merge because of https://youtrack.jetbrains.com/issue/WEB-12496 for which there is a workaround: In file ~/.IntelliJIdea2016.3/config/plugins/js-karma/js_reporter/karma-intellij/lib/intellijRunner.js find function runTests and change runWithConfig invocation param refresh to true. It should look like this:

function runTests() {
  var serverPort = cli.getServerPort();
  var urlRoot = cli.getUrlRoot() || '/';
  if (urlRoot.charAt(urlRoot.length - 1) !== '/') {
    urlRoot = urlRoot + '/';
  }
  runWithConfig({
    port: serverPort,
    refresh: true,
    urlRoot: urlRoot
  });
}

Configuration object applied on bro.js when using intellij's karma runner

{ LOG_DISABLE: 'OFF',
  LOG_ERROR: 'ERROR',
  LOG_WARN: 'WARN',
  LOG_INFO: 'INFO',
  LOG_DEBUG: 'DEBUG',
  set: [Function],
  frameworks: [ 'browserify', 'jasmine' ],
  protocol: 'http:',
  port: 9876,
  hostname: 'localhost',
  httpsServerConfig: {},
  basePath: '/home/pawel/workspace/toggl-reporter',
  files: 
   [ { pattern: '/home/pawel/workspace/toggl-reporter/node_modules/angular/angular.js',
       served: true,
       included: true,
       watched: true,
       nocache: false,
       weight: [Object] },
     { pattern: '/home/pawel/workspace/toggl-reporter/node_modules/angular-mocks/angular-mocks.js',
       served: true,
       included: true,
       watched: true,
       nocache: false,
       weight: [Object] },
     { pattern: '/home/pawel/workspace/toggl-reporter/src/index.js',
       served: true,
       included: true,
       watched: true,
       nocache: false,
       weight: [Object] },
     { pattern: '/home/pawel/workspace/toggl-reporter/src/**/*.spec.js',
       served: true,
       included: true,
       watched: true,
       nocache: false,
       weight: [Object] } ],
  browserConsoleLogOptions: { level: 'debug', format: '%b %T: %m', terminal: true },
  customContextFile: null,
  customDebugFile: null,
  exclude: 
   [ '/home/pawel/workspace/toggl-reporter/scripts/karma.conf.js',
     '/home/pawel/.IntelliJIdea2016.3/config/plugins/js-karma/js_reporter/karma-intellij/lib/intellij.conf.js' ],
  logLevel: 'DEBUG',
  colors: true,
  autoWatch: false,
  autoWatchBatchDelay: 0,
  restartOnFileChange: false,
  usePolling: true,
  reporters: [ 'intellij_c831a91b03572bad3b3db88354641e3b' ],
  singleRun: false,
  browsers: [ 'PhantomJS' ],
  captureTimeout: 60000,
  proxies: {},
  proxyValidateSSL: true,
  preprocessors: 
   { '/home/pawel/workspace/toggl-reporter/src/**/*.js': [ 'browserify' ],
     '/tmp/*.browserify': [ 'browserify-bundle' ] },
  urlRoot: '/',
  reportSlowerThan: 0,
  loggers: [ { type: 'console', layout: [Object], makers: [Object] } ],
  transports: [ 'polling', 'websocket' ],
  forceJSONP: false,
  plugins: 
   [ 'karma-*',
     '/home/pawel/.IntelliJIdea2016.3/config/plugins/js-karma/js_reporter/karma-intellij/lib/intellijPlugin.js' ],
  client: 
   { args: [],
     useIframe: true,
     captureConsole: true,
     clearContext: true },
  defaultClient: 
   { args: [],
     useIframe: true,
     captureConsole: true,
     clearContext: true },
  browserDisconnectTimeout: 2000,
  browserDisconnectTolerance: 0,
  browserNoActivityTimeout: 10000,
  concurrency: Infinity,
  failOnEmptyTestSuite: true,
  retryLimit: 2,
  detached: false,
  configFile: '/home/pawel/.IntelliJIdea2016.3/config/plugins/js-karma/js_reporter/karma-intellij/lib/intellij.conf.js',
  browserify: { debug: true, transform: [ [Object] ] } }
bendrucker commented 7 years ago

I don't think it's appropriate for this project to add and document hacks for a specific IDE

nikku commented 7 years ago

Glad you created that issue and good to know there exists an actual workaround.

We won't include that hack into our project, for the reasons @bendrucker outlined.

Others interested in a workaround may resort to your hack.

pchudzik commented 7 years ago

It's hack for one particular IDE (maybe more than one because there is intellij, webstorm, phpstorm, etc) and I'm not surprised that you decided to decline it :)

In case anybody stumbles on this I've decided to fix it and publish to npm (it's more convenient for me to use package from npm instead of github link): http://npmjs.com/package/karma-browserify-intellij https://github.com/pchudzik/karma-browserify

I will try to keep my fork up to date, and maybe one day jet brains will fix it...