Skip to content

Commit

Permalink
feat: handle nonexistent files passed to --file
Browse files Browse the repository at this point in the history
- added error handling when using the --file flag to do it the way
  --require does
- added a test to assert that we throw the same type of error
  • Loading branch information
khoaHyh committed Jan 24, 2024
1 parent 53a4baf commit bd47d79
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lib/cli/collect-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const minimatch = require('minimatch');
const {NO_FILES_MATCH_PATTERN} = require('../errors').constants;
const lookupFiles = require('./lookup-files');
const {castArray} = require('../utils');
const symbols = require('log-symbols');

/**
* Exports a function that collects test files from CLI parameters.
Expand Down Expand Up @@ -52,6 +53,18 @@ module.exports = ({
}
}, []);

fileArgs.forEach(file => {
const fileAbsolutePath = path.resolve(file);
try {
require.resolve(fileAbsolutePath);
} catch (err) {
// this error doesn't bubble up to the middleware like how
// --require handles it so we have to do it here
console.error(`\n${symbols.error} ${ansi.red('ERROR:')}`, err);
process.exit(1);
}
});

// ensure we don't sort the stuff from fileArgs; order is important!
if (sort) {
specFiles.sort();
Expand Down
27 changes: 24 additions & 3 deletions test/integration/options/file.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

var path = require('path').posix;
var helpers = require('../helpers');
var runMochaJSON = helpers.runMochaJSON;
var resolvePath = helpers.resolveFixturePath;
const {
runMochaJSON,
resolveFixturePath: resolvePath,
runMocha
} = require('../helpers');

describe('--file', function () {
var args = [];
Expand Down Expand Up @@ -64,4 +66,23 @@ describe('--file', function () {
done();
});
});

it('should throw an ERR_MODULE_NOT_FOUND if a nonexistent file is specified', function (done) {
runMocha(
'esm/type-module/test-that-imports-non-existing-module.fixture.js',
['--file'],
function (err, res) {
if (err) {
return done(err);
}

expect(res.output, 'to contain', 'ERR_MODULE_NOT_FOUND').and(
'to contain',
'test-that-imports-non-existing-module'
);
done();
},
{stdio: 'pipe'}
);
});
});

0 comments on commit bd47d79

Please sign in to comment.