jejacks0n / teaspoon

Teaspoon: Javascript test runner for Rails. Use Selenium, BrowserStack, or PhantomJS.
1.43k stars 243 forks source link

How to override Mocha's interface (UI setting)? #438

Closed christian-schulze closed 8 years ago

christian-schulze commented 8 years ago

Is it possible to override Mocha's interface? See https://mochajs.org/#interfaces, and https://mochajs.org/#browser-configuration.

Currently it looks like the interface is hard coded to bdd here: https://github.com/modeset/teaspoon/blob/cdf46b24676c093f7a2b0ed8e8e473dd89a47564/teaspoon-mocha/lib/teaspoon/mocha/assets/teaspoon/mocha/intiialize.coffee.

I would like to, for example, use the bdd-lazy-var UI defined here: https://github.com/stalniy/bdd-lazy-var.

christian-schulze commented 8 years ago

Have found a solution. With some poking around window.env by adding (console.log prop + ': ' + value) for prop, value of window.env to spec_helper.coffee, I found:

ui: function (ui) {
  Mocha.prototype.ui.call(this, ui);
  this.suite.emit('pre-require', global, null, this);
  return this;
}

Calling window.env.ui('bdd-lazy-var') in spec_helper.coffee works perfectly.

Perhaps this could be added to the Mocha wiki page to help future developers?

jejacks0n commented 8 years ago

That's good info. Can I understand why you want to do this? I don't know that things will work when you change this, and am curious how you're using it.


Jeremy Jackson

On Dec 5, 2015, at 5:12 PM, Christian Schulze notifications@github.com wrote:

Have found a solution. With some pocking around window.env by adding (console.log prop + ': ' + value) for prop, value of window.env to spec_helper.coffee, I found:

ui: function (ui) { Mocha.prototype.ui.call(this, ui); this.suite.emit('pre-require', global, null, this); return this; } Calling window.env.ui('bdd-lazy-var') in spec_helper.coffee works perfectly.

Perhaps this could be added to the Mocha wiki page to help future developers?

— Reply to this email directly or view it on GitHub.

christian-schulze commented 8 years ago

I'm coming from an rspec background, where you can define lazy loaded variables using the let statement, and also assign a lazily loaded subject which you test in your assertions.

Whether this sort of feature addition to Mocha is valid is up for debate, so lets leave that for another discussion.

The bdd-lazy-var module allows injecting this behaviour into Mocha via the ui interface, which is a well defined api. After which you can now write specs like this:

describe 'Widget', ->
  subject ->
    new Widget(get('color'), get('price'))

  describe '#build', ->
    def 'color', -> 'black'
    def 'price', -> 50

    it 'assigns the color', ->
      expect(subject().color).to.equal('black')

    it 'assigns the price', ->
      expect(subject().price).to.equal(50)

A very contrived example, but demonstrates how you can dry up the assertions.

I've actually switched to using the lazy-bdd implementation as the projects code is cleaner and more easily understood. An example spec using lazy-bdd looks like:

describe 'Widget', ->
  subject ->
    new Widget(@color, @price)

  describe '#build', ->
    lazy 'color', -> 'black'
    lazy 'price', -> 50

    it 'assigns the color', ->
      expect(@subject.color).to.equal('black')

    it 'assigns the price', ->
      expect(@subject.price).to.equal(50)
christian-schulze commented 8 years ago

Will close this as everything is good.

jejacks0n commented 8 years ago

Thanks for the info!