Skip to content

Commit

Permalink
add a naive Babel plugin that can be used by integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage authored and novemberborn committed Mar 7, 2016
1 parent eaca9f0 commit 3fcd492
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/fixture/babel-plugin-test-doubler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
A Babel plugin that causes each AVA test to be duplicated with a new title.
test('foo', t => {});
becomes
test('foo', t => {});
test('repeated test: foo', t => {});
This is used by some integration tests to validate correct handling of Babel config options.
*/

function plugin(babel) {
var t = babel.types;
var anonCount = 1;

return {
visitor: {
CallExpression: function (path) {
var node = path.node;
var callee = node.callee;
var args = node.arguments;
if (callee.type === 'Identifier' && callee.name === 'test') {
if (args.length === 1) {
args = [t.StringLiteral('repeated test: anonymous' + anonCount++), args[0]];
} else if (args.length === 2 && args[0].type === 'StringLiteral') {
if (/^repeated test/.test(args[0].value)) {
return;
}
args = args.slice();
args[0] = t.StringLiteral('repeated test: ' + args[0].value);
} else {
throw new Error('the plugin does not know how to handle this call to test');
}
path.insertAfter(t.CallExpression(
t.Identifier('test'),
args
));
}
}
}
};
}

module.exports = plugin;

0 comments on commit 3fcd492

Please sign in to comment.