-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimentally configure module formats for test files
Fixes #2345. Co-authored-by: Mark Wubben <mark@novemberborn.net>
- Loading branch information
1 parent
2b41fb0
commit 5c9dbb9
Showing
30 changed files
with
345 additions
and
11 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,9 @@ | ||
const skipTests = []; | ||
if (process.versions.node < '12.14.0') { | ||
skipTests.push('!test/configurable-module-format/module.js'); | ||
} | ||
|
||
export default { | ||
files: ['test/**', '!test/**/{fixtures,helpers}/**'], | ||
files: ['test/**', '!test/**/{fixtures,helpers}/**', ...skipTests], | ||
ignoredByWatcher: ['{coverage,docs,media,test-d,test-tap}/**'] | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const requireTrueValue = value => { | ||
if (value !== true) { | ||
throw new TypeError('When specifying module types, use `true` for ’cjs’, ’mjs’ and ’js’ extensions'); | ||
} | ||
}; | ||
|
||
const normalize = (extension, type, defaultModuleType) => { | ||
switch (extension) { | ||
case 'cjs': | ||
requireTrueValue(type); | ||
return 'commonjs'; | ||
case 'mjs': | ||
requireTrueValue(type); | ||
return 'module'; | ||
case 'js': | ||
requireTrueValue(type); | ||
return defaultModuleType; | ||
default: | ||
if (type !== 'commonjs' && type !== 'module') { | ||
throw new TypeError(`Module type for ’${extension}’ must be ’commonjs’ or ’module’`); | ||
} | ||
|
||
return type; | ||
} | ||
}; | ||
|
||
const deriveFromObject = (extensionsObject, defaultModuleType) => { | ||
const moduleTypes = {}; | ||
for (const [extension, type] of Object.entries(extensionsObject)) { | ||
moduleTypes[extension] = normalize(extension, type, defaultModuleType); | ||
} | ||
|
||
return moduleTypes; | ||
}; | ||
|
||
const deriveFromArray = (extensions, defaultModuleType) => { | ||
const moduleTypes = {}; | ||
for (const extension of extensions) { | ||
switch (extension) { | ||
case 'cjs': | ||
moduleTypes.cjs = 'commonjs'; | ||
break; | ||
case 'mjs': | ||
moduleTypes.mjs = 'module'; | ||
break; | ||
case 'js': | ||
moduleTypes.js = defaultModuleType; | ||
break; | ||
default: | ||
moduleTypes[extension] = 'commonjs'; | ||
} | ||
} | ||
|
||
return moduleTypes; | ||
}; | ||
|
||
module.exports = (configuredExtensions, defaultModuleType, experiments) => { | ||
if (configuredExtensions === undefined) { | ||
return { | ||
cjs: 'commonjs', | ||
mjs: 'module', | ||
js: defaultModuleType | ||
}; | ||
} | ||
|
||
if (Array.isArray(configuredExtensions)) { | ||
return deriveFromArray(configuredExtensions, defaultModuleType); | ||
} | ||
|
||
if (!experiments.configurableModuleFormat) { | ||
throw new Error('You must enable the `configurableModuleFormat` experiment in order to specify module types'); | ||
} | ||
|
||
return deriveFromObject(configuredExtensions, defaultModuleType); | ||
}; |
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,26 @@ | ||
const test = require('@ava/test'); | ||
const exec = require('../helpers/exec'); | ||
|
||
test('load js and cjs as commonjs (default configuration)', async t => { | ||
const result = await exec.fixture(['*.js', '*.cjs']); | ||
const files = new Set(result.stats.passed.map(({file}) => file)); | ||
t.is(files.size, 2); | ||
t.true(files.has('test.cjs')); | ||
t.true(files.has('test.js')); | ||
}); | ||
|
||
test('load js and cjs as commonjs (using an extensions array)', async t => { | ||
const result = await exec.fixture(['*.js', '*.cjs', '--config', 'array-extensions.config.js']); | ||
const files = new Set(result.stats.passed.map(({file}) => file)); | ||
t.is(files.size, 2); | ||
t.true(files.has('test.cjs')); | ||
t.true(files.has('test.js')); | ||
}); | ||
|
||
test('load js and cjs as commonjs (using an extensions object)', async t => { | ||
const result = await exec.fixture(['*.js', '*.cjs', '--config', 'object-extensions.config.js']); | ||
const files = new Set(result.stats.passed.map(({file}) => file)); | ||
t.is(files.size, 2); | ||
t.true(files.has('test.cjs')); | ||
t.true(files.has('test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const test = require('@ava/test'); | ||
const exec = require('../helpers/exec'); | ||
|
||
test('load ts as commonjs (using an extensions array)', async t => { | ||
const result = await exec.fixture(['*.ts', '--config', 'array-custom.config.js']); | ||
const files = new Set(result.stats.passed.map(({file}) => file)); | ||
t.is(files.size, 1); | ||
t.true(files.has('test.ts')); | ||
}); | ||
|
||
test('load ts as commonjs (using an extensions object)', async t => { | ||
const result = await exec.fixture(['*.ts', '--config', 'object-custom.config.js']); | ||
const files = new Set(result.stats.passed.map(({file}) => file)); | ||
t.is(files.size, 1); | ||
t.true(files.has('test.ts')); | ||
}); |
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 @@ | ||
const test = require('@ava/test'); | ||
const exec = require('../helpers/exec'); | ||
|
||
const stripLeadingFigures = string => string.replace(/^\W+/, ''); | ||
|
||
test('opt-in is required', async t => { | ||
const result = await t.throwsAsync(exec.fixture(['--config', 'not-enabled.config.js'])); | ||
t.is(result.exitCode, 1); | ||
t.snapshot(stripLeadingFigures(result.stderr.trim())); | ||
}); |
3 changes: 3 additions & 0 deletions
3
test/configurable-module-format/fixtures/array-custom.config.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
extensions: ['js', 'ts'] | ||
}; |
3 changes: 3 additions & 0 deletions
3
test/configurable-module-format/fixtures/array-extensions.config.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
extensions: ['js', 'cjs', 'mjs'] | ||
}; |
9 changes: 9 additions & 0 deletions
9
test/configurable-module-format/fixtures/bad-custom-type.config.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default { | ||
extensions: { | ||
js: true, | ||
ts: 'cjs' | ||
}, | ||
nonSemVerExperiments: { | ||
configurableModuleFormat: true | ||
} | ||
}; |
8 changes: 8 additions & 0 deletions
8
test/configurable-module-format/fixtures/change-cjs-loading.config.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
extensions: { | ||
cjs: 'module' | ||
}, | ||
nonSemVerExperiments: { | ||
configurableModuleFormat: true | ||
} | ||
}; |
8 changes: 8 additions & 0 deletions
8
test/configurable-module-format/fixtures/change-js-loading.config.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
extensions: { | ||
js: 'module' | ||
}, | ||
nonSemVerExperiments: { | ||
configurableModuleFormat: true | ||
} | ||
}; |
8 changes: 8 additions & 0 deletions
8
test/configurable-module-format/fixtures/change-mjs-loading.config.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
extensions: { | ||
mjs: 'commonjs' | ||
}, | ||
nonSemVerExperiments: { | ||
configurableModuleFormat: true | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
7
test/configurable-module-format/fixtures/not-enabled.config.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
extensions: { | ||
js: true, | ||
cjs: true, | ||
mjs: true | ||
} | ||
}; |
9 changes: 9 additions & 0 deletions
9
test/configurable-module-format/fixtures/object-custom.config.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default { | ||
extensions: { | ||
js: true, | ||
ts: 'commonjs' | ||
}, | ||
nonSemVerExperiments: { | ||
configurableModuleFormat: true | ||
} | ||
}; |
10 changes: 10 additions & 0 deletions
10
test/configurable-module-format/fixtures/object-extensions.config.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default { | ||
extensions: { | ||
js: true, | ||
cjs: true, | ||
mjs: true | ||
}, | ||
nonSemVerExperiments: { | ||
configurableModuleFormat: 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 @@ | ||
{} |
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 @@ | ||
const test = require('ava'); | ||
|
||
test('always passing test', t => { | ||
t.pass(); | ||
}); |
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 @@ | ||
const test = require('ava'); | ||
|
||
test('always passing test', t => { | ||
t.pass(); | ||
}); |
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 @@ | ||
import test from 'ava'; | ||
|
||
test('always passing test', t => { | ||
t.pass(); | ||
}); |
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,8 @@ | ||
// eslint-disable-next-line ava/no-ignored-test-files | ||
const test = require('ava'); | ||
|
||
test('always passing test', t => { | ||
const numberWithTypes = 0; | ||
|
||
t.is(numberWithTypes, 0); | ||
}); |
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,24 @@ | ||
const test = require('@ava/test'); | ||
const exec = require('../helpers/exec'); | ||
|
||
const stripLeadingFigures = string => string.replace(/^\W+/, ''); | ||
|
||
test('cannot configure how js extensions should be loaded', async t => { | ||
const result = await t.throwsAsync(exec.fixture(['--config', 'change-js-loading.config.js'])); | ||
t.snapshot(stripLeadingFigures(result.stderr.trim())); | ||
}); | ||
|
||
test('cannot configure how cjs extensions should be loaded', async t => { | ||
const result = await t.throwsAsync(exec.fixture(['--config', 'change-cjs-loading.config.js'])); | ||
t.snapshot(stripLeadingFigures(result.stderr.trim())); | ||
}); | ||
|
||
test('cannot configure how mjs extensions should be loaded', async t => { | ||
const result = await t.throwsAsync(exec.fixture(['--config', 'change-mjs-loading.config.js'])); | ||
t.snapshot(stripLeadingFigures(result.stderr.trim())); | ||
}); | ||
|
||
test('custom extensions must be either commonjs or module', async t => { | ||
const result = await t.throwsAsync(exec.fixture(['--config', 'bad-custom-type.config.js'])); | ||
t.snapshot(stripLeadingFigures(result.stderr.trim())); | ||
}); |
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,23 @@ | ||
const test = require('@ava/test'); | ||
const exec = require('../helpers/exec'); | ||
|
||
test('load mjs as module (default configuration)', async t => { | ||
const result = await exec.fixture(['*.mjs']); | ||
const files = new Set(result.stats.passed.map(({file}) => file)); | ||
t.is(files.size, 1); | ||
t.true(files.has('test.mjs')); | ||
}); | ||
|
||
test('load mjs as module (using an extensions array)', async t => { | ||
const result = await exec.fixture(['*.mjs', '--config', 'array-extensions.config.js']); | ||
const files = new Set(result.stats.passed.map(({file}) => file)); | ||
t.is(files.size, 1); | ||
t.true(files.has('test.mjs')); | ||
}); | ||
|
||
test('load mjs as module (using an extensions object)', async t => { | ||
const result = await exec.fixture(['*.mjs', '--config', 'object-extensions.config.js']); | ||
const files = new Set(result.stats.passed.map(({file}) => file)); | ||
t.is(files.size, 1); | ||
t.true(files.has('test.mjs')); | ||
}); |
11 changes: 11 additions & 0 deletions
11
test/configurable-module-format/snapshots/experimental.js.md
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,11 @@ | ||
# Snapshot report for `test/configurable-module-format/experimental.js` | ||
|
||
The actual snapshot is saved in `experimental.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## opt-in is required | ||
|
||
> Snapshot 1 | ||
'You must enable the `configurableModuleFormat` experiment in order to specify module types' |
Binary file not shown.
Oops, something went wrong.