Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

No way to use custom mocha ui #84

Closed
stalniy opened this issue Nov 25, 2015 · 6 comments · Fixed by #100
Closed

No way to use custom mocha ui #84

stalniy opened this issue Nov 25, 2015 · 6 comments · Fixed by #100

Comments

@stalniy
Copy link

stalniy commented Nov 25, 2015

Currently it's impossible to use custom mocha ui.
What I want is extra config option which allow to specify which files I want to load before bootstraping mocha (before adapter.js)

var initMocha = function (files, mochaConfig) {
  var mochaPath = path.dirname(require.resolve('mocha'))
  files.unshift(createPattern(__dirname + '/adapter.js'))
  // HERE: I want to iterate over config.mochaFiles and add them right after mocha.js
  files.unshift(createPattern(mochaPath + '/mocha.js'))

  if (mochaConfig && mochaConfig.reporter) {
    files.unshift(createPattern(mochaPath + '/mocha.css'))
  }
}
@claydiffrient
Copy link

It seems like in my testing the mocha.opts file isn't respected by karma-mocha.

Putting --require ./test/spec_helper.js inside mocha.opts does nothing.

@stalniy
Copy link
Author

stalniy commented Dec 27, 2015

Actually the main cause of this exception is that adapter.js is included before custom ui. So, the simplest solution is to check if adapter is already inside files then don't add it otherwise - default logic. Then devs can do this

files: [
  'path/to/my-ui.js',
  require.resolve('karma-mocha/lib/adapter'),
  'path/to/specs/*'
],
client: {
  mocha: {
    ui: 'my-ui'
  }
}

cc: @vojtajina @maksimr @zzo @dignifiedquire

@maksimr
Copy link
Contributor

maksimr commented Feb 3, 2016

@stalniy you can create inline adapter for custom mocha reporter and load file before karma-mocha files

@stalniy
Copy link
Author

stalniy commented Feb 3, 2016

@maksimr this is browser's environment. I need to load files not before mocha.js but right after it (i.e. before adapter.js but not before mocha.js as first one initialize mocha instance). You can try to do this. I wanted to load https://github.com/stalniy/bdd-lazy-var right after mocha. If I do it before I will receive error (Mocha is undefined) as it adds new interface to Mocha.interfaces.

@dtothefp
Copy link

Think this is related but it would be nice to have a require option similar to gulp-mocha where we could hook into the --require functionality.

ex.

client: {
  mocha: {
    ui: 'my-ui',
    require: [ path.join(__dirname, 'some-script.js') ]
  }
}

Specifically this would be useful for me to do something like https://labnotes.org/yield-to-the-test-using-mocha-with-es6-generators/ where you could hook into something like mocha.Runnable.prototype.run before instantiation

@christian-schulze
Copy link
Contributor

christian-schulze commented Jun 25, 2016

@stalniy I've had some success getting your https://github.com/stalniy/bdd-lazy-var mocha ui to work by making the following changes to lib/index.js#initMocha:

var initMocha = function (files, mochaConfig) {
  var mochaPath = path.dirname(require.resolve('mocha'))
  var bddLazyVarPath = path.dirname(require.resolve('bdd-lazy-var'))
  files.unshift(createPattern(path.join(__dirname, 'adapter.js')))
  files.unshift(createPattern(path.join(bddLazyVarPath, 'bdd_lazy_var_global.js')))
  files.unshift(createPattern(path.join(mochaPath, 'mocha.js')))

  if (mochaConfig && mochaConfig.reporter) {
    files.unshift(createPattern(path.join(mochaPath, 'mocha.css')))
  }
}

I've generalised this solution, by using @dtothefp idea of a require array:

client: {
  mocha: {
    ui: 'my-ui',
    require: [ require.resolve('bdd-lazy-var/bdd_lazy_var_global') ]
  }
}

lib/index.js#initMocha

var initMocha = function (files, mochaConfig) {
  files.unshift(createPattern(path.join(__dirname, 'adapter.js')))

  if (mochaConfig.require) {
    for (var requirePath of mochaConfig.require) {
      files.unshift(createPattern(requirePath));
    }
  }

  var mochaPath = path.dirname(require.resolve('mocha'))
  files.unshift(createPattern(path.join(mochaPath, 'mocha.js')))

  if (mochaConfig && mochaConfig.reporter) {
    files.unshift(createPattern(path.join(mochaPath, 'mocha.css')))
  }
}

Also need to exclude the require array from being sent to mocha in the adapter. Will throw a PR together asap.

christian-schulze pushed a commit to christian-schulze/karma-mocha that referenced this issue Jun 26, 2016
Allow requiring files after mocha is initialised, via karma.conf.js

Closes karma-runner#84
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants