code: function wrapMocha() {
    const log = console.log.bind(console);
    console.log = (...args) => {
      if (args.length !== 0) {
        return log(...args);
      } else {
        return log('');  // send blank line if no args
      }
    };
  
    function runnerDone(runner) {
      // store tests under their categories
      const tests = {'all': []};
      ['pass', 'fail', 'pending'].forEach((type) => {
        const t = [];
        tests[type] = t;
        runner.on(type, (test) => {
          const flat = flatten(test);
          t.push(flat);
          tests['all'].push(flat);
        });
      });
  
      /**
       * Flatten Mocha's `Test` object into plain JSON.
       */
      function flatten(test) {
        return {
          title: test.title,
          duration: test.duration,
          err: test.err ? Object.assign({}, test.err) : null,
        };
      }
  
      return new Promise((resolve) => {
        runner.on('end', () => resolve(tests));
      });
    }
  
    Object.defineProperty(window, 'mocha', {
      get() {
        return undefined;
      },
      set(instance) {
        delete window.mocha;
        window.mocha = instance;
  
        // trick Mocha into outputing terminal-like output
        instance.constructor.reporters.Base.useColors = true;
        instance.reporter(Mocha.reporters.spec);
  
        const run = instance.run.bind(instance);
        instance.run = (...args) => {
          const runner = run(...args);
  
          // steal output and log to common global to report completion
          runnerDone(runner).then((out) => {
            window.__mochaTest = out;
          });
  
          return runner;
        };
  
        delete window.mocha;
        window.mocha = instance;
      },
      configurable: true,
    });
  }
docs: Serialized and run inside the client/test page.