ember-mocha icon indicating copy to clipboard operation
ember-mocha copied to clipboard

Component tests run fine if in the 'root' describe block, but throw 'You have turned on testing mode, which disabled the run-loop's autorun' error when in nested describe blocks

Open fushi opened this issue 8 years ago • 15 comments

The following pseudo code works fine:

describe('Unit | Component | common | input-with-validation', function() {
  setupComponentTest('componentToTest', {
    needs: [],
    unit: true
  });
  beforeEach(function () {
    component = this.subject();
  });

  it('Test Observer', function() {
    component.set('observedKey', true);
  }
}

but the following does not, and raises an assertion error: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run

describe('Unit | Component | common | input-with-validation', function() {
  setupComponentTest('componentToTest', {
    needs: [],
    unit: true
  });

  beforeEach(function () {
    component = this.subject();
  });

  describe('Observers', function () {
    it('Test Observer', function() {
      component.set('observedKey', true);
    }
  }
}

fushi avatar Jan 24 '17 20:01 fushi

@fushi where are the setupComponentTest() calls in your code?

Turbo87 avatar Jan 24 '17 20:01 Turbo87

Updated the original question with more complete code blocks

fushi avatar Jan 24 '17 20:01 fushi

I've got a very ugly workaround, for now:

Create a test helper

import Ember from 'ember';
import { it as originalIt } from 'mocha';

function it(label, func) {
  originalIt(label, function () {
    Ember.run(this, func);
  });
}

Object.keys(originalIt).forEach(key => it[key] = originalIt[key]);

export {
  it
};

And in the test file:

import { it } from 'tests/helpers/mocha-helper';

fushi avatar Jan 24 '17 21:01 fushi

import { expect } from 'chai';
import { describe, it, beforeEach } from 'mocha';
import { setupComponentTest } from 'ember-mocha';

describe('Unit | Component | foo bar', function() {
  setupComponentTest('foo-bar', {
    // Specify the other units that are required for this test
    // needs: ['component:foo', 'helper:bar'],
    unit: true
  });

  let component;
  beforeEach(function() {
    component = this.subject();
  });

  describe('bla', function() {
    it('renders', function() {
      component.set('foo', 21);
      expect(component.get('bar')).to.equal(42);
    });
  });
});

seems to work fine for me

Turbo87 avatar Jan 29 '17 11:01 Turbo87

Rendering is fine, it's only when you attempt to modify a component attribute that is being watched by an observer.

fushi avatar Jan 30 '17 23:01 fushi

Can you share a demo repo? I'm getting confused 🙃

rwjblue avatar Jan 31 '17 00:01 rwjblue

I just reproduced this in our app. Here is an example test that reproduces the error: https://github.com/efx/ember-mocha/commit/560abc959fe70abd8baf8313eeecb055117aebe0

ghost avatar Jan 31 '17 22:01 ghost

Just ran in to this as well, any ideas? Is it basically just recommended to not use nested describe blocks with ember-mocha?

plwalters avatar May 31 '17 18:05 plwalters

@PWKad We've been using the workaround from above successfully for the last 6 months.

fushi avatar May 31 '17 18:05 fushi

Rebased the above reproduction failure on this branch: https://github.com/efx/ember-mocha/tree/bug-132.

ghost avatar Dec 08 '17 14:12 ghost

The error comes from an ember.js or ember-data validation. Wrapping a try catch around this line causes the error to disappear: https://github.com/emberjs/ember-mocha/blob/master/vendor/ember-mocha/ember-mocha-adapter.js#L69. That seems like a workaround. I do not know what effects it could have on other kinds of exceptions, et. al. @Turbo87 is this kind of fix worth pursuing? Or are there broader refactors that could help this case?

ghost avatar Dec 08 '17 16:12 ghost

that link goes to 404 for me 🤔

IMHO similar to how QUnit does it we should not start any runloops automatically in Mocha. This has been a major pain and also can't be implemented reliably. This is obviously a breaking change, so my plan is to tackle these things once the Ember CLI addon transition (see #173) has been completed.

Turbo87 avatar Dec 08 '17 16:12 Turbo87

whoops, updated so it should work. Ah interesting. Great, thanks for the update. I'll be glad to help test / try out the fixes on the application I work on.

ghost avatar Dec 08 '17 16:12 ghost

@efx Just hit the same problem. Can you please share the reproduction links again? The above mentioned ones seem to be broken.

rreckonerr avatar May 16 '20 21:05 rreckonerr

@rreckonerr I deleted my fork and that work; apologies! I will take a look later today to see if I have a copy locally. I reduced the example to the most basic form. You should be able to see the issue by simply nesting the 2nd describe in the first one. For the record, our team avoids nesting describe blocks in our ember application because of this.

ghost avatar Jun 01 '20 11:06 ghost