You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`RuleTester` depends on two functions to run tests: `describe` and `it`. These functions can come from various places:
1356
1357
1357
1358
1. If `RuleTester.describe` and `RuleTester.it` have been set to function values, `RuleTester` will use `RuleTester.describe` and `RuleTester.it` to run tests. You can use this to customize the behavior of `RuleTester` to match a test framework that you're using.
1358
-
1. Otherwise, if `describe` and `it` are present as globals, `RuleTester` will use `global.describe` and `global.it` to run tests. This allows `RuleTester` to work when using frameworks like [Mocha](https://mochajs.org/) without any additional configuration.
1359
-
1. Otherwise, `RuleTester#run` will simply execute all of the tests in sequence, and will throw an error if one of them fails. This means you can simply execute a test file that calls `RuleTester.run` using `node`, without needing a testing framework.
1360
1359
1361
-
`RuleTester#run` calls the `describe` function with two arguments: a string describing the rule, and a callback function. The callback calls the `it` function with a string describing the test case, and a test function. The test function will return successfully if the test passes, and throw an error if the test fails. (Note that this is the standard behavior for test suites when using frameworks like [Mocha](https://mochajs.org/); this information is only relevant if you plan to customize `RuleTester.it` and `RuleTester.describe`.)
1360
+
If `RuleTester.itOnly` has been set to a function value, `RuleTester` will call `RuleTester.itOnly` instead of `RuleTester.it` to run cases with `only: true`. If `RuleTester.itOnly` is not set but `RuleTester.it` has an `only` function property, `RuleTester` will fall back to `RuleTester.it.only`.
1361
+
1362
+
2. Otherwise, if `describe` and `it` are present as globals, `RuleTester` will use `global.describe` and `global.it` to run tests and `global.it.only` to run cases with `only: true`. This allows `RuleTester` to work when using frameworks like [Mocha](https://mochajs.org/) without any additional configuration.
1363
+
3. Otherwise, `RuleTester#run` will simply execute all of the tests in sequence, and will throw an error if one of them fails. This means you can simply execute a test file that calls `RuleTester.run` using `Node.js`, without needing a testing framework.
1364
+
1365
+
`RuleTester#run` calls the `describe` function with two arguments: a string describing the rule, and a callback function. The callback calls the `it` function with a string describing the test case, and a test function. The test function will return successfully if the test passes, and throw an error if the test fails. The signature for `only` is the same as `it`. `RuleTester` calls either `it` or `only` for every case even when some cases have `only: true`, and the test framework is responsible for implementing test case exclusivity. (Note that this is the standard behavior for test suites when using frameworks like [Mocha](https://mochajs.org/); this information is only relevant if you plan to customize `RuleTester.describe`, `RuleTester.it`, or `RuleTester.itOnly`.)
Copy file name to clipboardExpand all lines: docs/developer-guide/unit-tests.md
+20-2Lines changed: 20 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,11 +10,29 @@ This automatically starts Mocha and runs all tests in the `tests` directory. You
10
10
11
11
## Running Individual Tests
12
12
13
-
If you want to quickly run just one test, you can do so by running Mocha directly and passing in the filename. For example:
13
+
If you want to quickly run just one test file, you can do so by running Mocha directly and passing in the filename. For example:
14
14
15
15
npm run test:cli tests/lib/rules/no-wrap-func.js
16
16
17
-
Running individual tests is useful when you're working on a specific bug and iterating on the solution. You should be sure to run `npm test` before submitting a pull request.
17
+
If you want to run just one or a subset of `RuleTester` test cases, add `only: true` to each test case or wrap the test case in `RuleTester.only(...)` to add it automatically:
18
+
19
+
```js
20
+
ruleTester.run("my-rule", myRule, {
21
+
valid: [
22
+
RuleTester.only("const valid = 42;"),
23
+
// Other valid cases
24
+
],
25
+
invalid: [
26
+
{
27
+
code:"const invalid = 42;",
28
+
only:true,
29
+
},
30
+
// Other invalid cases
31
+
]
32
+
})
33
+
```
34
+
35
+
Running individual tests is useful when you're working on a specific bug and iterating on the solution. You should be sure to run `npm test` before submitting a pull request. `npm test` uses Mocha's `--forbid-only` option to prevent `only` tests from passing full test runs.
"Set `RuleTester.itOnly` to use `only` with a custom test framework.\n"+
434
+
"See https://eslint.org/docs/developer-guide/nodejs-api#customizing-ruletester for more."
435
+
);
436
+
}
437
+
if(typeofit==="function"){
438
+
thrownewError("The current test framework does not support exclusive tests with `only`.");
439
+
}
440
+
thrownewError("To use `only`, use RuleTester with a test framework that provides `it.only()` like Mocha.");
441
+
}
442
+
443
+
staticsetitOnly(value){
444
+
this[IT_ONLY]=value;
445
+
}
446
+
403
447
/**
404
448
* Define a rule for one particular run of tests.
405
449
* @param {string} name The name of the rule to define.
0 commit comments