Skip to content
Permalink
Browse files

chore(build): replaces legacy mocha.opts with new .mocharc.json

  • Loading branch information...
boneskull authored and raymondfeng committed Jan 24, 2019
1 parent 861256c commit c3d800700b253e97316fd0871641ea80fcb457f3
@@ -11,8 +11,8 @@
"program": "${workspaceRoot}/packages/build/node_modules/.bin/_mocha",
"cwd": "${workspaceRoot}",
"args": [
"--opts",
"${workspaceRoot}/test/mocha.opts",
"--config",
"${workspaceRoot}/packages/build/config/.mocharc.json",
"packages/*/dist/__tests__/**/*.js",
"-t",
"0"

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -23,16 +23,22 @@ function run(argv, options) {
const mochaOpts = argv.slice(2).map(a => a.replace(/\bDIST\b/g, dist));

const setMochaOpts =
!utils.isOptionSet(mochaOpts, '--opts') &&
!utils.mochaOptsFileProjectExists();
!utils.isOptionSet(
mochaOpts,
'--config', // mocha 6.x
'--opts', // legacy
'--package', // mocha 6.x
'--no-config', // mocha 6.x
) && !utils.mochaConfiguredForProject();

// Add default options
// Keep it backward compatible as dryRun
if (typeof options === 'boolean') options = {dryRun: options};
options = options || {};
if (setMochaOpts) {
const mochaOptsFile = utils.getConfigFile('mocha.opts');
mochaOpts.unshift('--opts', mochaOptsFile);
// Use the default `.mocharc.json` from `@loopback/build`
const mochaOptsFile = utils.getConfigFile('.mocharc.json');
mochaOpts.unshift('--config', mochaOptsFile);
}

const allowConsoleLogsIx = mochaOpts.indexOf('--allow-console-logs');
@@ -214,9 +214,18 @@ function isOptionSet(opts, ...optionNames) {
);
}

function mochaOptsFileProjectExists() {
const configFile = path.join(getPackageDir(), 'test/mocha.opts');
return fs.existsSync(configFile);
function mochaConfiguredForProject() {
const configFiles = [
'.mocharc.js',
'.mocharc.json',
'.mocharc.yaml',
'.mocharc.yml',
'test/mocha.opts',
];
return configFiles.some(f => {
const configFile = path.join(getPackageDir(), f);
return fs.existsSync(configFile);
});
}

exports.getCompilationTarget = getCompilationTarget;
@@ -228,4 +237,4 @@ exports.resolveCLI = resolveCLI;
exports.runCLI = runCLI;
exports.runShell = runShell;
exports.isOptionSet = isOptionSet;
exports.mochaOptsFileProjectExists = mochaOptsFileProjectExists;
exports.mochaConfiguredForProject = mochaConfiguredForProject;
@@ -0,0 +1,7 @@
{
"require": "source-map-support/register",
"recursive": true,
"exit": true,
"reporter": "dot"
}

This file was deleted.

Oops, something went wrong.
@@ -365,7 +365,7 @@ describe('mocha', function() {

function cleanup() {
var run = require('../../bin/run-clean');
run(['node', 'bin/run-clean', 'test/mocha.opts']);
run(['node', 'bin/run-clean', '.mocharc.json']);
}

beforeEach(() => {
@@ -378,42 +378,50 @@ describe('mocha', function() {
process.chdir(cwd);
});

it('loads built-in mocha.opts file', () => {
it('loads built-in .mocharc.json file', () => {
var run = require('../../bin/run-mocha');
var command = run(['node', 'bin/run-mocha', '"dist/__tests__"'], true);
const builtInMochaOptsFile = path.join(
__dirname,
'../../config/mocha.opts',
'../../config/.mocharc.json',
);
assert(
command.indexOf(builtInMochaOptsFile) !== -1,
'--opts should be set by default',
'--config should be set by default',
);
});

it('honors --opts option', () => {
it('honors --config option', () => {
var run = require('../../bin/run-mocha');
var command = run(
['node', 'bin/run-mocha', '--opts custom/mocha.opts', '"dist/__tests__"'],
[
'node',
'bin/run-mocha',
'--config custom/.mocharc.json',
'"dist/__tests__"',
],
true,
);
assert(
command.indexOf('--opts custom/mocha.opts') !== -1,
'--opts custom/mocha.opts should be honored',
command.indexOf('--config custom/.mocharc.json') !== -1,
'--config custom/.mocharc.json should be honored',
);
});

it('loads mocha.opts specific project file', () => {
it('loads .mocharc.json specific project file', () => {
var run = require('../../bin/run-mocha');
const buitInMochaOptsPath = path.join(__dirname, '../../config/mocha.opts');
const destPath = path.join(__dirname, './fixtures/test/mocha.opts');
const buitInMochaOptsPath = path.join(
__dirname,
'../../config/.mocharc.json',
);
const destPath = path.join(__dirname, './fixtures/.mocharc.json');

fs.copyFileSync(buitInMochaOptsPath, destPath);

var command = run(['node', 'bin/run-mocha', '"dist/__tests__"'], true);
assert(
command.indexOf('--opts') === -1,
'should skip built-in mocha.opts file when specific project file exist',
command.indexOf('--config') === -1,
'should skip built-in .mocharc.json file when specific project file exist',
);
});
});
@@ -0,0 +1,4 @@
{
"recursive": true,
"require": "source-map-support/register"
}

This file was deleted.

Oops, something went wrong.
@@ -255,7 +255,7 @@ module.exports = class ProjectGenerator extends BaseGenerator {
}

if (!this.projectInfo.mocha) {
this.fs.delete(this.destinationPath('test/mocha.opts'));
this.fs.delete(this.destinationPath('.mocharc.json'));
}

if (!this.projectInfo.vscode) {

0 comments on commit c3d8007

Please sign in to comment.
You can’t perform that action at this time.