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

Create Module not found error #4826

Closed
wants to merge 1 commit into from
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
11 changes: 10 additions & 1 deletion lib/cli/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const fs = require('fs');
const path = require('path');
const debug = require('debug')('mocha:cli:config');
const findUp = require('find-up');
const {createUnparsableFileError} = require('../errors');
const {
createUnparsableFileError,
createModuleNotFoundError
} = require('../errors');
const utils = require('../utils');

/**
Expand Down Expand Up @@ -82,6 +85,12 @@ exports.loadConfig = filepath => {
config = parsers.json(filepath);
}
} catch (err) {
if (isModuleNotFoundError(err)) {
throw createModuleNotFoundError(
`${filepath} is attempting to import a module that does not exist: ${err}`,
filepath
);
}
Copy link
Contributor

@juergba juergba Feb 9, 2022

Choose a reason for hiding this comment

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

It's not clear to me how you are going to distinguish module not found errors caused by invalid path (see parsers) and by invalid imports.

throw createUnparsableFileError(
`Unable to read/parse ${filepath}: ${err}`,
filepath
Expand Down
29 changes: 28 additions & 1 deletion lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,14 @@ var constants = {
* @constant
* @default
*/
UNPARSABLE_FILE: 'ERR_MOCHA_UNPARSABLE_FILE'
UNPARSABLE_FILE: 'ERR_MOCHA_UNPARSABLE_FILE',

/**
* File attempts to import non-existent module
* @constant
* @default
*/
MODULE_NOT_FOUND: 'ERR_MOCHA_MODULE_NOT_FOUND'
};

/**
Expand Down Expand Up @@ -516,6 +523,25 @@ function createUnparsableFileError(message, filename) {
return err;
}

/**
* Creates an error object to be thrown when file attempts to import module that does not exist.
* @public
* @static
* @param {string} message - Error message to be displayed.
* @param {string} importerFilename - File name of importing module
* @param {string} importedFilename - File name of imported module
* @returns {Error} Error with code {@link constants.MODULE_NOT_FOUND}
*/
function createModuleNotFoundError(
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need a new Mocha error code, we can't wrap each and every Node error in a new Mocha error object. We just have to rethrow Node's error somehow or include Node's error message in Mocha's UnparsableFileError object.

message,
importerFilename,
importedFilename
) {
var err = new Error(message);
err.code = constants.MODULE_NOT_FOUND;
return err;
}

/**
* Returns `true` if an error came out of Mocha.
* _Can suffer from false negatives, but not false positives._
Expand Down Expand Up @@ -547,6 +573,7 @@ module.exports = {
createNoFilesMatchPatternError,
createTimeoutError,
createUnparsableFileError,
createModuleNotFoundError,
createUnsupportedError,
deprecate,
isMochaError,
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions test/integration/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var fs = require('fs');
var path = require('path');
const {constants} = require('../../lib/errors');
var loadConfig = require('../../lib/cli/config').loadConfig;

describe('config', function () {
Expand Down Expand Up @@ -47,6 +48,28 @@ describe('config', function () {
expect(js, 'to equal', json);
});

it('should throw module not found error from absolute path configuration', function () {
function _loadConfig() {
loadConfig(path.join(configDir, 'mocharcWithInvalidImport.js'));
}

expect(_loadConfig, 'to throw', {
code: constants.MODULE_NOT_FOUND
});
});

it('should throw module not found error from relative path configuration', function () {
var relConfigDir = configDir.substring(projRootDir.length + 1);

function _loadConfig() {
loadConfig(path.join('.', relConfigDir, 'mocharcWithInvalidImport.js'));
}

expect(_loadConfig, 'to throw', {
code: constants.MODULE_NOT_FOUND
});
});

it('should rethrow error from absolute path configuration', function () {
function _loadConfig() {
loadConfig(path.join(configDir, 'mocharcWithThrowError.js'));
Expand Down
10 changes: 10 additions & 0 deletions test/integration/fixtures/config/mocharcWithInvalidImport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

require('invalidImport.js');
// a comment
module.exports = {
require: ['foo', 'bar'],
bail: true,
reporter: 'dot',
slow: 60
};
2 changes: 1 addition & 1 deletion test/integration/fixtures/config/mocharcWithThrowError.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

throw new Error("Error from mocharcWithThrowError");
throw new Error('Error from mocharcWithThrowError');

// a comment
module.exports = {
Expand Down