]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Unit Tests |
2 | ||
3 | Most parts of ESLint have unit tests associated with them. Unit tests are written using [Mocha](https://mochajs.org/) and are required when making contributions to ESLint. You'll find all of the unit tests in the `tests` directory. | |
4 | ||
5 | When you first get the source code, you need to run `npm install` once initially to set ESLint for development. Once you've done that, you can run the tests via: | |
6 | ||
7 | npm test | |
8 | ||
9 | This automatically starts Mocha and runs all tests in the `tests` directory. You need only add yours and it will automatically be picked up when running tests. | |
10 | ||
11 | ## Running Individual Tests | |
12 | ||
609c276f | 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: |
eb39fafa DC |
14 | |
15 | npm run test:cli tests/lib/rules/no-wrap-func.js | |
16 | ||
609c276f TL |
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. | |
eb39fafa DC |
36 | |
37 | ## More Control on Unit Testing | |
38 | ||
39 | `npm run test:cli` is an alias of the Mocha cli in `./node_modules/.bin/mocha`. [Options](https://mochajs.org/#command-line-usage) are available to be provided to help to better control the test to run. |