Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing the name of the input file in fixtures (overriding code.js) #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MeLlamoPablo
Copy link

This PR adds an option to override the default name of the code input in fixture directories from code.js to something else.

With this PR, the user will be able to add a babel-test.json configuration file containing the inputFileName parameter and override the default code.js value.

Rationale

The name of the file being transformed may affect the functionality of the transformer. This is the case of styled-components/babel-plugin-styled-components: this transformer adds useful debugging information by annotating the generated class names with the component names (see their docs).

To generate the display name of the component, they use a combination of the file name and the component name. For example, for the styled-component SubmitButton inside the file Forms.tsx, they will generate Forms_SubmitButton. If we had a Forms/SubmitButton/index.tsx structure, index_Forms would look bad and not be very useful, which is why, when the file name is index, they look at the directory name instead to still generate Forms_SubmitButton.

Now, this library can't currently test this case, because it can't name the test file index.js. It must be named code.js

More configuration?

I appreciate the value of opinionated libraries, and understand that this library has made an effort to enforce a consistent pattern. Adding configuration is not something to be done lightly, as it increases the complexity of the code and makes the library harder to reason about. I'd understand if this PR was rejected in favor of protecting the simplicity of the library.

However, I believe that in this case, adding this is fine, because:

  • It solves a real limitation of the library, not a style preference.
  • It is opt-in, and doesn't add any burden to new users of the library.
  • Creating a babel-test.json for each fixture is an intentional friction. There is no way to override the "global" default name. This ensures that using the default code.js stays always more convenient, and it is only overridden when necessary. Thus:
    • The enforced convention is protected. You won't have developers renaming it to input.js just because they like it better.
    • The file name is semantic. If it is named code.js, the test probably doesn't care about it. If there is a babel-config.json file and the file name is other than code.js, the developer can immediately understand that there is something important about the file name.
  • An alternative would be to use a standalone test, but this would increase the complexity of the test suite. It would make the developer think "why do we always use the fixtures directory except on this one test?", which defeats the point of having an opinionated convention.

Example usage

This PR adds tests which use a file named input.js. But you can see a real-world usage on this commit, where I implement the missing test that styled-components/babel-plugin-styled-components lacks.

Thanks for reading, and I'll be happy to make any changes or address any feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant