Skip to content

Commit 400bdf7

Browse files
committed
Change Babel configuration format
Remove 'default' and 'inherits' options, since 'inherits' is now the default. In preparation for custom extension support and source compilation, move options for test compilation into the `testOptions` field.
1 parent 65d7ecc commit 400bdf7

File tree

4 files changed

+61
-112
lines changed

4 files changed

+61
-112
lines changed

lib/babel-config.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ const syntaxObjectRestSpreadPath = require.resolve('@babel/plugin-syntax-object-
1414
const transformTestFilesPath = require.resolve('@ava/babel-preset-transform-test-files');
1515

1616
function validate(conf) {
17-
if (conf === undefined || conf === null) {
18-
conf = 'default';
17+
if (conf === undefined) {
18+
return {testOptions: {}};
1919
}
2020

21-
// Check for valid babel config shortcuts (can be either `default` or `inherit`)
22-
const isValidShortcut = conf === 'default' || conf === 'inherit';
23-
24-
if (!conf || (typeof conf === 'string' && !isValidShortcut)) {
21+
if (!conf || typeof conf !== 'object' || !conf.testOptions || typeof conf.testOptions !== 'object' || Array.isArray(conf.testOptions) || Object.keys(conf).length > 1) {
2522
let message = colors.error(figures.cross);
2623
message += ' Unexpected Babel configuration for AVA. ';
2724
message += 'See ' + chalk.underline('https://github.com/avajs/ava#es2017-support') + ' for allowed values.';
@@ -115,12 +112,12 @@ function build(projectDir, cacheDir, userOptions, compileEnhancements) {
115112
};
116113

117114
// By default extend the project's Babel configuration, but allow this to be
118-
// disabled in userOptions.
119-
if (userOptions === 'default' || userOptions === 'inherit' || userOptions.babelrc !== false) {
115+
// disabled through userOptions.
116+
if (userOptions.testOptions.babelrc !== false) {
120117
baseOptions.babelrc = true;
121118
}
122-
if (userOptions.extends) {
123-
baseOptions.extends = userOptions.extends;
119+
if (userOptions.testOptions.extends) {
120+
baseOptions.extends = userOptions.testOptions.extends;
124121
}
125122

126123
// Include object rest spread support for Node.js versions that support it
@@ -138,9 +135,9 @@ function build(projectDir, cacheDir, userOptions, compileEnhancements) {
138135
});
139136

140137
let intermediateConfig = baseConfig;
141-
if (userOptions !== 'default' && userOptions !== 'inherit') {
138+
if (Object.keys(userOptions.testOptions).length > 0) {
142139
// At this level, babelrc *must* be false.
143-
const options = Object.assign({}, userOptions, {babelrc: false});
140+
const options = Object.assign({}, userOptions.testOptions, {babelrc: false});
144141
// Any extends option has been applied in baseConfig.
145142
delete options.extends;
146143
intermediateConfig = configManager.createConfig({

profile.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ Promise.longStackTraces();
3737

3838
const conf = pkgConf.sync('ava', {
3939
defaults: {
40-
babel: 'default'
40+
babel: {
41+
testOptions: {}
42+
}
4143
}
4244
});
4345

@@ -76,7 +78,7 @@ const cacheDir = findCacheDir({
7678
files: [file]
7779
}) || uniqueTempDir();
7880

79-
babelConfigHelper.build(process.cwd(), cacheDir, conf.babel, true)
81+
babelConfigHelper.build(process.cwd(), cacheDir, babelConfigHelper.validate(conf.babel), true)
8082
.then(result => {
8183
const precompiler = new CachingPrecompiler({
8284
path: cacheDir,

test/api.js

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const ROOT_DIR = path.join(__dirname, '..');
1212

1313
function apiCreator(options) {
1414
options = options || {};
15-
options.babelConfig = options.babelConfig || 'default';
15+
options.babelConfig = options.babelConfig || {testOptions: {}};
1616
options.projectDir = options.projectDir || ROOT_DIR;
1717
options.resolveTestsFrom = options.resolveTestsFrom || options.projectDir;
1818
const instance = new Api(options);
@@ -817,13 +817,14 @@ function generateTests(prefix, apiCreator) {
817817
});
818818
});
819819

820-
test(`${prefix} Custom Babel Plugin Support`, t => {
820+
test(`${prefix} babel.testOptions with a custom plugin`, t => {
821821
t.plan(2);
822822

823823
const api = apiCreator({
824824
babelConfig: {
825-
presets: ['@ava/stage-4'],
826-
plugins: [testCapitalizerPlugin]
825+
testOptions: {
826+
plugins: [testCapitalizerPlugin]
827+
}
827828
},
828829
cacheEnabled: false,
829830
projectDir: __dirname
@@ -841,31 +842,10 @@ function generateTests(prefix, apiCreator) {
841842
}, t.threw);
842843
});
843844

844-
test(`${prefix} Default babel config uses .babelrc`, t => {
845-
t.plan(3);
846-
847-
const api = apiCreator({
848-
projectDir: path.join(__dirname, 'fixture/babelrc')
849-
});
850-
851-
api.on('test-run', runStatus => {
852-
runStatus.on('test', data => {
853-
t.ok((data.title === 'foo') || (data.title === 'repeated test: foo'));
854-
});
855-
});
856-
857-
return api.run()
858-
.then(result => {
859-
t.is(result.passCount, 2);
860-
});
861-
});
862-
863-
test(`${prefix} babelConfig:"inherit" uses .babelrc`, t => {
845+
test(`${prefix} babel.testOptions.babelrc effectively defaults to true`, t => {
864846
t.plan(3);
865847

866848
const api = apiCreator({
867-
babelConfig: 'inherit',
868-
cacheEnabled: false,
869849
projectDir: path.join(__dirname, 'fixture/babelrc')
870850
});
871851

@@ -881,11 +861,13 @@ function generateTests(prefix, apiCreator) {
881861
});
882862
});
883863

884-
test(`${prefix} babelConfig:{babelrc:true} uses .babelrc`, t => {
864+
test(`${prefix} babel.testOptions.babelrc can explicitly be true`, t => {
885865
t.plan(3);
886866

887867
const api = apiCreator({
888-
babelConfig: {babelrc: true},
868+
babelConfig: {
869+
testOptions: {babelrc: true}
870+
},
889871
cacheEnabled: false,
890872
projectDir: path.join(__dirname, 'fixture/babelrc')
891873
});
@@ -902,11 +884,13 @@ function generateTests(prefix, apiCreator) {
902884
});
903885
});
904886

905-
test(`${prefix} babelConfig:{babelrc:false} does not use .babelrc`, t => {
887+
test(`${prefix} babel.testOptions.babelrc can explicitly be false`, t => {
906888
t.plan(2);
907889

908890
const api = apiCreator({
909-
babelConfig: {babelrc: false},
891+
babelConfig: {
892+
testOptions: {babelrc: false}
893+
},
910894
cacheEnabled: false,
911895
projectDir: path.join(__dirname, 'fixture/babelrc')
912896
});
@@ -923,13 +907,15 @@ function generateTests(prefix, apiCreator) {
923907
});
924908
});
925909

926-
test(`${prefix} babelConfig:{babelrc:true, plugins:[...]} merges plugins with .babelrc`, t => {
910+
test(`${prefix} babelConfig.testOptions merges plugins with .babelrc`, t => {
927911
t.plan(3);
928912

929913
const api = apiCreator({
930914
babelConfig: {
931-
plugins: [testCapitalizerPlugin],
932-
babelrc: true
915+
testOptions: {
916+
babelrc: true,
917+
plugins: [testCapitalizerPlugin]
918+
}
933919
},
934920
cacheEnabled: false,
935921
projectDir: path.join(__dirname, 'fixture/babelrc')
@@ -947,13 +933,15 @@ function generateTests(prefix, apiCreator) {
947933
});
948934
});
949935

950-
test(`${prefix} babelConfig:{extends:path, plugins:[...]} merges plugins with .babelrc`, t => {
936+
test(`${prefix} babelConfig.testOptions with extends still merges plugins with .babelrc`, t => {
951937
t.plan(3);
952938

953939
const api = apiCreator({
954940
babelConfig: {
955-
plugins: [testCapitalizerPlugin],
956-
extends: path.join(__dirname, 'fixture/babelrc/.alt-babelrc')
941+
testOptions: {
942+
plugins: [testCapitalizerPlugin],
943+
extends: path.join(__dirname, 'fixture/babelrc/.alt-babelrc')
944+
}
957945
},
958946
cacheEnabled: false,
959947
projectDir: path.join(__dirname, 'fixture/babelrc')

0 commit comments

Comments
 (0)