Skip to content

Commit 097ab71

Browse files
boneskullraymondfeng
authored andcommitted
chore(build): replaces legacy mocha.opts with new .mocharc.json
1 parent c73e8d0 commit 097ab71

File tree

10 files changed

+80
-51
lines changed

10 files changed

+80
-51
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"program": "${workspaceRoot}/packages/build/node_modules/.bin/_mocha",
1212
"cwd": "${workspaceRoot}",
1313
"args": [
14-
"--opts",
15-
"${workspaceRoot}/test/mocha.opts",
14+
"--config",
15+
"${workspaceRoot}/packages/build/config/.mocharc.json",
1616
"packages/*/dist/__tests__/**/*.js",
1717
"-t",
1818
"0"

docs/site/todo-tutorial-scaffolding.md

Lines changed: 22 additions & 21 deletions
Large diffs are not rendered by default.

packages/build/bin/run-mocha.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,22 @@ function run(argv, options) {
2323
const mochaOpts = argv.slice(2).map(a => a.replace(/\bDIST\b/g, dist));
2424

2525
const setMochaOpts =
26-
!utils.isOptionSet(mochaOpts, '--opts') &&
27-
!utils.mochaOptsFileProjectExists();
26+
!utils.isOptionSet(
27+
mochaOpts,
28+
'--config', // mocha 6.x
29+
'--opts', // legacy
30+
'--package', // mocha 6.x
31+
'--no-config', // mocha 6.x
32+
) && !utils.mochaConfiguredForProject();
2833

2934
// Add default options
3035
// Keep it backward compatible as dryRun
3136
if (typeof options === 'boolean') options = {dryRun: options};
3237
options = options || {};
3338
if (setMochaOpts) {
34-
const mochaOptsFile = utils.getConfigFile('mocha.opts');
35-
mochaOpts.unshift('--opts', mochaOptsFile);
39+
// Use the default `.mocharc.json` from `@loopback/build`
40+
const mochaOptsFile = utils.getConfigFile('.mocharc.json');
41+
mochaOpts.unshift('--config', mochaOptsFile);
3642
}
3743

3844
const allowConsoleLogsIx = mochaOpts.indexOf('--allow-console-logs');

packages/build/bin/utils.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,18 @@ function isOptionSet(opts, ...optionNames) {
214214
);
215215
}
216216

217-
function mochaOptsFileProjectExists() {
218-
const configFile = path.join(getPackageDir(), 'test/mocha.opts');
219-
return fs.existsSync(configFile);
217+
function mochaConfiguredForProject() {
218+
const configFiles = [
219+
'.mocharc.js',
220+
'.mocharc.json',
221+
'.mocharc.yaml',
222+
'.mocharc.yml',
223+
'mocha.opts',
224+
];
225+
return configFiles.some(f => {
226+
const configFile = path.join(getPackageDir(), f);
227+
return fs.existsSync(configFile);
228+
});
220229
}
221230

222231
exports.getCompilationTarget = getCompilationTarget;
@@ -228,4 +237,4 @@ exports.resolveCLI = resolveCLI;
228237
exports.runCLI = runCLI;
229238
exports.runShell = runShell;
230239
exports.isOptionSet = isOptionSet;
231-
exports.mochaOptsFileProjectExists = mochaOptsFileProjectExists;
240+
exports.mochaConfiguredForProject = mochaConfiguredForProject;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"require": "source-map-support/register",
3+
"recursive": true,
4+
"exit": true,
5+
"reporter": "dot"
6+
}
7+

packages/build/config/mocha.opts

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/build/test/integration/scripts.integration.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ describe('mocha', function() {
365365

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

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

381-
it('loads built-in mocha.opts file', () => {
381+
it('loads built-in .mocharc.json file', () => {
382382
var run = require('../../bin/run-mocha');
383383
var command = run(['node', 'bin/run-mocha', '"dist/__tests__"'], true);
384384
const builtInMochaOptsFile = path.join(
385385
__dirname,
386-
'../../config/mocha.opts',
386+
'../../config/.mocharc.json',
387387
);
388388
assert(
389389
command.indexOf(builtInMochaOptsFile) !== -1,
390-
'--opts should be set by default',
390+
'--config should be set by default',
391391
);
392392
});
393393

394-
it('honors --opts option', () => {
394+
it('honors --config option', () => {
395395
var run = require('../../bin/run-mocha');
396396
var command = run(
397-
['node', 'bin/run-mocha', '--opts custom/mocha.opts', '"dist/__tests__"'],
397+
[
398+
'node',
399+
'bin/run-mocha',
400+
'--config custom/.mocharc.json',
401+
'"dist/__tests__"',
402+
],
398403
true,
399404
);
400405
assert(
401-
command.indexOf('--opts custom/mocha.opts') !== -1,
402-
'--opts custom/mocha.opts should be honored',
406+
command.indexOf('--config custom/.mocharc.json') !== -1,
407+
'--config custom/.mocharc.json should be honored',
403408
);
404409
});
405410

406-
it('loads mocha.opts specific project file', () => {
411+
it('loads .mocharc.json specific project file', () => {
407412
var run = require('../../bin/run-mocha');
408-
const buitInMochaOptsPath = path.join(__dirname, '../../config/mocha.opts');
409-
const destPath = path.join(__dirname, './fixtures/test/mocha.opts');
413+
const buitInMochaOptsPath = path.join(
414+
__dirname,
415+
'../../config/.mocharc.json',
416+
);
417+
const destPath = path.join(__dirname, './fixtures/.mocharc.json');
410418

411419
fs.copyFileSync(buitInMochaOptsPath, destPath);
412420

413421
var command = run(['node', 'bin/run-mocha', '"dist/__tests__"'], true);
414422
assert(
415-
command.indexOf('--opts') === -1,
416-
'should skip built-in mocha.opts file when specific project file exist',
423+
command.indexOf('--config') === -1,
424+
'should skip built-in .mocharc.json file when specific project file exist',
417425
);
418426
});
419427
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"recursive": true,
3+
"require": "source-map-support/register"
4+
}

packages/cli/generators/project/templates/test/mocha.opts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/cli/lib/project-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ module.exports = class ProjectGenerator extends BaseGenerator {
255255
}
256256

257257
if (!this.projectInfo.mocha) {
258-
this.fs.delete(this.destinationPath('test/mocha.opts'));
258+
this.fs.delete(this.destinationPath('.mocharc.json'));
259259
}
260260

261261
if (!this.projectInfo.vscode) {

0 commit comments

Comments
 (0)