Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node.js-style --require CLI argument #296

Merged
merged 1 commit into from
Dec 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Api(files, options) {
EventEmitter.call(this);

this.options = options || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already fixed this in 3fdf44d, sorry. Can you rebase to fix the merge conflict?


this.rejectionCount = 0;
this.exceptionCount = 0;
this.passCount = 0;
Expand Down
10 changes: 8 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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)',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be updated in the readme.md too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

'',
'Examples',
' ava',
Expand All @@ -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'
Expand All @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion lib/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think opts.require is already guaranteed to be an array, so this check is moot.

https://github.com/sindresorhus/ava/pull/296/files#diff-2cce40143051e25f811b56c79d619bf5R73

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. In the API tests, new Api() is called without the second parameter, which means it ends up being an empty Object, without a "require" property. Should I change the fallback default value in api.js to include an empty "require" array? e.g. this.options = options || { require: [] }; ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, never mind then. I read it wrong.

require(resolveCwd(moduleId));
});

var sourceMapCache = Object.create(null);

var sourceMapSupport = require('source-map-support');
Expand All @@ -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');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions test/fixture/install-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.foo = 'bar';
3 changes: 3 additions & 0 deletions test/fixture/validate-installed-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import test from '../../';

test(t => t.is(global.foo, 'bar'));