Skip to content

Commit

Permalink
code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
khoaHyh committed Jun 5, 2024
1 parent 1a9f155 commit ff58b19
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
11 changes: 10 additions & 1 deletion lib/cli/collect-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,18 @@ module.exports = ({
* @property {string[]} file - List of additional files to include
* @property {boolean} recursive - Find files recursively
* @property {boolean} sort - Sort test files
* @typedef {Object} UnmatchedFile - Diagnostic object containing unmatched files
*/

/**
* Diagnostic object containing unmatched files
* @typedef {Object} UnmatchedFile -
* @property {string} absolutePath - A list of unmatched files derived from the file arguments passed in.
* @property {string} pattern - A list of unmatched files derived from the file arguments passed in.
*
*/

/**
* Response object containing a list of files to test and unmatched files.
* @typedef {Object} FileCollectionResponse
* @property {string[]} files - A list of files to test
* @property {UnmatchedFile[]} unmatchedFiles - A list of unmatched files derived from the file arguments passed in.
Expand Down
52 changes: 29 additions & 23 deletions lib/cli/run-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {format} = require('util');
const {createInvalidLegacyPluginError} = require('../errors');
const {requireOrImport} = require('../nodejs/esm-utils');
const PluginLoader = require('../plugin-loader');
const {UnmatchedFile} = require('./collect-files');

/**
* Exits Mocha when tests + code under test has finished execution (default)
Expand Down Expand Up @@ -107,6 +108,32 @@ exports.handleRequires = async (requires = [], {ignoredPlugins = []} = {}) => {
return plugins;
};

/**
* Logs errors and exits the app if unmatched files exist
* @param {Mocha} mocha - Mocha instance
* @param {UnmatchedFile} unmatchedFiles - object containing unmatched file paths
* @returns {Promise<Runner>}
* @private
*/
const handleUnmatchedFiles = (mocha, unmatchedFiles) => {
if (unmatchedFiles.length === 0) {
return;
}

unmatchedFiles.forEach(({pattern, absolutePath}) => {
console.error(
ansi.yellow(
`Warning: Cannot find any files matching pattern "${pattern}" at the absolute path "${absolutePath}"`
)
);
});
console.log(
'No test file(s) found with the given pattern, exiting with code 1'
);

return mocha.run(exitMocha(1));
};

/**
* Collect and load test files, then run mocha instance.
* @param {Mocha} mocha - Mocha instance
Expand All @@ -121,18 +148,7 @@ const singleRun = async (mocha, {exit}, fileCollectParams) => {
const fileCollectionObj = collectFiles(fileCollectParams);

if (fileCollectionObj.unmatchedFiles.length > 0) {
fileCollectionObj.unmatchedFiles.forEach(({pattern, absolutePath}) => {
console.error(
ansi.yellow(
`Warning: Cannot find any files matching pattern "${pattern}" at the absolute path "${absolutePath}"`
)
);
});
console.log(
'No test file(s) found with the given pattern, exiting with code 1'
);

return mocha.run(exitMocha(1));
return handleUnmatchedFiles(fileCollectionObj.unmatchedFiles);
}

debug('single run with %d file(s)', fileCollectionObj.files.length);
Expand Down Expand Up @@ -160,17 +176,7 @@ const parallelRun = async (mocha, options, fileCollectParams) => {
const fileCollectionObj = collectFiles(fileCollectParams);

if (fileCollectionObj.unmatchedFiles.length > 0) {
fileCollectionObj.unmatchedFiles.forEach(({pattern, absolutePath}) => {
console.error(
ansi.yellow(
`Warning: Cannot find any files matching pattern "${pattern}" at the absolute path "${absolutePath}"`
)
);
});
console.log(
'No test file(s) found with the given pattern, exiting with code 1'
);
return mocha.run(exitMocha(1));
return handleUnmatchedFiles(fileCollectionObj.unmatchedFiles);
}

debug(
Expand Down
4 changes: 2 additions & 2 deletions test/integration/options/file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('--file', function () {
});
});

it('should log a warning if a nonexistent file is specified', function (done) {
it('should log a warning if a nonexistent file with an unknown extension is specified', function (done) {
const nonexistentTestFileArg = 'nonexistent.test.ts';
runMocha(
nonexistentTestFileArg,
Expand All @@ -101,7 +101,7 @@ describe('--file', function () {
);
});

it('should provide warning for nonexistent cjs file extensions', function (done) {
it('should provide warning for nonexistent js file extensions', function (done) {
const nonexistentCjsArg = 'nonexistent.test.js';

runMocha(
Expand Down

0 comments on commit ff58b19

Please sign in to comment.