How to use the mocha.prototype function in mocha

To help you get started, we’ve selected a few mocha examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tfennelly / jenkins-js-builder / internal / tests.js View on Github external
if (dependencies.getDependency('babel-core')) {
        mochaConfig.compilers = {
            js: require('babel-core/register')
        };
    }

    //
    // Mocha spec loading can fail during mocha initialization. If path.resolve
    // fails for some reason, you get a stack trace, but it's totally useless
    // because it doesn't tell you which spec file was the source of the resolve
    // error. The following code patches that.
    //
    var MochaConstructor = require('mocha');
    if (MochaConstructor.prototype.loadFiles && !MochaConstructor.__jenkinsLoadfilesWrapped) {
        var path = require('path');
        MochaConstructor.prototype.loadFiles = function(fn) {
            var self = this;
            var suite = this.suite;
            this.files.forEach(function(file) {
                try {
                    file = path.resolve(file);
                    suite.emit('pre-require', global, file, self);
                    suite.emit('require', require(file), file, self);
                    suite.emit('post-require', global, file, self);
                } catch(e) {
                    logger.logError('*****************************************************************');
                    logger.logError('Mocha test initialization failure. Failed to load spec file "' + file + '". Tests will not run. See stack trace below.');
                    logger.logError('*****************************************************************');
                    throw e;
                }
            });
            fn && fn();
github ripple / rippled / test / mocha-loader-patch.js View on Github external
return amountParse.call(this, j);
}

var accountParse = ripplelib.UInt160.prototype.parse_json;
ripplelib.UInt160.prototype.parse_json = function(j) {
  if (config.accounts[j]) {
    j = config.accounts[j].account;
  }
  return accountParse.call(this, j);
}


var oldLoader = mocha.prototype.loadFiles
if (!oldLoader.monkeyPatched) {
  // Gee thanks Mocha ...
  mocha.prototype.loadFiles = function() {
    try {
      oldLoader.apply(this, arguments);
    } catch (e) {
      // Normally mocha just silently bails
      console.error(e.stack);
      // We throw, so mocha doesn't continue trying to run the test suite
      throw e;
    }
  }
  mocha.prototype.loadFiles.monkeyPatched = true;
};
github ripple / rippled / test / mocha-loader-patch.js View on Github external
var oldLoader = mocha.prototype.loadFiles
if (!oldLoader.monkeyPatched) {
  // Gee thanks Mocha ...
  mocha.prototype.loadFiles = function() {
    try {
      oldLoader.apply(this, arguments);
    } catch (e) {
      // Normally mocha just silently bails
      console.error(e.stack);
      // We throw, so mocha doesn't continue trying to run the test suite
      throw e;
    }
  }
  mocha.prototype.loadFiles.monkeyPatched = true;
};
github tfennelly / jenkins-js-builder / internal / tests.js View on Github external
var mochaConfig = {};

    if (dependencies.getDependency('babel-core')) {
        mochaConfig.compilers = {
            js: require('babel-core/register')
        };
    }

    //
    // Mocha spec loading can fail during mocha initialization. If path.resolve
    // fails for some reason, you get a stack trace, but it's totally useless
    // because it doesn't tell you which spec file was the source of the resolve
    // error. The following code patches that.
    //
    var MochaConstructor = require('mocha');
    if (MochaConstructor.prototype.loadFiles && !MochaConstructor.__jenkinsLoadfilesWrapped) {
        var path = require('path');
        MochaConstructor.prototype.loadFiles = function(fn) {
            var self = this;
            var suite = this.suite;
            this.files.forEach(function(file) {
                try {
                    file = path.resolve(file);
                    suite.emit('pre-require', global, file, self);
                    suite.emit('require', require(file), file, self);
                    suite.emit('post-require', global, file, self);
                } catch(e) {
                    logger.logError('*****************************************************************');
                    logger.logError('Mocha test initialization failure. Failed to load spec file "' + file + '". Tests will not run. See stack trace below.');
                    logger.logError('*****************************************************************');
                    throw e;
                }
github ripple / rippled / test / mocha-loader-patch.js View on Github external
j = String(Math.floor(parseFloat(j, 10) * 1e6));
    }
  }
  return amountParse.call(this, j);
}

var accountParse = ripplelib.UInt160.prototype.parse_json;
ripplelib.UInt160.prototype.parse_json = function(j) {
  if (config.accounts[j]) {
    j = config.accounts[j].account;
  }
  return accountParse.call(this, j);
}


var oldLoader = mocha.prototype.loadFiles
if (!oldLoader.monkeyPatched) {
  // Gee thanks Mocha ...
  mocha.prototype.loadFiles = function() {
    try {
      oldLoader.apply(this, arguments);
    } catch (e) {
      // Normally mocha just silently bails
      console.error(e.stack);
      // We throw, so mocha doesn't continue trying to run the test suite
      throw e;
    }
  }
  mocha.prototype.loadFiles.monkeyPatched = true;
};
github mocha-parallel / mocha-parallel-tests / api.js View on Github external
runTests({
            options: Object.assign({}, {
                reporterName: this._reporterName,
                reporterOptions: this._reporterOptions,
                reporter: Reporter,
                testsLength: this._filesTotal
            }),
            throttledCalls: this._throttledCalls
        });

        return this._customRunner;
    }
}

