<NME> setup-test-test.js
<BEF> import { setupContainerTest } from 'ember-mocha';
import { describe, it, beforeEach, afterEach, after } from 'mocha';
import { expect } from 'chai';
import { pauseTest, resumeTest } from '@ember/test-helpers';
import Service from '@ember/service';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import { Promise } from 'rsvp';

describe('setupContainerTest', function() {
  if (!hasEmberVersion(2, 4)) {
    return;
  }

  describe('context setup', function() {

    setupContainerTest();

    afterEach(function() {
      expect(this.owner, 'Context does not leak between tests').to.be.undefined;
    });

    it('sets up owner', function() {
      expect(this.owner, 'Owner exists').to.exist;
      this.owner.register('service:dummy', Service.extend({ foo: 'bar' }));

      let subject = this.owner.lookup('service:dummy');
      expect(subject.get('foo')).to.equal('bar');
    });

    it('has setters and getters', function() {
      expect(this)
        .to.respondTo('get')
        .and.to.respondTo('getProperties')
        .and.to.respondTo('set')
        .and.to.respondTo('setProperties');

      this.set('foo', 'bar');
      expect(this.get('foo')).to.equal('bar');
    });
  });

  describe('pauseTest/resumeTest', function() {

    setupContainerTest();

    it('can pause tests without timeouts', async function() {
      this.timeout(100);

      setTimeout(resumeTest, 200); // resume test after timeout
      // make sure pauseTest does not wait forever, if resumeTest fails
      let timer = setTimeout(() => {
        throw new Error('resumeTest() did not work')
      }, 300);

      await pauseTest();
      clearTimeout(timer);
    });
  });

  describe('context in beforeEach/afterEach hooks', function() {

    // @todo will throw `this.get is not a function` if called after `setupRenderingTest()`
    afterEach(function() {
      expect(this.get('name')).to.equal('blue');
    });

    setupContainerTest();

    beforeEach(function() {
      this.set('name', 'red');
    });

    beforeEach(function() {
      expect(this.get('name')).to.equal('red');
    });

    it('has correct context', async function() {
      expect(this.get('name')).to.equal('red');
      this.set('name', 'blue');
    });
  });

  describe('hooks API', function() {

    describe('calls hooks in order', function() {
      let calledSteps = [];

      function setupFoo(hooks) {
        hooks.beforeEach(function() {
          expect(this.owner, 'Context is set up already').to.exist;
          expect(calledSteps).to.deep.equal([]);
          calledSteps.push('bE1');
        });
        hooks.afterEach(function() {
          expect(calledSteps, 'afterEach is called in LIFO order').to.deep.equal(['bE1', 'bE2', 'it', 'aE2']);
          calledSteps.push('aE1');
        });
      }

      after(function() {
        expect(calledSteps, 'hooks are called in correct order').to.deep.equal(['bE1', 'bE2', 'it', 'aE2', 'aE1']);
      });

      let hooks = setupContainerTest();
      setupFoo(hooks);

      hooks.beforeEach(function() {
        expect(calledSteps, 'beforeEach is called in FIFO order').to.deep.equal(['bE1']);
        calledSteps.push('bE2');
      });
      hooks.afterEach(function() {
        expect(calledSteps, 'afterEach is called in LIFO order').to.deep.equal(['bE1', 'bE2', 'it']);
        calledSteps.push('aE2');
      });

      it('calls beforeEach/afterEach in FIFO/LIFO order', function() {
        expect(calledSteps, 'it() is called after all beforeEach').to.deep.equal(['bE1', 'bE2']);
        calledSteps.push('it');
      });
    });

    describe('is Promise aware', function() {
      let calledSteps = [];
      function delay(ms) {
        return new Promise((resolve) => setTimeout(resolve, ms));
      }

      after(function() {
        expect(calledSteps, 'hooks are called in correct order').to.deep.equal(['bE1', 'bE2', 'it', 'aE2', 'aE1']);
      });

      let hooks = setupContainerTest();

      hooks.beforeEach(function() {
        expect(calledSteps, 'beforeEach waits for promise').to.deep.equal([]);
        return delay(10)
          .then(() => calledSteps.push('bE1'));
      });
      hooks.beforeEach(function() {
        expect(calledSteps, 'beforeEach waits for promise').to.deep.equal(['bE1']);
        return delay(10)
          .then(() => calledSteps.push('bE2'));
      });
      hooks.afterEach(function() {
        expect(calledSteps, 'afterEach waits for promise').to.deep.equal(['bE1', 'bE2', 'it', 'aE2']);
        return delay(10)
          .then(() => calledSteps.push('aE1'));
      });
      hooks.afterEach(function() {
        expect(calledSteps, 'afterEach waits for promise').to.deep.equal(['bE1', 'bE2', 'it']);
        return delay(10)
          .then(() => calledSteps.push('aE2'));
      });

      it('beforeEach/afterEach chain up promises', function() {
        expect(calledSteps, 'it() is called after all beforeEach').to.deep.equal(['bE1', 'bE2']);
        calledSteps.push('it');
      });
    });

  });

});

<MSG> Implement setupTest supporting old and new testing APIs

Closes #198

<DFF> @@ -1,4 +1,4 @@
-import { setupContainerTest } from 'ember-mocha';
+import { setupTest } from 'ember-mocha';
 import { describe, it, beforeEach, afterEach, after } from 'mocha';
 import { expect } from 'chai';
 import { pauseTest, resumeTest } from '@ember/test-helpers';
@@ -6,14 +6,14 @@ import Service from '@ember/service';
 import hasEmberVersion from 'ember-test-helpers/has-ember-version';
 import { Promise } from 'rsvp';
 
-describe('setupContainerTest', function() {
+describe('setupTest', function() {
   if (!hasEmberVersion(2, 4)) {
     return;
   }
 
   describe('context setup', function() {
 
-    setupContainerTest();
+    setupTest();
 
     afterEach(function() {
       expect(this.owner, 'Context does not leak between tests').to.be.undefined;
@@ -41,7 +41,7 @@ describe('setupContainerTest', function() {
 
   describe('pauseTest/resumeTest', function() {
 
-    setupContainerTest();
+    setupTest();
 
     it('can pause tests without timeouts', async function() {
       this.timeout(100);
@@ -64,7 +64,7 @@ describe('setupContainerTest', function() {
       expect(this.get('name')).to.equal('blue');
     });
 
-    setupContainerTest();
+    setupTest();
 
     beforeEach(function() {
       this.set('name', 'red');
@@ -101,7 +101,7 @@ describe('setupContainerTest', function() {
         expect(calledSteps, 'hooks are called in correct order').to.deep.equal(['bE1', 'bE2', 'it', 'aE2', 'aE1']);
       });
 
-      let hooks = setupContainerTest();
+      let hooks = setupTest();
       setupFoo(hooks);
 
       hooks.beforeEach(function() {
@@ -129,7 +129,7 @@ describe('setupContainerTest', function() {
         expect(calledSteps, 'hooks are called in correct order').to.deep.equal(['bE1', 'bE2', 'it', 'aE2', 'aE1']);
       });
 
-      let hooks = setupContainerTest();
+      let hooks = setupTest();
 
       hooks.beforeEach(function() {
         expect(calledSteps, 'beforeEach waits for promise').to.deep.equal([]);

Implement setupTest supporting old and new testing APIs