Browse Source

add tests that demonstrate various ways to use babel config.

browser-support
James Talmage 9 years ago
parent
commit
9288d78a0b
  1. 101
      test/api.js
  2. 19
      test/fixture/babel-plugin-foo-to-bar.js
  3. 19
      test/fixture/babel-plugin-test-capitalizer.js
  4. 4
      test/fixture/babelrc/.alt-babelrc
  5. 3
      test/fixture/babelrc/.babelrc
  6. 2
      test/fixture/babelrc/test.js

101
test/api.js

@ -5,7 +5,7 @@ var figures = require('figures');
var rimraf = require('rimraf'); var rimraf = require('rimraf');
var test = require('tap').test; var test = require('tap').test;
var Api = require('../api'); 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) { test('must be called with new', function (t) {
t.throws(function () { t.throws(function () {
@ -731,29 +731,116 @@ test('verify test count', function (t) {
}); });
test('Custom Babel Plugin Support', function (t) { test('Custom Babel Plugin Support', function (t) {
t.plan(1); t.plan(2);
var api = new Api({ var api = new Api({
babelConfig: { babelConfig: {
presets: ['es2015', 'stage-2'], 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( .then(
function () { function () {
t.is(api.passCount, 2); t.is(api.passCount, 1);
}, },
t.threw t.threw
); );
}); });
test('Default babel config doesn\'t use .babelrc', function (t) { test('Default babel config doesn\'t use .babelrc', function (t) {
t.plan(1); t.plan(2);
var api = new Api(); 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')]) return api.run([path.join(__dirname, 'fixture/babelrc/test.js')])
.then(function () { .then(function () {
t.is(api.passCount, 1); t.is(api.passCount, 1);

19
test/fixture/babel-plugin-foo-to-bar.js

@ -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
test/fixture/babel-plugin-test-capitalizer.js

@ -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
test/fixture/babelrc/.alt-babelrc

@ -0,0 +1,4 @@
{
"presets": ["es2015", "stage-2"],
"plugins": ["../babel-plugin-foo-to-bar"]
}

3
test/fixture/babelrc/.babelrc

@ -1,3 +1,4 @@
{ {
"plugins": ["this-plugin-does-not-exist"] "presets": ["es2015", "stage-2"],
"plugins": ["../babel-plugin-test-doubler"]
} }

2
test/fixture/babelrc/test.js

@ -1,3 +1,3 @@
import test from '../../../' import test from '../../../'
test(t => t.pass()); test('foo', t => t.pass());

Loading…
Cancel
Save