Skip to content

Internal: generate tests for all supported frameworks per rule #589

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

Open
Belco90 opened this issue May 17, 2022 · 1 comment
Open

Internal: generate tests for all supported frameworks per rule #589

Belco90 opened this issue May 17, 2022 · 1 comment
Labels
enhancement New feature or request triage Pending to be triaged by a maintainer

Comments

@Belco90
Copy link
Member

Belco90 commented May 17, 2022

Plugin version

v5

What problem do you want to solve?

Quoting myself from #588:

Thinking about all the changes we did to test all rules for the corresponding Testing Library frameworks, I think it makes sense to add some improvements to our TestingLibraryRuleMeta and createRuleTester to automatically generate the test cases variations for indicated frameworks.

Your take on the correct solution?

In my mind, it could look like this:

// rules/foo.js
export default createTestingLibraryRule({
  // ...
  meta: {
    // ...
    supportedFrameworks: ['react', 'vue']
    // ^ we extend our TestingLibraryRuleMeta so it has this new required property,
    // which can be used also to autogenerate a table of compatible frameworks for each rule doc
  }
})

// test/lib/rules/foo.js
ruleTester.run(RULE_NAME, rule, {
  valid: [
    {
      code: `
      import { fireEvent } from '{TESTING_LIBRARY_FRAMEWORK}'
      //                                          ^ this placeholder gets replaced automatically,
      //                                             iterating over each value from supportedFrameworks
    
      await fireEvent.click(element)
      `
    }
  ]
})

Anything else?

No response

Do you want to submit a pull request to implement this change?

Yes

@Belco90 Belco90 added enhancement New feature or request triage Pending to be triaged by a maintainer labels May 17, 2022
@Belco90
Copy link
Member Author

Belco90 commented May 17, 2022

We could even derive supportedFrameworks from the recommendedConfig meta.

@MichaelDeBoey when the rules are enabled in a recommended config yes, but I'm afraid we can't rely on this when the rule isn't enabled in any shareable config (e.g. no-manual-cleanup, or prefer-explicit-assert). So we would need this new supportedFrameworks.

We should even be able to run all rules on all testing frameworks though.
I currently only did this when the rule is available in the config for that specific testing framework.

That made me think about my approach might not be right, actually. For example, in no-manual-cleanup there are different outputs depending on the testing framework. This is a scenario we couldn't reach with my proposal, so perhaps it has to be a callback, receiving the framework being tested to do things conditionally when needed, rather than just a placeholder replaced. Something like:

// test/lib/rules/foo.js
ruleTester.run(RULE_NAME, rule, ({ testingFramework }) => ({
  valid: [
    {
      code: `
      import { fireEvent } from ${testingFramework}
      await fireEvent.click(element)
      `
      output: testingFramework.startsWith('@marko') ? 'foo' : 'bar'
    }
  ]
}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request triage Pending to be triaged by a maintainer
Projects
None yet
Development

No branches or pull requests

1 participant