Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
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
2 changes: 2 additions & 0 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {loadOptions, YARGS_PARSER_CONFIG} = require('./options');
const commands = require('./commands');
const ansi = require('ansi-colors');
const {repository, homepage, version, gitter} = require('../../package.json');
const mocharc = require('../mocharc.json');

/**
* - Accepts an `Array` of arguments
Expand Down Expand Up @@ -65,6 +66,7 @@ exports.main = (argv = process.argv.slice(2)) => {
)
.parserConfiguration(YARGS_PARSER_CONFIG)
.config(args)
.default(mocharc)
.parse(args._);
};

Expand Down
13 changes: 2 additions & 11 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ const nargOpts = types.array
/**
* Wrapper around `yargs-parser` which applies our settings
* @param {string|string[]} args - Arguments to parse
* @param {Object} defaultValues - Default values of mocharc.json
* @param {...Object} configObjects - `configObjects` for yargs-parser
* @private
* @ignore
*/
const parse = (args = [], defaultValues = {}, ...configObjects) => {
const parse = (args = [], ...configObjects) => {
// save node-specific args for special handling.
// 1. when these args have a "=" they should be considered to have values
// 2. if they don't, they just boolean flags
Expand All @@ -121,7 +120,6 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
const result = yargsParser.detailed(args, {
configuration,
configObjects,
default: defaultValues,
coerce: coerceOpts,
narg: nargOpts,
alias: aliases,
Expand Down Expand Up @@ -306,14 +304,7 @@ const loadOptions = (argv = []) => {
args._ = args._.concat(optsConfig._ || []);
}

args = parse(
args._,
mocharc,
args,
rcConfig || {},
pkgConfig || {},
optsConfig || {}
);
args = parse(args._, args, rcConfig || {}, pkgConfig || {}, optsConfig || {});

// recombine positional arguments and "spec"
if (args.spec) {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/fixtures/options/extends/mocha-extends.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ui": "tdd"
}
1 change: 1 addition & 0 deletions test/integration/fixtures/options/extends/mocharc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends: 'test/integration/fixtures/options/extends/mocha-extends.json'
37 changes: 37 additions & 0 deletions test/integration/options/extends.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

var path = require('path').posix;
var helpers = require('../helpers');
var invokeMocha = helpers.invokeMocha;

describe('--extends', function() {
var fixture = {
config: path.join(
'test',
'integration',
'fixtures',
'options',
'extends',
'mocharc.yaml'
),
extends: path.join(
'test',
'integration',
'fixtures',
'options',
'extends',
'mocha-extends.json'
)
};

it('should apply inherited configuration', function(done) {
var args = ['--config', fixture.config, '--extends', fixture.extends];
invokeMocha(args, function(err, res) {
if (err) {
return done(err);
}
expect(res, 'to have passed');
done();
});
});
});
127 changes: 44 additions & 83 deletions test/node-unit/cli/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,20 @@ describe('options', function() {
});
});

it('should return an object containing positional args, defaults, and anti-reloading flags', function() {
it('should return an object containing positional args, and anti-reloading flags', function() {
expect(
loadOptions(),
'to equal',
Object.assign({}, defaults, {
_: [],
config: false,
opts: false,
package: false,
retries: '3'
})
Object.assign(
{},
{
_: [],
config: false,
opts: false,
package: false,
retries: '3'
}
)
);
});
});
Expand Down Expand Up @@ -196,7 +199,6 @@ describe('options', function() {
{
_: ['foobar.spec.js']
},
defaults,
{
config: false,
opts: false,
Expand Down Expand Up @@ -242,17 +244,20 @@ describe('options', function() {
result = loadOptions('--no-opts');
});

it('should return parsed args, default config, config file, and package.json', function() {
it('should return parsed args, config file, and package.json', function() {
expect(
result,
'to equal',
Object.assign({_: []}, defaults, {
'check-leaks': false,
config: false,
opts: false,
package: false,
retries: 3
})
Object.assign(
{_: []},
{
'check-leaks': false,
config: false,
opts: false,
package: false,
retries: 3
}
)
);
});

Expand Down Expand Up @@ -300,7 +305,6 @@ describe('options', function() {
{
_: []
},
defaults,
{
config: false,
opts: false,
Expand Down Expand Up @@ -381,7 +385,6 @@ describe('options', function() {
{
_: ['foobar.spec.js']
},
defaults,
{
config: false,
opts: false,
Expand Down Expand Up @@ -417,17 +420,20 @@ describe('options', function() {
result = loadOptions('--no-package');
});

it('should return parsed args, default config, package.json and mocha.opts', function() {
it('should return parsed args, package.json and mocha.opts', function() {
expect(
result,
'to equal',
Object.assign({_: []}, defaults, {
'check-leaks': true,
config: false,
opts: false,
package: false,
retries: '3'
})
Object.assign(
{_: []},
{
'check-leaks': true,
config: false,
opts: false,
package: false,
retries: '3'
}
)
);
});

Expand Down Expand Up @@ -466,17 +472,20 @@ describe('options', function() {
result = loadOptions('--no-config');
});

it('should return parsed args, default config, package.json and mocha.opts', function() {
it('should return parsed args, package.json and mocha.opts', function() {
expect(
result,
'to equal',
Object.assign({_: ['foobar.spec.js']}, defaults, {
'check-leaks': true,
config: false,
opts: false,
package: false,
retries: '3'
})
Object.assign(
{_: ['foobar.spec.js']},
{
'check-leaks': true,
config: false,
opts: false,
package: false,
retries: '3'
}
)
);
});

Expand Down Expand Up @@ -719,54 +728,6 @@ describe('options', function() {
});
});

describe('"extension" handling', function() {
describe('when user supplies "extension" option', function() {
let result;

beforeEach(function() {
readFileSync = sandbox.stub();
readFileSync.onFirstCall().throws();
findConfig = sandbox.stub().returns('/some/.mocharc.json');
loadConfig = sandbox.stub().returns({extension: ['tsx']});
findupSync = sandbox.stub();
loadOptions = proxyLoadOptions({
readFileSync,
findConfig,
loadConfig,
findupSync
});
result = loadOptions(['--extension', 'ts']);
});

it('should not concatenate the default value', function() {
expect(result, 'to have property', 'extension', ['ts', 'tsx']);
});
});

describe('when user does not supply "extension" option', function() {
let result;

beforeEach(function() {
readFileSync = sandbox.stub();
readFileSync.onFirstCall().throws();
findConfig = sandbox.stub().returns('/some/.mocharc.json');
loadConfig = sandbox.stub().returns({});
findupSync = sandbox.stub();
loadOptions = proxyLoadOptions({
readFileSync,
findConfig,
loadConfig,
findupSync
});
result = loadOptions();
});

it('should retain the default', function() {
expect(result, 'to have property', 'extension', ['js']);
});
});
});

describe('"spec" handling', function() {
describe('when user supplies "spec" in config and positional arguments', function() {
let result;
Expand Down