Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@boneskull
Created September 11, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boneskull/8df7ef15d877c483affd0d003ca69410 to your computer and use it in GitHub Desktop.
Save boneskull/8df7ef15d877c483affd0d003ca69410 to your computer and use it in GitHub Desktop.
how can I do this

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:

  1. It could analyze the test source's AST, then
  2. 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment