question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RuleTester] Test case validation

See original GitHub issue

The version of ESLint you are using. Node version: v15.3.0 npm version: v6.14.2 Local ESLint version: v7.15.0 (Currently used) Global ESLint version: Not found

The problem you want to solve. The rule tester throws a type error and does not run any test case if the test case property is not a string:

import { RuleTester } from 'eslint';
const ruleTester = new RuleTester();
ruleTester.run("rule-id", rule, {
	valid: [{
		code: [
			'...',
		] // <-- missing .join('\n')
	}],
	invalid: [],
});

which results in an ugly error:

text.replace is not a function 
  at ​​​sanitize​​​ ​./node_modules/eslint/lib/rule-tester/rule-tester.js:207

Relevant code is here. I categorized this as a change as there is only little test case validation but this issue may also be seen as a bug report.

Your take on the correct solution to problem. The rule tester only fails the impacted test case with an instruction on how to correct the error. This may be expanded to other properties than the code property. Currently the rule tester only checks the structure of the scenarios (eg. valid and invalid are present) and for test cases that options are an array and that parser is an absolute path.

Are you willing to submit a pull request to implement this change? Yes

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Dec 10, 2020

Makes sense to me, it would be useful to provide more helpful info. I’ll champion this.

Currently the rule tester only checks the structure of the scenarios (eg. valid and invalid are present) and for test cases that options are an array and that parser is an absolute path.

Some properties are passed through as ESLint config properties and validated as such. For example, try to specify globals: [] or env: [].

These are all RuleTester-specific properties:

https://github.com/eslint/eslint/blob/06b58096975935ec016d96dd5f333f059c270f26/lib/rule-tester/rule-tester.js#L118-L124

options are validated in runRuleForItem, errors is validated in testInvalidTemplate.

Aside from code, we can consider explicit validations for the remaining two properties, but I think we should limit the scope to only those that certainly cause throwing or failing the test if the type is invalid. Other changes would probably need a RFC.

1reaction
mdjermanoviccommented, Dec 14, 2021

Marked as accepted since this is more of a bug than an enhancement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the eslint.RuleTester.setDefaultConfig function in ...
To help you get started, we've selected a few eslint.RuleTester.setDefaultConfig examples, based on popular ways it is used in public projects.
Read more >
Field Validation Table (FVT): A Test Design Technique for ...
This article mainly emphasis on how effectively we can create the test cases using field validation test FVT test design technique that too ......
Read more >
How I built my first custom ESLint rule - Maxime Heckel's Blog
We're lucky, because ESLint comes with its own tool for testing rules called RuleTester . With this tool, we can specify all the...
Read more >
Firewall Rule Tester and Test Reports - Barracuda Campus
Rule Tester – Tests the firewall rule set with the specified connection settings. Also verifies the ... In most cases, the overlap is...
Read more >
Working with Rules - ESLint - Pluggable JavaScript Linter
replacedBy (array) in the case of a deprecated rule, ... ESLint provides the RuleTester utility to make it easy to write tests for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found