-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for loading config from ESM modules (#7)
Fixes #6
- Loading branch information
1 parent
79e3da7
commit bc5ea3e
Showing
24 changed files
with
228 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"test-ignore": "helpers", | ||
"check-coverage": true | ||
"esm": false, | ||
"nyc-arg": "--nycrc-path=nyc-settings.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
module.exports = async filename => { | ||
const mod = await import(filename); | ||
if ('default' in mod === false) { | ||
throw new Error(`${filename} has no default export`); | ||
} | ||
|
||
return mod.default; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const {hasImport} = require('./test/helpers'); | ||
|
||
const include = [ | ||
'index.js' | ||
]; | ||
|
||
if (hasImport) { | ||
include.push('load-esm.js'); | ||
} | ||
|
||
module.exports = {include}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,68 @@ | ||
const t = require('tap'); | ||
const {fixturePath, basicTest} = require('./helpers'); | ||
const {fixturePath, basicTest, hasImport, hasESM} = require('./helpers'); | ||
const {loadNycConfig} = require('..'); | ||
|
||
t.test('options.nycrcPath points to non-existent file', t => { | ||
t.test('options.nycrcPath points to non-existent file', async t => { | ||
const cwd = fixturePath(); | ||
const nycrcPath = fixturePath('does-not-exist.json'); | ||
t.throws(() => loadNycConfig({cwd, nycrcPath})); | ||
t.end(); | ||
await t.rejects(loadNycConfig({cwd, nycrcPath})); | ||
}); | ||
|
||
t.test('no-config-file', basicTest); | ||
t.test('nycrc-no-ext', basicTest); | ||
t.test('nycrc-json', basicTest); | ||
t.test('nycrc-yml', basicTest); | ||
t.test('nycrc-yaml', basicTest); | ||
t.test('nyc-config-cjs', basicTest); | ||
t.test('nyc-config-js', basicTest); | ||
t.test('nyc-config-async', basicTest); | ||
t.test('array-field-fixup', basicTest); | ||
t.test('camel-decamel', basicTest); | ||
t.test('extends', basicTest); | ||
t.test('extends-array-empty', basicTest); | ||
t.test('extends-array', basicTest); | ||
|
||
t.test('extends failures', t => { | ||
const errorConfigs = ['looper1.json', 'invalid.json', 'missing.json']; | ||
t.test('extends failures', async t => { | ||
const cwd = fixturePath('extends'); | ||
errorConfigs.map(f => fixturePath('extends', f)).forEach(nycrcPath => { | ||
t.throws(() => loadNycConfig({cwd, nycrcPath})); | ||
const files = { | ||
'looper1.json': /Circular extended configurations/, | ||
'invalid.json': /contains an invalid 'extends' option/, | ||
'invalid.js': /Unexpected identifier/, | ||
'invalid.cjs': /Unexpected identifier/, | ||
'missing.json': /Could not resolve configuration file/ | ||
}; | ||
if (hasImport && await hasESM()) { | ||
files['invalid.mjs'] = /has no default export/; | ||
} | ||
|
||
const tests = Object.entries(files).map(([file, error]) => { | ||
return t.rejects(loadNycConfig({ | ||
cwd, | ||
nycrcPath: fixturePath('extends', file) | ||
}), error, file); | ||
}); | ||
t.end(); | ||
|
||
await Promise.all(tests); | ||
}); | ||
|
||
t.test('no package.json', t => { | ||
t.test('no package.json', async t => { | ||
const cwd = '/'; | ||
const nycrcPath = fixturePath('nycrc-no-ext', '.nycrc'); | ||
|
||
t.matchSnapshot(loadNycConfig({cwd}), 'no config'); | ||
t.matchSnapshot(loadNycConfig({cwd, nycrcPath}), 'explicit .nycrc'); | ||
t.end(); | ||
t.matchSnapshot(await loadNycConfig({cwd}), 'no config'); | ||
t.matchSnapshot(await loadNycConfig({cwd, nycrcPath}), 'explicit .nycrc'); | ||
}); | ||
|
||
if (hasImport) { | ||
t.test('esm', async t => { | ||
if (await hasESM()) { | ||
if (process.versions.node.split('.')[0] >= 12) { | ||
await t.test('nyc-config-js-type-module', basicTest); | ||
} | ||
|
||
await t.test('nyc-config-mjs', basicTest); | ||
} else { | ||
t.pass('we have import but it doesn\'t support ES modules'); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
syntax error! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
syntax error! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This is not supported, config must be provided in the default export. | ||
export const all = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const {promisify} = require('util'); | ||
|
||
const delay = promisify(setTimeout); | ||
|
||
async function loadConfig() { | ||
await delay(10); | ||
return {all: false}; | ||
} | ||
|
||
module.exports = loadConfig(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"nyc": { | ||
"all": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = {all: false}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
all: false | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "module", | ||
"nyc": { | ||
"all": true | ||
} | ||
} |
Oops, something went wrong.