☕️ Classic, reliable, trusted test framework for Node.js and the browser
use ES6 in Node-only sources; closes #3146
Christopher Hiller1d486155e377
diff --git a/.eslintrc.yaml b/.eslintrc.ymlsimilarity index 77%rename from .eslintrc.yamlrename to .eslintrc.ymlindex d5673232..86337ec8 100644--- a/.eslintrc.yaml+++ b/.eslintrc.yml@@ -1,11 +1,11 @@root: true+extends: semistandardenv:- node: true- browser: true+ node: yes+ browser: yes+ es6: noparserOptions:- ecmaVersion: 5sourceType: script-extends: semistandardrules:strict:- errordiff --git a/bin/.eslintrc.yml b/bin/.eslintrc.ymlnew file mode 100644index 00000000..36e0a779--- /dev/null+++ b/bin/.eslintrc.yml@@ -0,0 +1,3 @@+env:+ es6: true+ browser: falsediff --git a/bin/_mocha b/bin/_mochaindex b3c14242..19cc1c50 100755--- a/bin/_mocha+++ b/bin/_mocha@@ -7,52 +7,136 @@* Module dependencies.*/-var program = require('commander');-var path = require('path');-var fs = require('fs');-var resolve = path.resolve;-var exists = fs.existsSync || path.existsSync;-var Mocha = require('../');-var utils = Mocha.utils;-var interfaceNames = Object.keys(Mocha.interfaces);-var join = path.join;-var cwd = process.cwd();-var getOptions = require('./options');-var mocha = new Mocha();+const program = require('commander');+const path = require('path');+const fs = require('fs');+const resolve = path.resolve;+const exists = fs.existsSync;+const Mocha = require('../');+const utils = Mocha.utils;+const interfaceNames = Object.keys(Mocha.interfaces);+const join = path.join;+const cwd = process.cwd();+const getOptions = require('./options');+const mocha = new Mocha();/*** Save timer references to avoid Sinon interfering (see GH-237).*/-var Date = global.Date;-var setTimeout = global.setTimeout;-var setInterval = global.setInterval;-var clearTimeout = global.clearTimeout;-var clearInterval = global.clearInterval;+const Date = global.Date;+const setTimeout = global.setTimeout;+const setInterval = global.setInterval;+const clearTimeout = global.clearTimeout;+const clearInterval = global.clearInterval;++/**+ * Exits Mocha when tests + code under test has finished execution (default)+ * @param {number} code - Exit code; typically # of failures+ */+const exitLater = code => {+ process.on('exit', () => {+ process.exit(Math.min(code, 255));+ });+};++/**+ * Exits Mocha when Mocha itself has finished execution, regardless of+ * what the tests or code under test is doing.+ * @param {number} code - Exit code; typically # of failures+ */+const exit = code => {+ const clampedCode = Math.min(code, 255);+ let draining = 0;++ // Eagerly set the process's exit code in case stream.write doesn't+ // execute its callback before the process terminates.+ process.exitCode = clampedCode;++ // flush output for Node.js Windows pipe bug+ // https://github.com/joyent/node/issues/6247 is just one bug example+ // https://github.com/visionmedia/mocha/issues/333 has a good discussion+ const done = () => {+ if (!(draining--)) {+ process.exit(clampedCode);+ }+ };++ const streams = [process.stdout, process.stderr];++ streams.forEach(stream => {+ // submit empty write request and wait for completion+ draining += 1;+ stream.write('', done);+ });++ done();+};++/**+ * Parse list.+ */+const list = str => str.split(/ *, */);++/**+ * Hide the cursor.+ */+const hideCursor = () => {+ process.stdout.write('\u001b[?25l');+};++/**+ * Show the cursor.+ */+const showCursor = () => {+ process.stdout.write('\u001b[?25h');+};++/**+ * Stop play()ing.+ */+const stop = () => {+ process.stdout.write('\u001b[2K');+ clearInterval(play.timer);+};++/**+ * Play the given array of strings.+ */+const play = (arr, interval) => {+ const len = arr.length;+ interval = interval || 100;+ let i = 0;++ play.timer = setInterval(() => {+ const str = arr[i++ % len];+ process.stdout.write(`\u001b[0G${str}`);+ }, interval);+};/*** Files.*/-var files = [];+let files = [];/*** Globals.*/-var globals = [];+let globals = [];/*** Requires.*/-var requires = [];+const requires = [];/*** Images.*/-var images = {+const images = {fail: path.join(__dirname, '..', 'images', 'error.png'),pass: path.join(__dirname, '..', 'images', 'ok.png')};@@ -78,7 +162,7 @@ program.option('-r, --require <name>', 'require the given module').option('-s, --slow <ms>', '"slow" test threshold in milliseconds [75]').option('-t, --timeout <ms>', 'set test-case timeout in milliseconds [2000]')- .option('-u, --ui <name>', 'specify user-interface (' + interfaceNames.join('|') + ')', 'bdd')+ .option('-u, --ui <name>', `specify user-interface (${interfaceNames.join('|')})`, 'bdd').option('-w, --watch', 'watch files for changes').option('--check-leaks', 'check for global variable leaks').option('--full-trace', 'display the full stack trace')@@ -124,12 +208,12 @@ program._name = 'mocha';program.command('init <path>').description('initialize a client-side mocha setup at <path>')- .action(function (path) {- var mkdir = require('mkdirp');+ .action(path => {+ const mkdir = require('mkdirp');mkdir.sync(path);- var css = fs.readFileSync(join(__dirname, '..', 'mocha.css'));- var js = fs.readFileSync(join(__dirname, '..', 'mocha.js'));- var tmpl = fs.readFileSync(join(__dirname, '..', 'lib/template.html'));+ const css = fs.readFileSync(join(__dirname, '..', 'mocha.css'));+ const js = fs.readFileSync(join(__dirname, '..', 'mocha.js'));+ const tmpl = fs.readFileSync(join(__dirname, '..', 'lib/template.html'));fs.writeFileSync(join(path, 'mocha.css'), css);fs.writeFileSync(join(path, 'mocha.js'), js);fs.writeFileSync(join(path, 'tests.js'), '');@@ -139,13 +223,13 @@ program// --globals-program.on('option:globals', function (val) {+program.on('option:globals', val => {globals = globals.concat(list(val));});// --reporters-program.on('option:reporters', function () {+program.on('option:reporters', () => {console.log();console.log(' dot - dot matrix');console.log(' doc - html documentation');@@ -166,10 +250,10 @@ program.on('option:reporters', function () {// --interfaces-program.on('option:interfaces', function () {+program.on('option:interfaces', () => {console.log('');- interfaceNames.forEach(function (interfaceName) {- console.log(' ' + interfaceName);+ interfaceNames.forEach(interfaceName => {+ console.log(` ${interfaceName}`);});console.log('');process.exit();@@ -179,8 +263,8 @@ program.on('option:interfaces', function () {module.paths.push(cwd, join(cwd, 'node_modules'));-program.on('option:require', function (mod) {- var abs = exists(mod) || exists(mod + '.js');+program.on('option:require', mod => {+ const abs = exists(mod) || exists(`${mod}.js`);if (abs) {mod = resolve(mod);}@@ -202,12 +286,12 @@ Error.stackTraceLimit = Infinity; // TODO: config// reporter options-var reporterOptions = {};+const reporterOptions = {};if (program.reporterOptions !== undefined) {- program.reporterOptions.split(',').forEach(function (opt) {- var L = opt.split('=');+ program.reporterOptions.split(',').forEach(opt => {+ const L = opt.split('=');if (L.length > 2 || L.length === 0) {- throw new Error("invalid reporter option '" + opt + "'");+ throw new Error(`invalid reporter option '${opt}'`);} else if (L.length === 2) {reporterOptions[L[0]] = L[1];} else {@@ -222,14 +306,14 @@ mocha.reporter(program.reporter, reporterOptions);// load reporter-var Reporter = null;+let Reporter = null;try {- Reporter = require('../lib/reporters/' + program.reporter);+ Reporter = require(`../lib/reporters/${program.reporter}`);} catch (err) {try {Reporter = require(program.reporter);} catch (err2) {- throw new Error('reporter "' + program.reporter + '" does not exist');+ throw new Error(`reporter "${program.reporter}" does not exist`);}}@@ -354,14 +438,14 @@ if (program.forbidPending) mocha.forbidPending();// custom compiler supportif (program.compilers.length > 0) {- require('util').deprecate(function () {},+ require('util').deprecate(() => {},'"--compilers" will be removed in a future version of Mocha; see https://git.io/vdcSr for more info')();}-var extensions = ['js'];-program.compilers.forEach(function (c) {- var idx = c.indexOf(':');- var ext = c.slice(0, idx);- var mod = c.slice(idx + 1);+const extensions = ['js'];+program.compilers.forEach(c => {+ const idx = c.indexOf(':');+ const ext = c.slice(0, idx);+ let mod = c.slice(idx + 1);if (mod[0] === '.') {mod = join(process.cwd(), mod);@@ -373,7 +457,7 @@ program.compilers.forEach(function (c) {// requires-requires.forEach(function (mod) {+requires.forEach(mod => {require(mod);});@@ -383,7 +467,7 @@ mocha.ui(program.ui);// args-var args = program.args;+const args = program.args;// default files to test/*.{js,coffee}@@ -391,13 +475,13 @@ if (!args.length) {args.push('test');}-args.forEach(function (arg) {- var newFiles;+args.forEach(arg => {+ let newFiles;try {newFiles = utils.lookupFiles(arg, extensions, program.recursive);} catch (err) {if (err.message.indexOf('cannot resolve path') === 0) {- console.error('Warning: Could not find any test files matching pattern: ' + arg);+ console.error(`Warning: Could not find any test files matching pattern: ${arg}`);return;}@@ -414,9 +498,7 @@ if (!files.length) {// resolve-files = files.map(function (path) {- return resolve(path);-});+files = files.map(path => resolve(path));if (program.sort) {files.sort();@@ -424,28 +506,28 @@ if (program.sort) {// --watch-var runner;-var loadAndRun;-var purge;-var rerun;+let runner;+let loadAndRun;+let purge;+let rerun;if (program.watch) {console.log();hideCursor();- process.on('SIGINT', function () {+ process.on('SIGINT', () => {showCursor();console.log('\n');process.exit(130);});- var watchFiles = utils.files(cwd, [ 'js' ].concat(program.watchExtensions));- var runAgain = false;+ const watchFiles = utils.files(cwd, [ 'js' ].concat(program.watchExtensions));+ let runAgain = false;- loadAndRun = function loadAndRun () {+ loadAndRun = () => {try {mocha.files = files;runAgain = false;- runner = mocha.run(function () {+ runner = mocha.run(() => {runner = null;if (runAgain) {rerun();@@ -456,15 +538,15 @@ if (program.watch) {}};- purge = function purge () {- watchFiles.forEach(function (file) {+ purge = () => {+ watchFiles.forEach(file => {delete require.cache[file];});};loadAndRun();- rerun = function rerun () {+ rerun = () => {purge();stop();if (!program.grep) {@@ -476,7 +558,7 @@ if (program.watch) {loadAndRun();};- utils.watch(watchFiles, function () {+ utils.watch(watchFiles, () => {runAgain = true;if (runner) {runner.abort();@@ -491,41 +573,7 @@ if (program.watch) {runner = mocha.run(program.exit ? exit : exitLater);}-function exitLater (code) {- process.on('exit', function () {- process.exit(Math.min(code, 255));- });-}--function exit (code) {- var clampedCode = Math.min(code, 255);-- // Eagerly set the process's exit code in case stream.write doesn't- // execute its callback before the process terminates.- process.exitCode = clampedCode;-- // flush output for Node.js Windows pipe bug- // https://github.com/joyent/node/issues/6247 is just one bug example- // https://github.com/visionmedia/mocha/issues/333 has a good discussion- function done () {- if (!(draining--)) {- process.exit(clampedCode);- }- }-- var draining = 0;- var streams = [process.stdout, process.stderr];-- streams.forEach(function (stream) {- // submit empty write request and wait for completion- draining += 1;- stream.write('', done);- });-- done();-}--process.on('SIGINT', function () {+process.on('SIGINT', () => {runner.abort();// This is a hack:@@ -533,51 +581,3 @@ process.on('SIGINT', function () {// The amount of failures will be emitted as error code laterrunner.failures = 130;});--/**- * Parse list.- */--function list (str) {- return str.split(/ *, */);-}--/**- * Hide the cursor.- */--function hideCursor () {- process.stdout.write('\u001b[?25l');-}--/**- * Show the cursor.- */--function showCursor () {- process.stdout.write('\u001b[?25h');-}--/**- * Stop play()ing.- */--function stop () {- process.stdout.write('\u001b[2K');- clearInterval(play.timer);-}--/**- * Play the given array of strings.- */--function play (arr, interval) {- var len = arr.length;- interval = interval || 100;- var i = 0;-- play.timer = setInterval(function () {- var str = arr[i++ % len];- process.stdout.write('\u001b[0G' + str);- }, interval);-}diff --git a/bin/mocha b/bin/mochaindex d632fbc3..38d09fb8 100755--- a/bin/mocha+++ b/bin/mocha@@ -7,17 +7,17 @@* when found, before invoking the "real" _mocha(1) executable.*/-var spawn = require('child_process').spawn;-var path = require('path');-var getOptions = require('./options');-var args = [path.join(__dirname, '_mocha')];+const spawn = require('child_process').spawn;+const path = require('path');+const getOptions = require('./options');+const args = [path.join(__dirname, '_mocha')];// Load mocha.opts into process.argv// Must be loaded here to handle node-specific optionsgetOptions();-process.argv.slice(2).forEach(function (arg) {- var flag = arg.split('=')[0];+process.argv.slice(2).forEach(arg => {+ const flag = arg.split('=')[0];switch (flag) {case '-d':@@ -69,9 +69,11 @@ process.argv.slice(2).forEach(function (arg) {}});-var proc = spawn(process.execPath, args, { stdio: 'inherit' });-proc.on('exit', function (code, signal) {- process.on('exit', function () {+const proc = spawn(process.execPath, args, {+ stdio: 'inherit'+});+proc.on('exit', (code, signal) => {+ process.on('exit', () => {if (signal) {process.kill(process.pid, signal);} else {@@ -81,7 +83,7 @@ proc.on('exit', function (code, signal) {});// terminate children.-process.on('SIGINT', function () {+process.on('SIGINT', () => {proc.kill('SIGINT'); // calls runner.abort()proc.kill('SIGTERM'); // if that didn't work, we're probably in an infinite loop, so make it die.});diff --git a/bin/options.js b/bin/options.jsindex 7ee0eca8..ac835bf8 100644--- a/bin/options.js+++ b/bin/options.js@@ -4,7 +4,7 @@* Dependencies.*/-var fs = require('fs');+const fs = require('fs');/*** Export `getOptions`.@@ -21,18 +21,16 @@ function getOptions () {return;}- var optsPath = process.argv.indexOf('--opts') === -1+ const optsPath = process.argv.indexOf('--opts') === -1? 'test/mocha.opts': process.argv[process.argv.indexOf('--opts') + 1];try {- var opts = fs.readFileSync(optsPath, 'utf8')+ const opts = fs.readFileSync(optsPath, 'utf8').replace(/\\\s/g, '%20').split(/\s/).filter(Boolean)- .map(function (value) {- return value.replace(/%20/g, ' ');- });+ .map(value => value.replace(/%20/g, ' '));process.argv = process.argv.slice(0, 2)diff --git a/docs/.eslintrc.yaml b/docs/.eslintrc.ymlsimilarity index 100%rename from docs/.eslintrc.yamlrename to docs/.eslintrc.ymldiff --git a/karma.conf.js b/karma.conf.jsindex cf07b0b9..c4d752d4 100644--- a/karma.conf.js+++ b/karma.conf.js@@ -1,11 +1,11 @@'use strict';-var fs = require('fs');-var path = require('path');-var mkdirp = require('mkdirp');-var baseBundleDirpath = path.join(__dirname, '.karma');+const fs = require('fs');+const path = require('path');+const mkdirp = require('mkdirp');+const baseBundleDirpath = path.join(__dirname, '.karma');-var browserPlatformPairs = {+const browserPlatformPairs = {'chrome@latest': 'Windows 8','MicrosoftEdge@latest': 'Windows 10','internet [email protected]': 'Windows 8.1',@@ -15,9 +15,9 @@ var browserPlatformPairs = {'safari@latest': 'OS X 10.12'};-module.exports = function (config) {- var bundleDirpath;- var cfg = {+module.exports = config => {+ let bundleDirpath;+ const cfg = {frameworks: ['browserify','expect',@@ -40,11 +40,11 @@ module.exports = function (config) {.ignore('path').ignore('supports-color').require(path.join(__dirname, 'node_modules', 'buffer'), {expose: 'buffer'})- .on('bundled', function (err, content) {+ .on('bundled', (err, content) => {if (!err && bundleDirpath) {// write bundle to directory for debuggingfs.writeFileSync(path.join(bundleDirpath,- 'bundle.' + Date.now() + '.js'), content);+ `bundle.${Date.now()}.js`), content);}});}@@ -75,8 +75,8 @@ module.exports = function (config) {// TO RUN LOCALLY, execute:// `CI=1 SAUCE_USERNAME=<user> SAUCE_ACCESS_KEY=<key> BROWSER=<browser> PLATFORM=<platform> make test-browser`- var env = process.env;- var sauceConfig;+ const env = process.env;+ let sauceConfig;if (env.CI) {console.error('CI mode enabled');@@ -86,8 +86,7 @@ module.exports = function (config) {if (env.SAUCE_USERNAME && env.SAUCE_ACCESS_KEY) {// correlate build/tunnel with TravissauceConfig = {- build: 'TRAVIS #' + env.TRAVIS_BUILD_NUMBER +- ' (' + env.TRAVIS_BUILD_ID + ')',+ build: `TRAVIS #${env.TRAVIS_BUILD_NUMBER} (${env.TRAVIS_BUILD_ID})`,tunnelIdentifier: env.TRAVIS_JOB_NUMBER,startConnect: false};@@ -103,7 +102,7 @@ module.exports = function (config) {bundleDirpath = path.join(baseBundleDirpath, 'local');// don't need to run sauce from appveyor b/c travis does it.if (env.SAUCE_USERNAME || env.SAUCE_ACCESS_KEY) {- var id = require('os').hostname() + ' (' + Date.now() + ')';+ const id = `${require('os').hostname()} (${Date.now()})`;sauceConfig = {build: id,tunnelIdentifier: id,@@ -128,14 +127,14 @@ module.exports = function (config) {// tests. since you can only load one at a time, each must be run separately.// each has its own set of acceptance tests and a fixture.// the "bdd" fixture is used by default.- var ui = env.MOCHA_UI;+ const ui = env.MOCHA_UI;if (ui) {if (cfg.sauceLabs) {- cfg.sauceLabs.testName = 'Interface "' + ui + '" integration tests';+ cfg.sauceLabs.testName = `Interface "${ui}" integration tests`;}cfg.files = [- 'test/browser-fixtures/' + ui + '.fixture.js',- 'test/interfaces/' + ui + '.spec.js'+ `test/browser-fixtures/${ui}.fixture.js`,+ `test/interfaces/${ui}.spec.js`];} else if (cfg.sauceLabs) {cfg.sauceLabs.testName = 'Unit Tests';@@ -146,13 +145,13 @@ module.exports = function (config) {function addSauceTests (cfg) {cfg.reporters.push('saucelabs');- var browsers = Object.keys(browserPlatformPairs);+ const browsers = Object.keys(browserPlatformPairs);cfg.browsers = cfg.browsers.concat(browsers);- cfg.customLaunchers = browsers.reduce(function (acc, browser) {- var platform = browserPlatformPairs[browser];- var browserParts = browser.split('@');- var browserName = browserParts[0];- var version = browserParts[1];+ cfg.customLaunchers = browsers.reduce((acc, browser) => {+ const platform = browserPlatformPairs[browser];+ const browserParts = browser.split('@');+ const browserName = browserParts[0];+ const version = browserParts[1];acc[browser] = {base: 'SauceLabs',browserName: browserName,diff --git a/lib/browser/.eslintrc.yaml b/lib/browser/.eslintrc.ymlsimilarity index 100%rename from lib/browser/.eslintrc.yamlrename to lib/browser/.eslintrc.ymldiff --git a/scripts/.eslintrc.yaml b/scripts/.eslintrc.yamldeleted file mode 100644index 1ddd632e..00000000--- a/scripts/.eslintrc.yaml+++ /dev/null@@ -1,2 +0,0 @@-parserOptions:- ecmaVersion: 2017diff --git a/scripts/.eslintrc.yml b/scripts/.eslintrc.ymlnew file mode 100644index 00000000..36e0a779--- /dev/null+++ b/scripts/.eslintrc.yml@@ -0,0 +1,3 @@+env:+ es6: true+ browser: falsediff --git a/scripts/dedefine.js b/scripts/dedefine.jsindex 850fde38..cd21b623 100644--- a/scripts/dedefine.js+++ b/scripts/dedefine.js@@ -5,8 +5,8 @@* dependencies in our Browserify bundle.*/-var through = require('through2');-var defineRx = /typeof\s+define\s*===?\s*['"]function['"]\s*&&\s*(?:define\.amd|typeof\s+define\.amd\s*===?\s*['"]object['"]\s*&&\s*define\.amd)/g;+const through = require('through2');+const defineRx = /typeof\s+define\s*===?\s*['"]function['"]\s*&&\s*(?:define\.amd|typeof\s+define\.amd\s*===?\s*['"]object['"]\s*&&\s*define\.amd)/g;function createStream () {return through.obj(function (chunk, enc, next) {this.push(String(chunk)@@ -15,10 +15,10 @@ function createStream () {});}-module.exports = function (b) {- function wrap () {+module.exports = b => {+ const wrap = () => {b.pipeline.get('wrap').push(createStream());- }+ };b.on('reset', wrap);wrap();diff --git a/test/.eslintrc.yaml b/test/.eslintrc.ymlsimilarity index 100%rename from test/.eslintrc.yamlrename to test/.eslintrc.yml