Allow regexes in RuleTester
See original GitHub issue- ESLint Version: 3.5.0
It would be nice for the RuleTester to allow regular expressions to match messages, rather than just strings. At Facebook, some of our lint rules have very long messages - Here’s one such message from a lint rule that has several possible messages:
context.report({
node: node,
message:
'This is a shared module but it is referencing a ' +
'{{dependencyProduct}} module ({{requiredModule}}). Shared ' +
'modules should not have any dependencies on product code. ' +
'If this module is specific to {{dependencyProduct}}, it should ' +
'be moved to {{productPath}}. Contact danlo@ if this is a ' +
'false positive.',
data: {
dependencyProduct: dependencyProduct,
productPath: getPathForProductName(dependencyProduct),
requiredModule: requiredModule,
},
});
And an example of one of the test cases:
{
// Shared module requiring a Power Editor module
code: 'require("AdsPEApplication")',
filename: 'html/js/ads/utils/AdsBidUtils.js',
errors: [
'This is a shared module but it is referencing a Power Editor module ' +
'(AdsPEApplication). Shared modules should not have any dependencies ' +
'on product code. If this module is specific to Power Editor, it ' +
'should be moved to html/js/ads/powereditor. Contact danlo@ if this ' +
'is a false positive.',
],
},
All I really want to check is that the correct message is being returned, and the correct variables are merged into it. Right now I need to copy and paste the entire message for every test case. If the message changes at all, I need to remember to update every test case. Alternatively, I could export the message from the lint rule module and import it into the unit test, but that feels a bit messy and I’d still need to deal with the variables in the string.
It would be nicer to be able to use a regex, so I could do something like:
errors: [
/Power Editor module \(AdsPEApplication\).+should be moved to html\/js\/ads\/powereditor/,
],
Alternatively, it would also be useful to be able to provide a “message ID” to the context.report
call:
context.report({
node: node,
message: 'Hello world',
message_id: 2, // Any arbitrary integer
});
Such that the RuleTester could match the message by ID rather than by actual message text:
{
code: '// Foo bar',
errors: [
{id: 2}, // "2" matches the integer used in context.report
],
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:11 (11 by maintainers)
Top GitHub Comments
Hi! Sorry for the delay, I got busy with other commitments. I’ll try to take a look this weekend 😃
Sent from my phone.
On Feb 6, 2017 7:00 PM, “Kevin Partington” notifications@github.com wrote:
@Daniel15 https://github.com/Daniel15 Friendly ping. Are you still interested in writing a PR for this?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eslint/eslint/issues/7837#issuecomment-277887329, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFnHXXv0Y267gXajqz9MwfKuEARNeChks5rZ95ZgaJpZM4LY_iF .
#8115