Skip to content

Commit

Permalink
add tests that demonstrate various ways to use babel config.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Apr 5, 2016
1 parent 8465934 commit 9288d78
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 9 deletions.
101 changes: 94 additions & 7 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var figures = require('figures');
var rimraf = require('rimraf');
var test = require('tap').test;
var Api = require('../api');
var testDoublerPlugin = require('./fixture/babel-plugin-test-doubler');
var testCapitalizerPlugin = require('./fixture/babel-plugin-test-capitalizer');

test('must be called with new', function (t) {
t.throws(function () {
Expand Down Expand Up @@ -731,29 +731,116 @@ test('verify test count', function (t) {
});

test('Custom Babel Plugin Support', function (t) {
t.plan(1);
t.plan(2);

var api = new Api({
babelConfig: {
presets: ['es2015', 'stage-2'],
plugins: [testDoublerPlugin]
}
plugins: [testCapitalizerPlugin]
},
cacheEnabled: false
});

api.run([path.join(__dirname, 'fixture/es2015.js')])
api.on('test', function (data) {
t.is(data.title, 'FOO');
});

api.run([path.join(__dirname, 'fixture/babelrc/test.js')])
.then(
function () {
t.is(api.passCount, 2);
t.is(api.passCount, 1);
},
t.threw
);
});

test('Default babel config doesn\'t use .babelrc', function (t) {
t.plan(1);
t.plan(2);

var api = new Api();

api.on('test', function (data) {
t.is(data.title, 'foo');
});

return api.run([path.join(__dirname, 'fixture/babelrc/test.js')])
.then(function () {
t.is(api.passCount, 1);
});
});

test('babelConfig:"inherit" uses .babelrc', function (t) {
t.plan(3);

var api = new Api({
babelConfig: 'inherit',
cacheEnabled: false
});

api.on('test', function (data) {
t.ok((data.title === 'foo') || (data.title === 'repeated test: foo'));
});

return api.run([path.join(__dirname, 'fixture/babelrc/test.js')])
.then(function () {
t.is(api.passCount, 2);
});
});

test('babelConfig:{babelrc:true} uses .babelrc', function (t) {
t.plan(3);

var api = new Api({
babelConfig: {babelrc: true},
cacheEnabled: false
});

api.on('test', function (data) {
t.ok((data.title === 'foo') || (data.title === 'repeated test: foo'));
});

return api.run([path.join(__dirname, 'fixture/babelrc/test.js')])
.then(function () {
t.is(api.passCount, 2);
});
});

test('babelConfig:{babelrc:true, plugins:[...]} merges plugins with .babelrc', function (t) {
t.plan(3);

var api = new Api({
babelConfig: {
plugins: [testCapitalizerPlugin],
babelrc: true
},
cacheEnabled: false
});

api.on('test', function (data) {
t.ok((data.title === 'FOO') || /^repeated test:/.test(data.title));
});

return api.run([path.join(__dirname, 'fixture/babelrc/test.js')])
.then(function () {
t.is(api.passCount, 2);
});
});

test('babelConfig:{extends:path, plugins:[...]} merges plugins with .babelrc', function (t) {
t.plan(2);

var api = new Api({
babelConfig: {
plugins: [testCapitalizerPlugin],
extends: path.join(__dirname, 'fixture/babelrc/.alt-babelrc')
},
cacheEnabled: false
});

api.on('test', function (data) {
t.ok((data.title === 'BAR'));
});

return api.run([path.join(__dirname, 'fixture/babelrc/test.js')])
.then(function () {
t.is(api.passCount, 1);
Expand Down
19 changes: 19 additions & 0 deletions test/fixture/babel-plugin-foo-to-bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = function (babel) {
var t = babel.types;

return {
visitor: {
CallExpression: function (path) {
// skip require calls
var firstArg = path.get('arguments')[0];
if (!isRequire(path) && firstArg && firstArg.isStringLiteral() && /foo/i.test(firstArg.node.value)) {
firstArg.replaceWith(t.stringLiteral(firstArg.node.value.replace('foo', 'bar').replace('FOO', 'BAR')));
}
}
}
};
};

function isRequire(path) {
return path.isCallExpression() && path.get('callee').isIdentifier() && (path.get('callee').node.name === 'require');
}
19 changes: 19 additions & 0 deletions test/fixture/babel-plugin-test-capitalizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = function (babel) {
var t = babel.types;

return {
visitor: {
CallExpression: function (path) {
// skip require calls
var firstArg = path.get('arguments')[0];
if (!isRequire(path) && firstArg && firstArg.isStringLiteral() && !/repeated test/.test(firstArg.node.value)) {
firstArg.replaceWith(t.stringLiteral(firstArg.node.value.toUpperCase()));
}
}
}
};
};

function isRequire(path) {
return path.isCallExpression() && path.get('callee').isIdentifier() && (path.get('callee').node.name === 'require');
}
4 changes: 4 additions & 0 deletions test/fixture/babelrc/.alt-babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015", "stage-2"],
"plugins": ["../babel-plugin-foo-to-bar"]
}
3 changes: 2 additions & 1 deletion test/fixture/babelrc/.babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"plugins": ["this-plugin-does-not-exist"]
"presets": ["es2015", "stage-2"],
"plugins": ["../babel-plugin-test-doubler"]
}
2 changes: 1 addition & 1 deletion test/fixture/babelrc/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import test from '../../../'

test(t => t.pass());
test('foo', t => t.pass());

0 comments on commit 9288d78

Please sign in to comment.