From bd47d79ff1252fcae4358479c55f8f88da85646e Mon Sep 17 00:00:00 2001 From: khoaHyh Date: Wed, 24 Jan 2024 01:44:40 -0500 Subject: [PATCH] feat: handle nonexistent files passed to --file - 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 --- lib/cli/collect-files.js | 13 +++++++++++++ test/integration/options/file.spec.js | 27 ++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/cli/collect-files.js b/lib/cli/collect-files.js index cc04559443..c24fbbe42e 100644 --- a/lib/cli/collect-files.js +++ b/lib/cli/collect-files.js @@ -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. @@ -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(); diff --git a/test/integration/options/file.spec.js b/test/integration/options/file.spec.js index 88815376f0..15a9c83b22 100644 --- a/test/integration/options/file.spec.js +++ b/test/integration/options/file.spec.js @@ -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 = []; @@ -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'} + ); + }); });