-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a naive Babel plugin that can be used by integration tests
- Loading branch information
1 parent
eaca9f0
commit 3fcd492
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |