diff --git a/browser-entry.js b/browser-entry.js index 572195b504..2d1aeecea5 100644 --- a/browser-entry.js +++ b/browser-entry.js @@ -45,7 +45,7 @@ process.removeListener = function (e, fn) { } else { global.onerror = function () {}; } - var i = Mocha.utils.indexOf(uncaughtExceptionHandlers, fn); + var i = uncaughtExceptionHandlers.indexOf(fn); if (i !== -1) { uncaughtExceptionHandlers.splice(i, 1); } @@ -103,7 +103,7 @@ Mocha.Runner.immediately = function (callback) { * only receive the 'message' attribute of the Error. */ mocha.throwError = function (err) { - Mocha.utils.forEach(uncaughtExceptionHandlers, function (fn) { + uncaughtExceptionHandlers.forEach(function (fn) { fn(err); }); throw err; diff --git a/lib/context.js b/lib/context.js index 95065b540d..38b2de28b3 100644 --- a/lib/context.js +++ b/lib/context.js @@ -71,11 +71,10 @@ Context.prototype.slow = function (ms) { * Mark a test as skipped. * * @api private - * @return {Context} self + * @throws Pending */ Context.prototype.skip = function () { this.runnable().skip(); - return this; }; /** diff --git a/lib/runner.js b/lib/runner.js index 16144c1f90..18bc2397db 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -10,15 +10,10 @@ var utils = require('./utils'); var inherits = utils.inherits; var debug = require('debug')('mocha:runner'); var Runnable = require('./runnable'); -var filter = utils.filter; -var indexOf = utils.indexOf; -var some = utils.some; -var keys = utils.keys; var stackFilter = utils.stackTraceFilter(); var stringify = utils.stringify; var type = utils.type; var undefinedError = utils.undefinedError; -var isArray = utils.isArray; /** * Non-enumerable globals. @@ -150,11 +145,11 @@ Runner.prototype.grepTotal = function (suite) { * @api private */ Runner.prototype.globalProps = function () { - var props = keys(global); + var props = Object.keys(global); // non-enumerables for (var i = 0; i < globals.length; ++i) { - if (~indexOf(props, globals[i])) { + if (~props.indexOf(globals[i])) { continue; } props.push(globals[i]); @@ -316,7 +311,7 @@ Runner.prototype.hook = function (name, fn) { if (name === 'beforeEach' || name === 'afterEach') { self.test.pending = true; } else { - utils.forEach(suite.tests, function (test) { + suite.tests.forEach(function (test) { test.pending = true; }); // a pending hook won't be executed twice. @@ -780,19 +775,19 @@ function cleanSuiteReferences (suite) { } } - if (isArray(suite._beforeAll)) { + if (Array.isArray(suite._beforeAll)) { cleanArrReferences(suite._beforeAll); } - if (isArray(suite._beforeEach)) { + if (Array.isArray(suite._beforeEach)) { cleanArrReferences(suite._beforeEach); } - if (isArray(suite._afterAll)) { + if (Array.isArray(suite._afterAll)) { cleanArrReferences(suite._afterAll); } - if (isArray(suite._afterEach)) { + if (Array.isArray(suite._afterEach)) { cleanArrReferences(suite._afterEach); } @@ -890,7 +885,7 @@ function filterOnly (suite) { } else { // Otherwise, do not run any of the tests in this suite. suite.tests = []; - utils.forEach(suite._onlySuites, function (onlySuite) { + suite._onlySuites.forEach(function (onlySuite) { // If there are other `only` tests/suites nested in the current `only` suite, then filter that `only` suite. // Otherwise, all of the tests on this `only` suite should be run, so don't filter it. if (hasOnly(onlySuite)) { @@ -898,8 +893,8 @@ function filterOnly (suite) { } }); // Run the `only` suites, as well as any other suites that have `only` tests/suites as descendants. - suite.suites = filter(suite.suites, function (childSuite) { - return indexOf(suite._onlySuites, childSuite) !== -1 || filterOnly(childSuite); + suite.suites = suite.suites.filter(function (childSuite) { + return suite._onlySuites.indexOf(childSuite) !== -1 || filterOnly(childSuite); }); } // Keep the suite only if there is something to run @@ -914,7 +909,7 @@ function filterOnly (suite) { * @api private */ function hasOnly (suite) { - return suite._onlyTests.length || suite._onlySuites.length || some(suite.suites, hasOnly); + return suite._onlyTests.length || suite._onlySuites.length || suite.suites.some(hasOnly); } /** @@ -926,7 +921,7 @@ function hasOnly (suite) { * @return {Array} */ function filterLeaks (ok, globals) { - return filter(globals, function (key) { + return globals.filter(function (key) { // Firefox and Chrome exposes iframes as index inside the window object if (/^\d+/.test(key)) { return false; @@ -950,7 +945,7 @@ function filterLeaks (ok, globals) { return false; } - var matched = filter(ok, function (ok) { + var matched = ok.filter(function (ok) { if (~ok.indexOf('*')) { return key.indexOf(ok.split('*')[0]) === 0; } @@ -969,7 +964,7 @@ function filterLeaks (ok, globals) { function extraGlobals () { if (typeof process === 'object' && typeof process.version === 'string') { var parts = process.version.split('.'); - var nodeVersion = utils.reduce(parts, function (a, v) { + var nodeVersion = parts.reduce(function (a, v) { return a << 8 | v; }); diff --git a/lib/suite.js b/lib/suite.js index c592e46bc1..f5dca4cfa5 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -383,7 +383,7 @@ Suite.prototype.titlePath = function () { * @return {number} */ Suite.prototype.total = function () { - return utils.reduce(this.suites, function (sum, suite) { + return this.suites.reduce(function (sum, suite) { return sum + suite.total(); }, 0) + this.tests.length; }; @@ -397,8 +397,8 @@ Suite.prototype.total = function () { * @return {Suite} */ Suite.prototype.eachTest = function (fn) { - utils.forEach(this.tests, fn); - utils.forEach(this.suites, function (suite) { + this.tests.forEach(fn); + this.suites.forEach(function (suite) { suite.eachTest(fn); }); return this; diff --git a/lib/utils.js b/lib/utils.js index a4357258cf..2770a1f1b4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,7 +8,7 @@ var basename = require('path').basename; var debug = require('debug')('mocha:watch'); -var exists = require('fs').existsSync || require('path').existsSync; +var exists = require('fs').existsSync; var glob = require('glob'); var path = require('path'); var join = path.join; @@ -38,20 +38,6 @@ exports.escape = function (html) { return he.encode(String(html), { useNamedReferences: false }); }; -/** - * Array#forEach (<=IE8) - * - * @api private - * @param {Array} arr - * @param {Function} fn - * @param {Object} scope - */ -exports.forEach = function (arr, fn, scope) { - for (var i = 0, l = arr.length; i < l; i++) { - fn.call(scope, arr[i], i); - } -}; - /** * Test if the given obj is type of string. * @@ -63,118 +49,6 @@ exports.isString = function (obj) { return typeof obj === 'string'; }; -/** - * Array#map (<=IE8) - * - * @api private - * @param {Array} arr - * @param {Function} fn - * @param {Object} scope - * @return {Array} - */ -exports.map = function (arr, fn, scope) { - var result = []; - for (var i = 0, l = arr.length; i < l; i++) { - result.push(fn.call(scope, arr[i], i, arr)); - } - return result; -}; - -/** - * Array#indexOf (<=IE8) - * - * @api private - * @param {Array} arr - * @param {Object} obj to find index of - * @param {number} start - * @return {number} - */ -var indexOf = exports.indexOf = function (arr, obj, start) { - for (var i = start || 0, l = arr.length; i < l; i++) { - if (arr[i] === obj) { - return i; - } - } - return -1; -}; - -/** - * Array#reduce (<=IE8) - * - * @api private - * @param {Array} arr - * @param {Function} fn - * @param {Object} val Initial value. - * @return {*} - */ -var reduce = exports.reduce = function (arr, fn, val) { - var rval = val; - - for (var i = 0, l = arr.length; i < l; i++) { - rval = fn(rval, arr[i], i, arr); - } - - return rval; -}; - -/** - * Array#filter (<=IE8) - * - * @api private - * @param {Array} arr - * @param {Function} fn - * @return {Array} - */ -exports.filter = function (arr, fn) { - var ret = []; - - for (var i = 0, l = arr.length; i < l; i++) { - var val = arr[i]; - if (fn(val, i, arr)) { - ret.push(val); - } - } - - return ret; -}; - -/** - * Array#some (<=IE8) - * - * @api private - * @param {Array} arr - * @param {Function} fn - * @return {Array} - */ -exports.some = function (arr, fn) { - for (var i = 0, l = arr.length; i < l; i++) { - if (fn(arr[i])) { - return true; - } - } - return false; -}; - -/** - * Object.keys (<=IE8) - * - * @api private - * @param {Object} obj - * @return {Array} keys - */ -exports.keys = typeof Object.keys === 'function' ? Object.keys : function (obj) { - var keys = []; - var has = Object.prototype.hasOwnProperty; // for `window` on <=IE8 - - for (var key in obj) { - if (has.call(obj, key)) { - keys.push(key); - } - } - - return keys; -}; - /** * Watch the given `files` for changes * and invoke `fn(file)` on modification. @@ -195,19 +69,6 @@ exports.watch = function (files, fn) { }); }; -/** - * Array.isArray (<=IE8) - * - * @api private - * @param {Object} obj - * @return {Boolean} - */ -var isArray = typeof Array.isArray === 'function' ? Array.isArray : function (obj) { - return Object.prototype.toString.call(obj) === '[object Array]'; -}; - -exports.isArray = isArray; - /** * Ignored files. * @@ -280,18 +141,7 @@ exports.clean = function (str) { str = str.replace(re, ''); - return exports.trim(str); -}; - -/** - * Trim the given `str`. - * - * @api private - * @param {string} str - * @return {string} - */ -exports.trim = function (str) { - return str.replace(/^\s+|\s+$/g, ''); + return str.trim(); }; /** @@ -302,7 +152,7 @@ exports.trim = function (str) { * @return {Object} */ exports.parseQuery = function (qs) { - return reduce(qs.replace('?', '').split('&'), function (obj, pair) { + return qs.replace('?', '').split('&').reduce(function (obj, pair) { var i = pair.indexOf('='); var key = pair.slice(0, i); var val = pair.slice(++i); @@ -425,7 +275,7 @@ var type = exports.type = function type (value) { exports.stringify = function (value) { var typeHint = type(value); - if (!~indexOf(['object', 'array', 'function'], typeHint)) { + if (!~['object', 'array', 'function'].indexOf(typeHint)) { if (typeHint === 'buffer') { var json = Buffer.prototype.toJSON.call(value); // Based on the toJSON result @@ -436,7 +286,7 @@ exports.stringify = function (value) { // IE7/IE8 has a bizarre String constructor; needs to be coerced // into an array and back to obj. if (typeHint === 'string' && typeof value === 'object') { - value = reduce(value.split(''), function (acc, char, idx) { + value = value.split('').reduce(function (acc, char, idx) { acc[idx] = char; return acc; }, {}); @@ -472,9 +322,9 @@ function jsonStringify (object, spaces, depth) { depth = depth || 1; var space = spaces * depth; - var str = isArray(object) ? '[' : '{'; - var end = isArray(object) ? ']' : '}'; - var length = typeof object.length === 'number' ? object.length : exports.keys(object).length; + var str = Array.isArray(object) ? '[' : '{'; + var end = Array.isArray(object) ? ']' : '}'; + var length = typeof object.length === 'number' ? object.length : Object.keys(object).length; // `.repeat()` polyfill function repeat (s, n) { return new Array(n).join(s); @@ -527,7 +377,7 @@ function jsonStringify (object, spaces, depth) { } --length; str += '\n ' + repeat(' ', space) + - (isArray(object) ? '' : '"' + i + '": ') + // key + (Array.isArray(object) ? '' : '"' + i + '": ') + // key _stringify(object[i]) + // value (length ? ',' : ''); // comma } @@ -570,7 +420,7 @@ exports.canonicalize = function canonicalize (value, stack, typeHint) { stack = stack || []; - if (indexOf(stack, value) !== -1) { + if (stack.indexOf(value) !== -1) { return '[Circular]'; } @@ -582,7 +432,7 @@ exports.canonicalize = function canonicalize (value, stack, typeHint) { break; case 'array': withStack(value, function () { - canonicalizedObj = exports.map(value, function (item) { + canonicalizedObj = value.map(function (item) { return exports.canonicalize(item, stack); }); }); @@ -602,7 +452,7 @@ exports.canonicalize = function canonicalize (value, stack, typeHint) { case 'object': canonicalizedObj = canonicalizedObj || {}; withStack(value, function () { - exports.forEach(exports.keys(value).sort(), function (key) { + Object.keys(value).sort().forEach(function (key) { canonicalizedObj[key] = exports.canonicalize(value[key], stack); }); }); @@ -742,7 +592,7 @@ exports.stackTraceFilter = function () { return function (stack) { stack = stack.split('\n'); - stack = reduce(stack, function (list, line) { + stack = stack.reduce(function (list, line) { if (isMochaInternal(line)) { return list; } diff --git a/test/unit/utils.spec.js b/test/unit/utils.spec.js index b945784438..080ddcb9c3 100644 --- a/test/unit/utils.spec.js +++ b/test/unit/utils.spec.js @@ -494,79 +494,6 @@ describe('lib/utils', function () { }); }); - describe('map()', function () { - var map = utils.map; - it('should behave same as Array.prototype.map', function () { - if (!Array.prototype.map) { - this.skip(); - return; - } - - var arr = [ - 1, - 2, - 3 - ]; - expect(map(arr, JSON.stringify)) - .to - .eql(arr.map(JSON.stringify)); - }); - - it('should call the callback with 3 arguments[currentValue, index, array]', - function () { - var index = 0; - map([ - 1, - 2, - 3 - ], function (e, i, arr) { - expect(e).to.equal(arr[index]); - expect(i).to.equal(index++); - }); - }); - - it('should apply with the given scope', function () { - var scope = {}; - map([ - 'a', - 'b', - 'c' - ], function () { - expect(this).to.equal(scope); - }, scope); - }); - }); - - describe('some()', function () { - var some = utils.some; - - it( - 'should return true when some array elements pass the check of the fn parameter', - function () { - var result = some([ - 'a', - 'b', - 'c' - ], function (e) { - return e === 'b'; - }); - expect(result).to.eql(true); - }); - - it( - 'should return false when none of the array elements pass the check of the fn parameter', - function () { - var result = some([ - 'a', - 'b', - 'c' - ], function (e) { - return e === 'd'; - }); - expect(result).to.eql(false); - }); - }); - describe('parseQuery()', function () { var parseQuery = utils.parseQuery; it('should get queryString and return key-value object', function () {