Allow customizing the name of the input file in fixtures (overriding code.js
)
#9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theinputFileName
parameter and override the defaultcode.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 fileForms.tsx
, they will generateForms_SubmitButton
. If we had aForms/SubmitButton/index.tsx
structure,index_Forms
would look bad and not be very useful, which is why, when the file name isindex
, they look at the directory name instead to still generateForms_SubmitButton
.Now, this library can't currently test this case, because it can't name the test file
index.js
. It must be namedcode.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:
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 defaultcode.js
stays always more convenient, and it is only overridden when necessary. Thus:input.js
just because they like it better.code.js
, the test probably doesn't care about it. If there is ababel-config.json
file and the file name is other thancode.js
, the developer can immediately understand that there is something important about the file name.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!