From 6081389d00221217d39115601cc4dfa485618f90 Mon Sep 17 00:00:00 2001 From: Ron Waldon Date: Thu, 3 Dec 2015 16:20:16 +1100 Subject: [PATCH] Node.js-style --require CLI argument --- api.js | 1 + cli.js | 10 ++++++++-- lib/babel.js | 6 +++++- package.json | 1 + readme.md | 1 + test/api.js | 14 ++++++++++++++ test/fixture/install-global.js | 1 + test/fixture/validate-installed-global.js | 3 +++ 8 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test/fixture/install-global.js create mode 100644 test/fixture/validate-installed-global.js diff --git a/api.js b/api.js index 2b6c5f207..797fe2dfe 100644 --- a/api.js +++ b/api.js @@ -18,6 +18,7 @@ function Api(files, options) { EventEmitter.call(this); this.options = options || {}; + this.rejectionCount = 0; this.exceptionCount = 0; this.passCount = 0; diff --git a/cli.js b/cli.js index 87338a0ad..d72109484 100755 --- a/cli.js +++ b/cli.js @@ -17,6 +17,7 @@ if (debug.enabled) { require('time-require'); } +var arrify = require('arrify'); var meow = require('meow'); var updateNotifier = require('update-notifier'); var chalk = require('chalk'); @@ -35,6 +36,7 @@ var cli = meow([ ' --init Add AVA to your project', ' --fail-fast Stop after first test failure', ' --serial Run tests serially', + ' --require Module to preload (Can be repeated)', '', 'Examples', ' ava', @@ -46,7 +48,10 @@ var cli = meow([ 'Default patterns when no arguments:', 'test.js test-*.js test/*.js' ], { - string: ['_'], + string: [ + '_', + 'require' + ], boolean: [ 'fail-fast', 'serial' @@ -64,7 +69,8 @@ log.write(); var api = new Api(cli.input, { failFast: cli.flags.failFast, - serial: cli.flags.serial + serial: cli.flags.serial, + require: arrify(cli.flags.require) }); api.on('test', function (test) { diff --git a/lib/babel.js b/lib/babel.js index dcb7d3456..805650fea 100644 --- a/lib/babel.js +++ b/lib/babel.js @@ -16,6 +16,11 @@ if (debug.enabled) { // Bind globals first, before anything has a chance to interfere. var globals = require('./globals'); +var resolveCwd = require('resolve-cwd'); +(opts.require || []).forEach(function (moduleId) { + require(resolveCwd(moduleId)); +}); + var sourceMapCache = Object.create(null); var sourceMapSupport = require('source-map-support'); @@ -33,7 +38,6 @@ sourceMapSupport.install({ var createEspowerPlugin = require('babel-plugin-espower/create'); var requireFromString = require('require-from-string'); var loudRejection = require('loud-rejection/api')(process); -var resolveCwd = require('resolve-cwd'); var hasGenerator = require('has-generator'); var serializeError = require('serialize-error'); var send = require('./send'); diff --git a/package.json b/package.json index 71c31bb09..04c8b884a 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ ], "dependencies": { "arr-flatten": "^1.0.1", + "arrify": "^1.0.0", "ava-init": "^0.1.0", "babel-core": "^5.8.23", "babel-plugin-espower": "^1.1.0", diff --git a/readme.md b/readme.md index 92f75996e..d8ba60f7f 100644 --- a/readme.md +++ b/readme.md @@ -102,6 +102,7 @@ $ ava --help --init Add AVA to your project --fail-fast Stop after first test failure --serial Run tests serially + --require Module to preload (Can be repeated) Examples ava diff --git a/test/api.js b/test/api.js index 310997588..e4a302895 100644 --- a/test/api.js +++ b/test/api.js @@ -260,3 +260,17 @@ test('test file in node_modules is ignored', function (t) { t.true(/Couldn't find any files to test/.test(err.message)); }); }); + +test('Node.js-style --require CLI argument', function (t) { + t.plan(1); + + var api = new Api( + [path.join(__dirname, 'fixture/validate-installed-global.js')], + {require: [path.join(__dirname, 'fixture', 'install-global.js')]} + ); + + api.run() + .then(function () { + t.is(api.passCount, 1); + }); +}); diff --git a/test/fixture/install-global.js b/test/fixture/install-global.js new file mode 100644 index 000000000..55ec32fb5 --- /dev/null +++ b/test/fixture/install-global.js @@ -0,0 +1 @@ +global.foo = 'bar'; diff --git a/test/fixture/validate-installed-global.js b/test/fixture/validate-installed-global.js new file mode 100644 index 000000000..c78eb4e8d --- /dev/null +++ b/test/fixture/validate-installed-global.js @@ -0,0 +1,3 @@ +import test from '../../'; + +test(t => t.is(global.foo, 'bar'));