karma-runner / karma

Spectacular Test Runner for JavaScript
http://karma-runner.github.io
MIT License
11.96k stars 1.71k forks source link

Allow serve multiple directories as baseDir #2259

Open luisvt opened 8 years ago

luisvt commented 8 years ago

Expected behavior

Allow specify a configuration option to be able to serve some directories like node_modules and bower_components as base directories.

This should be similar to what (browserSync baseDir)[https://www.browsersync.io/docs/options#option-server] option does. This will allow developers to use npm packages for browser unit-testing.

Actual behavior

Right now we should specify all the files coming from node_modules one by one. This is cumbersome. For example if we want to use requirejs, angular and lodash we should do:

files: [
  {pattern: 'node_modules/angular/angular.js', included: false},
  {pattern: 'node_modules/requirejs/require.js', included: false},
  {pattern: 'node_modules/lodash/lodash.js', included: false},
  'test-main.js'
}

since trying to use a glob value for node modules doesn't work, for example doing this:

files: [
  {pattern: 'node_modules/**/*.js', included: false},
  'test-main.js'
}

throws next error:

You need to include some adapter that implements __karma__.start method!

Enviroment Details

    files: [
      {pattern: 'node_modules/angular/**/*.js', included: false},
      {pattern: 'node_modules/angular-mocks/**/*.js', included: false},
      {pattern: 'node_modules/angular-resource/**/*.js', included: false},
      {pattern: 'node_modules/ts-helpers/index.js', included: false},
      {pattern: 'src/**/*', included: false},
      {pattern: 'test/**/*', included: false},
      'test/test-main.js'
    ],
    // list of files to exclude
    exclude: [
      'node_modules/**/test/**/*.js',
      'node_modules/**/*.spec.js',
      'node_modules/**/*.test.js'
    ],
dignifiedquire commented 8 years ago

The error about the __karma__.start method is tracked in #2194 and has nothing to do with the behaviour you are looking for. Your second configuration should work fine. Also in general you can easily write a method for the behaviour that you want

function toModule (name) {
  return {
    pattern: 'node_modules/' + name,
    included: false
  }
}
// later
files: [
  toModule('angular/angular.js'),
  toModule('requirejs/require.js'),
  toModule('lodash/lodash.js'),
  'test-main.js'
}
luisvt commented 8 years ago

with karma 1.2.0 having next code in karma.config.js:

files: [
  {pattern: 'node_modules/**/*.js', included: false},
  'test-main.js'
}

it produces next error message:

25 08 2016 18:41:32.578:ERROR [karma]: { Error: listen ENFILE 0.0.0.0:9876
    at Object.exports._errnoException (util.js:1008:11)
    at exports._exceptionWithHostPort (util.js:1031:20)
    at Server._listen2 (net.js:1240:19)
    at listen (net.js:1289:10)
    at Server.listen (net.js:1385:5)
    at afterPreprocess (/Users/lvargas/projects/js/angular-typescript/node_modules/karma/lib/server.js:174:15)
    at tryCatcher (/Users/lvargas/projects/js/angular-typescript/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/lvargas/projects/js/angular-typescript/node_modules/bluebird/js/release/promise.js:509:31)
    at Promise._settlePromise (/Users/lvargas/projects/js/angular-typescript/node_modules/bluebird/js/release/promise.js:566:18)
    at Promise._settlePromise0 (/Users/lvargas/projects/js/angular-typescript/node_modules/bluebird/js/release/promise.js:611:10)
    at Promise._settlePromises (/Users/lvargas/projects/js/angular-typescript/node_modules/bluebird/js/release/promise.js:686:18)
    at Async._drainQueue (/Users/lvargas/projects/js/angular-typescript/node_modules/bluebird/js/release/async.js:138:16)
    at Async._drainQueues (/Users/lvargas/projects/js/angular-typescript/node_modules/bluebird/js/release/async.js:148:10)
    at Immediate.Async.drainQueues (/Users/lvargas/projects/js/angular-typescript/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:566:20)
    at tryOnImmediate (timers.js:546:5)
  code: 'ENFILE',
  errno: 'ENFILE',
  syscall: 'listen',
  address: '0.0.0.0',
  port: 9876 }

From what I understand it means that Karma Server is trying to open and listen into too many files.

wesleycho commented 7 years ago

This is because that glob pattern is overly broad.

IMO, that is fine, because a broad glob pattern has huge negative performance implications as well. This is one area where I think supporting has large negative ramifications for the user on the performance side, and that nothing more should be done in karma itself in regards to this area.