Skip to content

Commit ff09749

Browse files
committed
Support babel.config.js
1 parent ff55236 commit ff09749

File tree

6 files changed

+161
-3
lines changed

6 files changed

+161
-3
lines changed

lib/babel-pipeline.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function validate(conf) {
3838
return null;
3939
}
4040

41-
const defaultOptions = {babelrc: true};
41+
const defaultOptions = {babelrc: true, configFile: true};
4242

4343
if (conf === undefined) {
4444
return {testOptions: defaultOptions};
@@ -102,7 +102,7 @@ function wantsStage4(userOptions, projectDir) {
102102
});
103103
}
104104

105-
function hashPartialTestConfig({babelrc, options: {plugins, presets}}, projectDir, pluginAndPresetHashes) {
105+
function hashPartialTestConfig({babelrc, config, options: {plugins, presets}}, projectDir, pluginAndPresetHashes) {
106106
const inputs = [];
107107
if (babelrc) {
108108
inputs.push(babelrc);
@@ -114,7 +114,9 @@ function hashPartialTestConfig({babelrc, options: {plugins, presets}}, projectDi
114114
inputs.push(stripBomBuf(fs.readFileSync(babelrc)));
115115
}
116116
}
117-
// TODO: If present, hash config contents
117+
if (config) {
118+
inputs.push(config, stripBomBuf(fs.readFileSync(config)));
119+
}
118120

119121
for (const {file: {resolved: filename}} of [...plugins, ...presets]) {
120122
if (pluginAndPresetHashes.has(filename)) {

test/api.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,105 @@ test('babel.testOptions.babelrc (when true) picks up .babelrc.js files', t => {
872872
});
873873
});
874874

875+
test('babel.testOptions.configFile effectively defaults to true', t => {
876+
t.plan(3);
877+
878+
const api = apiCreator({
879+
projectDir: path.join(__dirname, 'fixture/babel-config')
880+
});
881+
882+
api.on('run', plan => {
883+
plan.status.on('stateChange', evt => {
884+
if (evt.type === 'test-passed') {
885+
t.ok((evt.title === 'foo') || (evt.title === 'repeated test: foo'));
886+
}
887+
});
888+
});
889+
890+
return api.run()
891+
.then(runStatus => {
892+
t.is(runStatus.stats.passedTests, 2);
893+
});
894+
});
895+
896+
test('babel.testOptions.configFile can explicitly be true', t => {
897+
t.plan(3);
898+
899+
const api = apiCreator({
900+
babelConfig: {
901+
testOptions: {configFile: true}
902+
},
903+
cacheEnabled: false,
904+
projectDir: path.join(__dirname, 'fixture/babel-config')
905+
});
906+
907+
api.on('run', plan => {
908+
plan.status.on('stateChange', evt => {
909+
if (evt.type === 'test-passed') {
910+
t.ok(evt.title === 'foo' || evt.title === 'repeated test: foo');
911+
}
912+
});
913+
});
914+
915+
return api.run()
916+
.then(runStatus => {
917+
t.is(runStatus.stats.passedTests, 2);
918+
});
919+
});
920+
921+
test('babel.testOptions.configFile can explicitly be false', t => {
922+
t.plan(2);
923+
924+
const api = apiCreator({
925+
babelConfig: {
926+
testOptions: {configFile: false}
927+
},
928+
cacheEnabled: false,
929+
projectDir: path.join(__dirname, 'fixture/babel-config')
930+
});
931+
932+
api.on('run', plan => {
933+
plan.status.on('stateChange', evt => {
934+
if (evt.type === 'test-passed') {
935+
t.is(evt.title, 'foo');
936+
}
937+
});
938+
});
939+
940+
return api.run()
941+
.then(runStatus => {
942+
t.is(runStatus.stats.passedTests, 1);
943+
});
944+
});
945+
946+
test('babel.testOptions merges plugins with babel.config.js', t => {
947+
t.plan(3);
948+
949+
const api = apiCreator({
950+
babelConfig: {
951+
testOptions: {
952+
babelrc: true,
953+
plugins: [testCapitalizerPlugin]
954+
}
955+
},
956+
cacheEnabled: false,
957+
projectDir: path.join(__dirname, 'fixture/babel-config')
958+
});
959+
960+
api.on('run', plan => {
961+
plan.status.on('stateChange', evt => {
962+
if (evt.type === 'test-passed') {
963+
t.ok(evt.title === 'FOO' || evt.title === 'repeated test: foo');
964+
}
965+
});
966+
});
967+
968+
return api.run()
969+
.then(runStatus => {
970+
t.is(runStatus.stats.passedTests, 2);
971+
});
972+
});
973+
875974
test('babel.testOptions can disable ava/stage-4', t => {
876975
t.plan(1);
877976

@@ -925,6 +1024,34 @@ test('babel.testOptions with extends still merges plugins with .babelrc', t => {
9251024
});
9261025
});
9271026

1027+
test('babel.testOptions with extends still merges plugins with babel.config.js', t => {
1028+
t.plan(3);
1029+
1030+
const api = apiCreator({
1031+
babelConfig: {
1032+
testOptions: {
1033+
plugins: [testCapitalizerPlugin],
1034+
extends: path.join(__dirname, 'fixture/babel-config/.alt-babelrc')
1035+
}
1036+
},
1037+
cacheEnabled: false,
1038+
projectDir: path.join(__dirname, 'fixture/babel-config')
1039+
});
1040+
1041+
api.on('run', plan => {
1042+
plan.status.on('stateChange', evt => {
1043+
if (evt.type === 'test-passed') {
1044+
t.ok(evt.title === 'BAR' || evt.title === 'repeated test: bar');
1045+
}
1046+
});
1047+
});
1048+
1049+
return api.run()
1050+
.then(runStatus => {
1051+
t.is(runStatus.stats.passedTests, 2);
1052+
});
1053+
});
1054+
9281055
test('extended config can disable ava/stage-4', t => {
9291056
t.plan(1);
9301057

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["../babel-plugin-foo-to-bar"]
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
presets: ['@ava/stage-4'],
3+
env: {
4+
development: {
5+
plugins: ['../babel-plugin-test-capitalizer']
6+
},
7+
test: {
8+
plugins: ['../babel-plugin-test-doubler']
9+
}
10+
}
11+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "application-name",
3+
"version": "0.0.1"
4+
}

test/fixture/babel-config/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import test from '../../..';
2+
3+
const one = {one: 1};
4+
const two = {two: 2};
5+
6+
test('foo', t => {
7+
// Using object rest/spread to ensure it transpiles on Node.js 6, since this
8+
// is a Node.js 8 feature
9+
const actual = {...one, ...two};
10+
t.deepEqual(actual, {one: 1, two: 2});
11+
});

0 commit comments

Comments
 (0)