]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/developer-guide/unit-tests.md
0c8426fcadf7631ca505538aa1ad3366241d1ae6
[pve-eslint.git] / eslint / docs / src / developer-guide / unit-tests.md
1 ---
2 title: Unit Tests
3 layout: doc
4 eleventyNavigation:
5 key: run the tests
6 parent: developer guide
7 title: Run the Tests
8 order: 3
9
10 ---
11
12 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.
13
14 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:
15
16 ```shell
17 npm test
18 ```
19
20 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.
21
22 ## Running Individual Tests
23
24 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:
25
26 ```shell
27 npm run test:cli tests/lib/rules/no-undef.js
28 ```
29
30 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:
31
32 ```js
33 ruleTester.run("my-rule", myRule, {
34 valid: [
35 RuleTester.only("const valid = 42;"),
36 // Other valid cases
37 ],
38 invalid: [
39 {
40 code: "const invalid = 42;",
41 only: true,
42 },
43 // Other invalid cases
44 ]
45 })
46 ```
47
48 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.
49
50 ## More Control on Unit Testing
51
52 `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.
53
54 The default timeout for tests in `npm test` is 10000ms. You may change the timeout by providing `ESLINT_MOCHA_TIMEOUT` environment variable, for example:
55
56 ```shell
57 ESLINT_MOCHA_TIMEOUT=20000 npm test
58 ```