-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Feature: ESM configuration file #5353
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
base: main
Are you sure you want to change the base?
Changes from all commits
b4e7950
5590fb2
8b62fd4
4b0844c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,30 +24,45 @@ const utils = require('../utils'); | |
exports.CONFIG_FILES = [ | ||
'.mocharc.cjs', | ||
'.mocharc.js', | ||
'.mocharc.mjs', | ||
'.mocharc.yaml', | ||
'.mocharc.yml', | ||
'.mocharc.jsonc', | ||
'.mocharc.json' | ||
]; | ||
|
||
/** | ||
* Loads a CommonJS or ESM module, resolving its path if the file path is not | ||
* relative | ||
* | ||
* @param {string} filepath - Module file path to load | ||
* @returns {Object} CommonJS or ESM module | ||
*/ | ||
const loadModule = filepath => { | ||
let cwdFilepath; | ||
try { | ||
debug('parsers: load cwd-relative path: "%s"', path.resolve(filepath)); | ||
cwdFilepath = require.resolve(path.resolve(filepath)); // evtl. throws | ||
return require(cwdFilepath); | ||
} catch (err) { | ||
if (cwdFilepath) throw err; | ||
|
||
debug('parsers: retry load as module-relative path: "%s"', filepath); | ||
return require(filepath); | ||
} | ||
}; | ||
|
||
/** | ||
* Parsers for various config filetypes. Each accepts a filepath and | ||
* returns an object (but could throw) | ||
*/ | ||
const parsers = (exports.parsers = { | ||
yaml: filepath => require('js-yaml').load(fs.readFileSync(filepath, 'utf8')), | ||
js: filepath => { | ||
let cwdFilepath; | ||
try { | ||
debug('parsers: load cwd-relative path: "%s"', path.resolve(filepath)); | ||
cwdFilepath = require.resolve(path.resolve(filepath)); // evtl. throws | ||
return require(cwdFilepath); | ||
} catch (err) { | ||
if (cwdFilepath) throw err; | ||
js: loadModule, | ||
mjs: filepath => { | ||
const module = loadModule(filepath); | ||
|
||
debug('parsers: retry load as module-relative path: "%s"', filepath); | ||
return require(filepath); | ||
} | ||
return module.default ?? module; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to me on the Thinking out loud: that might be considered a breaking change. If someone happen to have a tl;dr, requesting changes: that the |
||
}, | ||
json: filepath => | ||
JSON.parse( | ||
|
@@ -73,7 +88,9 @@ exports.loadConfig = filepath => { | |
config = parsers.yaml(filepath); | ||
} else if (ext === '.js' || ext === '.cjs') { | ||
config = parsers.js(filepath); | ||
} else { | ||
} else if (ext === '.mjs') { | ||
config = parsers.mjs(filepath); | ||
}else { | ||
config = parsers.json(filepath); | ||
} | ||
} catch (err) { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,10 +12,12 @@ describe('config', function () { | |||||
var configDir = path.join(__dirname, 'fixtures', 'config'); | ||||||
var js = loadConfig(path.join(configDir, 'mocharc.js')); | ||||||
var cjs = loadConfig(path.join(configDir, 'mocharc.cjs')); | ||||||
var mjs = loadConfig(path.join(configDir, 'mocharc.mjs')); | ||||||
var json = loadConfig(path.join(configDir, 'mocharc.json')); | ||||||
var yaml = loadConfig(path.join(configDir, 'mocharc.yaml')); | ||||||
expect(js, 'to equal', json); | ||||||
expect(js, 'to equal', cjs); | ||||||
expect(mjs, 'to equal', cjs); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Bug] Typo?
Suggested change
|
||||||
expect(json, 'to equal', yaml); | ||||||
}); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
// a comment | ||
export default { | ||
require: ['foo', 'bar'], | ||
bail: true, | ||
reporter: 'dot', | ||
slow: 60 | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.