RuleTester - Add "debug" flag for valid/invalid code
See original GitHub issueThe problem you want to solve. While writing a custom rule, I found myself wanting to debug my rule but only for a specific code block. I was able to do this by commenting out all of the others - but then I was unable to see the status of those tests as I tweaked my custom rule for the code that I was interested in debugging. Once I had the rule working as expected for the code I was debugging, I uncommented the others and realized that some were broken.
It would be nice if there was a flag that I could pass into a specific code config that would enable a conditional breakpoint so that I could step into my custom rule for that code.
Your take on the correct solution to problem. I was thinking update the config for code to accept a “debug” flag, that would wrap the rule in an anonymous function with a debugger at the top.
// config accepts a "debug" flag that would enable a conditional debugger during testing of the code block
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [],
invalid: [{
code: "var foo = bar || baz",
debug: true
}],
});
// and then somewhere inside of RuleTester/linter the rule would be wrapped like so
function debuggedRule() {
debugger;
rule.apply(this, arguments);
}
// somewhere else 😅
var ruleToRun = config.debug ? debuggedRule : rule;
callRuleWithAstParsedSourceCode(ruleToRun, astParsedSourceCode)
Also as a best practice, I was thinking to automatically trigger a test failure if any of the rules have a “debug: true” flag - this way a debugger would hopefully not sneak into “live” code.
Are you willing to submit a pull request to implement this change? Yes - I can attempt to make a minimal set of changes. I have not really considered or understand how we might want to differentiate between wanting to debug the custom rule code vs the auto-fixing of the rule.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
Just an idea, maybe something like
before
function that would be called before running the rule on the test code:Thanks for the issue.
I have faced the similar issue multiple times. Rule tester is a public API and IMO, shipping
debugger
in public API maybe not be a good move. Can we tweak this to make it something like adding thedebugger
by the user itself instead of conditionally calling the function that is having the debugger?