<commit_before>/* Grunt Task to test mocha in a local Chrome instance */

const puppeteer = require('puppeteer');
const EventHandler = require('eventhandler');
const util = require('util');
const mapSeries = require('promise-map-series');

module.exports = function(grunt) {
  grunt.registerMultiTask('mochaChrome', async function() {
    const done = this.async();

    try {
      const browser = await puppeteer.launch({
        headless: true,
        args: ['--no-sandbox']
      });

      const options = this.data.options;
      for (const testURL of options.urls) {
        const eventHandler1 = new EventHandler();
        const page = await browser.newPage();

        try {
          await page.evaluateOnNewDocument(`
            addEventListener('DOMContentLoaded', () => {
              if (typeof mocha !== 'undefined') {
                const _mochaRun = mocha.run.bind(mocha);
                mocha.reporter('spec');
                mocha.run = function(fn) {
                  return _mochaRun(function(stats) {
                    fireMochaEvent('mocha:ended', stats);
                    fn(stats);
                  });
                };
              }
            });
          `);

          await new Promise(async (resolve, reject) => {
            page.on('console', async msg => {
              const args = await mapSeries(msg.args(), v => v.jsonValue());
              console.log(util.format.apply(util, args));
            });

            eventHandler1.on('mocha:ended', resolve);

            await page.exposeFunction(
              'fireMochaEvent',
              eventHandler1.emit.bind(eventHandler1)
            );
            await page.goto(testURL);
          }).catch(reason => {
            throw reason;
          });
        } finally {
          await page.close({
            runBeforeUnload: false
          });
        }
      }

      await browser.close();

      done();
    } catch (e) {
      if (e instanceof Error) {
        done(e);
      } else {
        done(new Error(e));
      }
    }
  });
};<commit_msg>handle failures correctly<commit_after>/* Grunt Task to test mocha in a local Chrome instance */

const puppeteer = require('puppeteer');
const EventHandler = require('eventhandler');
const util = require('util');
const mapSeries = require('promise-map-series');

module.exports = function(grunt) {
  grunt.registerMultiTask('mochaChrome', async function() {
    const done = this.async();

    const browser = await puppeteer.launch({
      headless: true,
      args: ['--no-sandbox']
    });

    try {
      const options = this.data.options;
      for (const testURL of options.urls) {
        const eventHandler1 = new EventHandler();
        const page = await browser.newPage();

        try {
          await page.evaluateOnNewDocument(`
            addEventListener('DOMContentLoaded', () => {
              if (typeof mocha !== 'undefined') {
                const _mochaRun = mocha.run.bind(mocha);
                mocha.reporter('spec');
                mocha.useColors(true);
                mocha.run = function(fn) {
                  debugger;
                  var runner = _mochaRun(function(stats) {
                    if (typeof fn === 'function')
                      return fn(stats);
                  });

                  runner.on('end', () => {
                    fireMochaEvent('mocha:end', runner.stats);
                  });

                  return runner;
                };
              }
            });
          `);

          page.on('console', async msg => {
            const args = await mapSeries(msg.args(), v => v.jsonValue());
            console.log(util.format.apply(util, args));
          });

          await page.exposeFunction(
            'fireMochaEvent',
            eventHandler1.emit.bind(eventHandler1)
          );

          await new Promise(async (resolve, reject) => {
            eventHandler1.on('mocha:end', stats => {
              if (stats.failure) resolve(stats);
              else reject(stats);
            });

            await page.goto(testURL);
          });
        } finally {
          await page.close({
            runBeforeUnload: false
          });
        }
      }

      done();
    } catch (e) {
      if (e instanceof Error) {
        done(e);
      } else {
        done(new Error(e));
      }
    } finally {
      await browser.close();
    }
  });
};
{'fraction_non_alphanumeric': 0.1282225237449118, 'fraction_numerical': 0.0018091361374943465, 'mean_word_length': 1.58806319485079, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '744241', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1169, 'n_words': 256}