Say we have a JS library, and that library exports an API.
For the sake of argument, that library could be mocha and the API could be the "BDD" interface:
describe('my suite', function () {
it('should timeout in 10s', function (done) {
this.timeout(10);
setTimeout(done, 9000);
});
});
I hate the context here. tape passes a test
object to the callback instead. That'd work better.
The equivalent could look like this (and enables lambdas):
describe('my suite', () => {
it('should timeout in 10s', (test, done) => {
test.timeout(10);
setTimeout(done, 9000);
});
});
Of course, if we ever just changed the API, it'd break like half of the Mocha tests in existence, and that's bad.
What if we could provide something that actually wrapped tests and "upgraded" them, automatically?
Like a codemod--but not something the user would have to execute against their test sources in an ad-hoc manner.
It would be built in to the test runner itself:
- It could analyze the test source's AST, then
- Mangle it on-the-fly from the first example to the second
(There are other details like source maps and sane stack traces that'd need to be solved, of course)
Could babel macros do this?