babel-plugin-tester icon indicating copy to clipboard operation
babel-plugin-tester copied to clipboard

Testing non-plugins

Open jamiebuilds opened this issue 8 years ago • 1 comments

It would be nice to be able to use this tool for things other than plugins easily.

Right now I do stuff like this:

import pluginTester from 'babel-plugin-tester';
import printAST from 'ast-pretty-print';
import lib from './';

const plugin = () => {
  return {
    name: 'test-plugin',
    visitor: {
      Program(path) {
        let result = lib(path);
        let printed = printAST(result);
        path.pushContainer(
          'body', 
          t.expressionStatement(t.templateLiteral([
            t.templateElement({ raw: printed }, true)
          ]))
        );
      },
    },
  };
};

babelTester({
  plugin,
  snapshot: true,
  tests: [...]
});

But it'd be nice if I could get something lower level where I can just call a function on a Babel File and the returned result would get printed via ast-pretty-print for snapshotting.

babelTester({
  name: 'util',
  snapshot: true,
  fn: file => {
    return lib(file.path);
  },
  tests: [...]
});

jamiebuilds avatar Jun 02 '17 03:06 jamiebuilds

Sounds good to me. Kinda broadens the scope of the package a bit, which is fine.

kentcdodds avatar Jun 02 '17 04:06 kentcdodds

I'm personally not opposed to a PR implementing this, but as I work through open issues and consider implementing a couple new features, I'm of the opinion that this functionality is sufficiently out of scope. Hence, I'm closing this for now, but I'll defer to you both if there is interest.

Xunnamius avatar Oct 02 '22 23:10 Xunnamius