Object.keys(Mocha.prototype).forEach(key => {
    if (typeof Mocha.prototype[key] !== 'function') {
        return;
    }

    // we have our own implementations of these methods
    // other methods should be saved and re-applied during runTests()
    if (key === 'run' || key === 'addFile' || key === 'reporter') {
        return;
    }

    MochaParallelTests.prototype[key] = function (...args) {
        // mocha calls some of its methods inside constructor
        // so MochaParallelTests own constructor function can still be in progress here
        this._throttledCalls = this._throttledCalls || [];

        this._throttledCalls.push({
github enobufs / mocha-prepare / index.js View on Github external
function prepare(onPrepare, onUnprepare) {
    // Monkey-patch run method
    var Mocha = require('mocha');
    var run = Mocha.prototype.run;

    Mocha.prototype.run = function (done) {
        var self = this;
        // Call onPrepare() before the actual run().
        onPrepare(function (err) {
            if (err) {
                if (err instanceof Error) {
                    console.error(err.stack);
                }
                process.exit(1);
                done(); // unreachable. test purpose only.
            } else {
                // Call the actual run().
                run.call(self, function () {
                    // All test complete with a result code.
                    // Pass forward the arugments for possible spec change
                    // in the future.
                    var thisArg = this;
github enobufs / mocha-prepare / index.js View on Github external
function prepare(onPrepare, onUnprepare) {
    // Monkey-patch run method
    var Mocha = require('mocha');
    var run = Mocha.prototype.run;

    Mocha.prototype.run = function (done) {
        var self = this;
        // Call onPrepare() before the actual run().
        onPrepare(function (err) {
            if (err) {
                if (err instanceof Error) {
                    console.error(err.stack);
                }
                process.exit(1);
                done(); // unreachable. test purpose only.
            } else {
                // Call the actual run().
                run.call(self, function () {
                    // All test complete with a result code.
                    // Pass forward the arugments for possible spec change
github walmartlabs / fruit-loops / test / lib / index.js View on Github external
global.should = chai.should();
chai.use(sinonChai);
chai.use(require('chai-properties'));

var sinon = require('sinon');

sinon.config = {
  injectIntoThis: true,
  injectInto: null,
  properties: ['spy', 'stub', 'mock', 'clock', 'sandbox', 'server', 'requests', 'on'],
  useFakeTimers: [10],
  useFakeServer: true
};

var loadFiles = Mocha.prototype.loadFiles;
Mocha.prototype.loadFiles = function() {
  this.suite.beforeEach(function() {
    var config = sinon.getConfig(sinon.config);
    config.injectInto = this;
    this.sandbox = sinon.sandbox.create(config);
  });
  this.suite.afterEach(function() {
    this.clock.tick(1000);
    this.sandbox.verifyAndRestore();
  });

  return loadFiles.apply(this);
};
github walmartlabs / fruit-loops / test / lib / index.js View on Github external
global.should = chai.should();
chai.use(sinonChai);
chai.use(require('chai-properties'));

var sinon = require('sinon');

sinon.config = {
  injectIntoThis: true,
  injectInto: null,
  properties: ['spy', 'stub', 'mock', 'clock', 'sandbox', 'server', 'requests', 'on'],
  useFakeTimers: [10],
  useFakeServer: true
};

var loadFiles = Mocha.prototype.loadFiles;
Mocha.prototype.loadFiles = function() {
  this.suite.beforeEach(function() {
    var config = sinon.getConfig(sinon.config);
    config.injectInto = this;
    this.sandbox = sinon.sandbox.create(config);
  });
  this.suite.afterEach(function() {
    this.clock.tick(1000);
    this.sandbox.verifyAndRestore();
  });

  return loadFiles.apply(this);
};