babel-utils/babel-plugin-tester

Question: How can I set the filename for the babel state for testing?

Closed this issue ยท 1 comments

  • babel-plugin-tester version: 10.1.0
  • node version: v18.8.0
  • npm (or yarn) version: 4.0.0-rc.13

Relevant code or config

import pluginTester from "babel-plugin-tester";
import { PluginPass } from "@babel/core";

pluginTester({
	plugin: babelPlugin,
        filename: 'testing.ts', // <-- doesn't seem to propagate to `state.filename` - it's probably not the right option for this I assume
	tests: [{ code: "'hello';" }],
});

const babelPlugin = function babelPlugin(): {
	visitor: Visitor<PluginPass>;
} {
	return {
		visitor: {
			Identifier(path, state) {
				console.log(state.filename);
                        }
                 }
        }
}

What you did:
I'm trying to test out how to use state.filename inside of a plugin and how I can set it in the test code.

What happened:
I'm getting an error since the state.filename variable is by default undefined and I have not found a way to add it via the test.

Problem description:
I've tried solving this by setting the filename option (not sure if that's the correct one) but it did not work. I have also thought about using one of the wrapper options (I'm not sure what they do so, so far no luck ๐Ÿ˜…)

Figured it out by a bit more of trial-and-error ๐Ÿ™‚

Since babel-plugin-tester uses the transform and transformAsync apis of babel, simply providing babelOptions.filename in the test sets the filename property on the state.