<commit_before>import {
  beforeEach,
  afterEach
} from 'mocha';
import {
  setupContext,
  teardownContext
} from '@ember/test-helpers';
import { assign, merge } from '@ember/polyfills';
import { resolve } from 'rsvp';

const _assign = assign || merge;

function chainHooks(hooks, context, promise = resolve()) {
  return hooks.reduce((promise, fn) => promise.then(fn.bind(context)), promise);
}

export default function setupUnitTest(options) {
  let originalContext;
  let beforeEachHooks = [];
  let afterEachHooks = [];

  beforeEach(function() {
    originalContext = _assign({}, this);

    let promise = setupContext(this, options).then(() => {

      let originalPauseTest = this.pauseTest;
      this.pauseTest = function Mocha_pauseTest() {
        this.timeout(0); // prevent the test from timing out

        return originalPauseTest.call(this);
      };
    });

    return chainHooks(beforeEachHooks, this, promise);
  });

  afterEach(function() {
    let promise = chainHooks(afterEachHooks, this);

    return promise
      .then(() => teardownContext(this))
      .then(() => {
        // delete any extraneous properties
        for (let key in this) {
          if (!(key in originalContext)) {
            delete this[key];
          }
        }

        // copy over the original values
        _assign(this, originalContext);
      });
  });

  /**
   * Provide a workaround for the inconvenient FIFO-always order of beforeEach/afterEach calls
   *<commit_msg>Simplify `chainHooks()` function<commit_after>import {
  beforeEach,
  afterEach
} from 'mocha';
import {
  setupContext,
  teardownContext
} from '@ember/test-helpers';
import { assign, merge } from '@ember/polyfills';
import { resolve } from 'rsvp';

const _assign = assign || merge;

function chainHooks(hooks, context) {
  return hooks.reduce((promise, fn) => promise.then(fn.bind(context)), resolve());
}

export default function setupUnitTest(options) {
  let originalContext;
  let beforeEachHooks = [];
  let afterEachHooks = [];

  beforeEach(function() {
    originalContext = _assign({}, this);

    return setupContext(this, options).then(() => {

      let originalPauseTest = this.pauseTest;
      this.pauseTest = function Mocha_pauseTest() {
        this.timeout(0); // prevent the test from timing out

        return originalPauseTest.call(this);
      };
    }).then(() => chainHooks(beforeEachHooks, this));
  });

  afterEach(function() {
    return chainHooks(afterEachHooks, this)
      .then(() => teardownContext(this))
      .then(() => {
        // delete any extraneous properties
        for (let key in this) {
          if (!(key in originalContext)) {
            delete this[key];
          }
        }

        // copy over the original values
        _assign(this, originalContext);
      });
  });

  /**
   * Provide a workaround for the inconvenient FIFO-always order of beforeEach/afterEach calls
   *
{'fraction_non_alphanumeric': 0.12786324786324788, 'fraction_numerical': 0.0006837606837606838, 'mean_word_length': 3.0082191780821916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2398896', 'n_tokens_mistral': 864, 'n_tokens_neox': 834, 'n_words': 221}