diff --git a/.eslint-doc-generatorrc.js b/.eslint-doc-generatorrc.js new file mode 100644 index 00000000..64cb4b97 --- /dev/null +++ b/.eslint-doc-generatorrc.js @@ -0,0 +1,14 @@ +import prettier from 'prettier' + +/** @type {import('eslint-doc-generator').GenerateOptions} */ +const config = { + ignoreConfig: ['legacy-all', 'legacy-recommended'], + postprocess: async (content, path) => + prettier.format(content, { + ...(await prettier.resolveConfig(path)), + parser: 'markdown', + }), + ruleDocTitleFormat: 'name', +} + +export default config diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 861df51b..789aac75 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,5 +51,8 @@ jobs: - name: lint code run: pnpm lint:js + - name: lint docs + run: pnpm lint:eslint-docs + - name: test run: pnpm test:ci diff --git a/README.md b/README.md index 8ec1af5b..5a42aa92 100644 --- a/README.md +++ b/README.md @@ -24,20 +24,20 @@ npm install @vitest/eslint-plugin --save-dev Make sure you're running ESLint `v9.0.0` or higher for the latest version of this plugin to work. The following example is how your `eslint.config.js` should be setup for this plugin to work for you. ```js -import vitest from "@vitest/eslint-plugin"; +import vitest from '@vitest/eslint-plugin' export default [ { - files: ["tests/**"], // or any other pattern + files: ['tests/**'], // or any other pattern plugins: { - vitest + vitest, }, rules: { ...vitest.configs.recommended.rules, // you can also use vitest.configs.all.rules to enable all rules - "vitest/max-nested-describe": ["error", { "max": 3 }] // you can also modify rules' behavior using option like this + 'vitest/max-nested-describe': ['error', { max: 3 }], // you can also modify rules' behavior using option like this }, }, -]; +] ``` If you're not using the latest version of ESLint (version `v8.57.0` or lower) you can setup this plugin using the following configuration @@ -73,7 +73,6 @@ If you're using old ESLint configuration, make sure to use legacy key like the f } ``` - #### Enabling with type-testing Vitest ships with an optional [type-testing feature](https://vitest.dev/guide/testing-types), which is disabled by default. @@ -81,21 +80,21 @@ Vitest ships with an optional [type-testing feature](https://vitest.dev/guide/te If you're using this feature, you should also enabled `typecheck` in the settings for this plugin. This ensures that rules like [expect-expect](docs/rules/expect-expect.md) account for type-related assertions in tests. ```js -import vitest from "@vitest/eslint-plugin"; +import vitest from '@vitest/eslint-plugin' export default [ { - files: ["tests/**"], // or any other pattern + files: ['tests/**'], // or any other pattern plugins: { vitest, }, rules: { ...vitest.configs.recommended.rules, }, - settings: { + settings: { vitest: { - typecheck: true - } + typecheck: true, + }, }, languageOptions: { globals: { @@ -109,33 +108,34 @@ export default [ ### Shareable configurations #### Recommended + This plugin exports a recommended configuration that enforces good testing practices. To enable this configuration with `eslint.config.js`, use `vitest.configs.recommended`: ```js -import vitest from "@vitest/eslint-plugin"; +import vitest from '@vitest/eslint-plugin' export default [ { - files: ["tests/**"], // or any other pattern - ...vitest.configs.recommended, - } + files: ['tests/**'], // or any other pattern + ...vitest.configs.recommended, + }, ] ``` - #### All + If you want to enable all rules instead of only some you can do so by adding the all configuration to your `eslint.config.js` config file: ```js -import vitest from "@vitest/eslint-plugin"; +import vitest from '@vitest/eslint-plugin' export default [ { - files: ["tests/**"], // or any other pattern - ...vitest.configs.all, - } + files: ['tests/**'], // or any other pattern + ...vitest.configs.all, + }, ] ``` @@ -143,90 +143,89 @@ export default [ -πŸ’Ό Configurations enabled in.\ -⚠️ Configurations set to warn in.\ -🌐 Set in the `all` configuration.\ -βœ… Set in the `recommended` configuration.\ πŸ”§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\ πŸ’‘ Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).\ +πŸ’­ Requires [type information](https://typescript-eslint.io/linting/typed-linting).\ ❌ Deprecated. -| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | ⚠️ | πŸ”§ | πŸ’‘ | ❌ | -| :----------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- | :- | :- | :- | -| [consistent-test-filename](docs/rules/consistent-test-filename.md) | require .spec test file pattern | | 🌐 | | | | -| [consistent-test-it](docs/rules/consistent-test-it.md) | enforce using test or it but not both | | 🌐 | πŸ”§ | | | -| [consistent-vitest-vi](docs/rules/consistent-vitest-vi.md) | enforce using vitest or vi but not both | | 🌐 | πŸ”§ | | | -| [expect-expect](docs/rules/expect-expect.md) | enforce having expectation in test body | βœ… | 🌐 | | | | -| [max-expects](docs/rules/max-expects.md) | enforce a maximum number of expect per test | | 🌐 | | | | -| [max-nested-describe](docs/rules/max-nested-describe.md) | require describe block to be less than set max value or default value | | 🌐 | | | | -| [no-alias-methods](docs/rules/no-alias-methods.md) | disallow alias methods | | 🌐 | πŸ”§ | | | -| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | disallow commented out tests | βœ… | 🌐 | | | | -| [no-conditional-expect](docs/rules/no-conditional-expect.md) | disallow conditional expects | | 🌐 | | | | -| [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | disallow conditional tests | | 🌐 | | | | -| [no-conditional-tests](docs/rules/no-conditional-tests.md) | disallow conditional tests | | 🌐 | | | | -| [no-disabled-tests](docs/rules/no-disabled-tests.md) | disallow disabled tests | | 🌐 | | | | -| [no-done-callback](docs/rules/no-done-callback.md) | disallow using a callback in asynchronous tests and hooks | | 🌐 | | πŸ’‘ | ❌ | -| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | disallow duplicate hooks and teardown hooks | | 🌐 | | | | -| [no-focused-tests](docs/rules/no-focused-tests.md) | disallow focused tests | | 🌐 | πŸ”§ | | | -| [no-hooks](docs/rules/no-hooks.md) | disallow setup and teardown hooks | | 🌐 | | | | -| [no-identical-title](docs/rules/no-identical-title.md) | disallow identical titles | βœ… | 🌐 | πŸ”§ | | | -| [no-import-node-test](docs/rules/no-import-node-test.md) | disallow importing `node:test` | βœ… | 🌐 | πŸ”§ | | | -| [no-importing-vitest-globals](docs/rules/no-importing-vitest-globals.md) | disallow importing Vitest globals | | 🌐 | πŸ”§ | | | -| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | disallow string interpolation in snapshots | | 🌐 | πŸ”§ | | | -| [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | 🌐 | | | | -| [no-mocks-import](docs/rules/no-mocks-import.md) | disallow importing from __mocks__ directory | | 🌐 | | | | -| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | disallow the use of certain matchers | | 🌐 | | | | -| [no-restricted-vi-methods](docs/rules/no-restricted-vi-methods.md) | disallow specific `vi.` methods | | 🌐 | | | | -| [no-standalone-expect](docs/rules/no-standalone-expect.md) | disallow using `expect` outside of `it` or `test` blocks | | 🌐 | | | | -| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Disallow using the `f` and `x` prefixes in favour of `.only` and `.skip` | | 🌐 | πŸ”§ | | | -| [no-test-return-statement](docs/rules/no-test-return-statement.md) | disallow return statements in tests | | 🌐 | | | | -| [padding-around-after-all-blocks](docs/rules/padding-around-after-all-blocks.md) | enforce padding around `afterAll` blocks | | 🌐 | πŸ”§ | | | -| [padding-around-after-each-blocks](docs/rules/padding-around-after-each-blocks.md) | enforce padding around `afterEach` blocks | | 🌐 | πŸ”§ | | | -| [padding-around-all](docs/rules/padding-around-all.md) | enforce padding around vitest functions | | 🌐 | πŸ”§ | | | -| [padding-around-before-all-blocks](docs/rules/padding-around-before-all-blocks.md) | enforce padding around `beforeAll` blocks | | 🌐 | πŸ”§ | | | -| [padding-around-before-each-blocks](docs/rules/padding-around-before-each-blocks.md) | enforce padding around `beforeEach` blocks | | 🌐 | πŸ”§ | | | -| [padding-around-describe-blocks](docs/rules/padding-around-describe-blocks.md) | enforce padding around `describe` blocks | | 🌐 | πŸ”§ | | | -| [padding-around-expect-groups](docs/rules/padding-around-expect-groups.md) | enforce padding around `expect` groups | | 🌐 | πŸ”§ | | | -| [prefer-called-with](docs/rules/prefer-called-with.md) | enforce using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | 🌐 | πŸ”§ | | | -| [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | enforce using the built-in comparison matchers | | 🌐 | πŸ”§ | | | -| [prefer-describe-function-title](docs/rules/prefer-describe-function-title.md) | prefer using a function as a describe name over an equivalent string | | 🌐 | πŸ”§ | | | -| [prefer-each](docs/rules/prefer-each.md) | enforce using `each` rather than manual loops | | 🌐 | | | | -| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | enforce using the built-in quality matchers | | 🌐 | | πŸ’‘ | | -| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | enforce using expect assertions instead of callbacks | | 🌐 | | πŸ’‘ | | -| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | enforce using `expect().resolves` over `expect(await ...)` syntax | | 🌐 | πŸ”§ | | | -| [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | enforce having hooks in consistent order | | 🌐 | | | | -| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | enforce having hooks before any test cases | | 🌐 | | | | -| [prefer-importing-vitest-globals](docs/rules/prefer-importing-vitest-globals.md) | enforce importing Vitest globals | | 🌐 | πŸ”§ | | | -| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | enforce lowercase titles | | 🌐 | πŸ”§ | | | -| [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | enforce mock resolved/rejected shorthands for promises | | 🌐 | πŸ”§ | | | -| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | enforce including a hint with external snapshots | | 🌐 | | | | -| [prefer-spy-on](docs/rules/prefer-spy-on.md) | enforce using `vi.spyOn` | | 🌐 | πŸ”§ | | | -| [prefer-strict-boolean-matchers](docs/rules/prefer-strict-boolean-matchers.md) | enforce using `toBe(true)` and `toBe(false)` over matchers that coerce types to boolean | | 🌐 | πŸ”§ | | | -| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | enforce strict equal over equal | | 🌐 | | πŸ’‘ | | -| [prefer-to-be](docs/rules/prefer-to-be.md) | enforce using toBe() | | 🌐 | πŸ”§ | | | -| [prefer-to-be-falsy](docs/rules/prefer-to-be-falsy.md) | enforce using toBeFalsy() | | 🌐 | πŸ”§ | | | -| [prefer-to-be-object](docs/rules/prefer-to-be-object.md) | enforce using toBeObject() | | 🌐 | πŸ”§ | | | -| [prefer-to-be-truthy](docs/rules/prefer-to-be-truthy.md) | enforce using `toBeTruthy` | | 🌐 | πŸ”§ | | | -| [prefer-to-contain](docs/rules/prefer-to-contain.md) | enforce using toContain() | | 🌐 | πŸ”§ | | | -| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | enforce using toHaveLength() | | 🌐 | πŸ”§ | | | -| [prefer-todo](docs/rules/prefer-todo.md) | enforce using `test.todo` | | 🌐 | πŸ”§ | | | -| [prefer-vi-mocked](docs/rules/prefer-vi-mocked.md) | Prefer `vi.mocked()` over `fn as Mock` | | 🌐 | πŸ”§ | | | -| [require-hook](docs/rules/require-hook.md) | require setup and teardown to be within a hook | | 🌐 | | | | -| [require-local-test-context-for-concurrent-snapshots](docs/rules/require-local-test-context-for-concurrent-snapshots.md) | require local Test Context for concurrent snapshot tests | βœ… | 🌐 | | | | -| [require-mock-type-parameters](docs/rules/require-mock-type-parameters.md) | enforce using type parameters with vitest mock functions | | 🌐 | | | | -| [require-to-throw-message](docs/rules/require-to-throw-message.md) | require toThrow() to be called with an error message | | 🌐 | | | | -| [require-top-level-describe](docs/rules/require-top-level-describe.md) | enforce that all tests are in a top-level describe | | 🌐 | | | | -| [valid-describe-callback](docs/rules/valid-describe-callback.md) | enforce valid describe callback | βœ… | 🌐 | | | | -| [valid-expect](docs/rules/valid-expect.md) | enforce valid `expect()` usage | βœ… | 🌐 | πŸ”§ | | | -| [valid-title](docs/rules/valid-title.md) | enforce valid titles | βœ… | 🌐 | πŸ”§ | | | -| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | require promises that have expectations in their chain to be valid | | 🌐 | | | | +| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ”§ | πŸ’‘ | πŸ’­ | ❌ | +| :----------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------- | :-- | :-- | :-- | :-- | +| [consistent-test-filename](docs/rules/consistent-test-filename.md) | require .spec test file pattern | | | | | +| [consistent-test-it](docs/rules/consistent-test-it.md) | enforce using test or it but not both | πŸ”§ | | | | +| [consistent-vitest-vi](docs/rules/consistent-vitest-vi.md) | enforce using vitest or vi but not both | πŸ”§ | | | | +| [expect-expect](docs/rules/expect-expect.md) | enforce having expectation in test body | | | | | +| [max-expects](docs/rules/max-expects.md) | enforce a maximum number of expect per test | | | | | +| [max-nested-describe](docs/rules/max-nested-describe.md) | require describe block to be less than set max value or default value | | | | | +| [no-alias-methods](docs/rules/no-alias-methods.md) | disallow alias methods | πŸ”§ | | | | +| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | disallow commented out tests | | | | | +| [no-conditional-expect](docs/rules/no-conditional-expect.md) | disallow conditional expects | | | | | +| [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | disallow conditional tests | | | | | +| [no-conditional-tests](docs/rules/no-conditional-tests.md) | disallow conditional tests | | | | | +| [no-disabled-tests](docs/rules/no-disabled-tests.md) | disallow disabled tests | | | | | +| [no-done-callback](docs/rules/no-done-callback.md) | disallow using a callback in asynchronous tests and hooks | | πŸ’‘ | | ❌ | +| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | disallow duplicate hooks and teardown hooks | | | | | +| [no-focused-tests](docs/rules/no-focused-tests.md) | disallow focused tests | πŸ”§ | | | | +| [no-hooks](docs/rules/no-hooks.md) | disallow setup and teardown hooks | | | | | +| [no-identical-title](docs/rules/no-identical-title.md) | disallow identical titles | πŸ”§ | | | | +| [no-import-node-test](docs/rules/no-import-node-test.md) | disallow importing `node:test` | πŸ”§ | | | | +| [no-importing-vitest-globals](docs/rules/no-importing-vitest-globals.md) | disallow importing Vitest globals | πŸ”§ | | | | +| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | disallow string interpolation in snapshots | πŸ”§ | | | | +| [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | | | | +| [no-mocks-import](docs/rules/no-mocks-import.md) | disallow importing from **mocks** directory | | | | | +| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | disallow the use of certain matchers | | | | | +| [no-restricted-vi-methods](docs/rules/no-restricted-vi-methods.md) | disallow specific `vi.` methods | | | | | +| [no-standalone-expect](docs/rules/no-standalone-expect.md) | disallow using `expect` outside of `it` or `test` blocks | | | | | +| [no-test-prefixes](docs/rules/no-test-prefixes.md) | disallow using the `f` and `x` prefixes in favour of `.only` and `.skip` | πŸ”§ | | | | +| [no-test-return-statement](docs/rules/no-test-return-statement.md) | disallow return statements in tests | | | | | +| [padding-around-after-all-blocks](docs/rules/padding-around-after-all-blocks.md) | Enforce padding around `afterAll` blocks | πŸ”§ | | | | +| [padding-around-after-each-blocks](docs/rules/padding-around-after-each-blocks.md) | Enforce padding around `afterEach` blocks | πŸ”§ | | | | +| [padding-around-all](docs/rules/padding-around-all.md) | Enforce padding around vitest functions | πŸ”§ | | | | +| [padding-around-before-all-blocks](docs/rules/padding-around-before-all-blocks.md) | Enforce padding around `beforeAll` blocks | πŸ”§ | | | | +| [padding-around-before-each-blocks](docs/rules/padding-around-before-each-blocks.md) | Enforce padding around `beforeEach` blocks | πŸ”§ | | | | +| [padding-around-describe-blocks](docs/rules/padding-around-describe-blocks.md) | Enforce padding around `describe` blocks | πŸ”§ | | | | +| [padding-around-expect-groups](docs/rules/padding-around-expect-groups.md) | Enforce padding around `expect` groups | πŸ”§ | | | | +| [padding-around-test-blocks](docs/rules/padding-around-test-blocks.md) | Enforce padding around `test` blocks | πŸ”§ | | | | +| [prefer-called-times](docs/rules/prefer-called-times.md) | enforce using `toBeCalledTimes(1)` or `toHaveBeenCalledTimes(1)` | πŸ”§ | | | | +| [prefer-called-with](docs/rules/prefer-called-with.md) | enforce using `toBeCalledWith()` or `toHaveBeenCalledWith()` | πŸ”§ | | | | +| [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | enforce using the built-in comparison matchers | πŸ”§ | | | | +| [prefer-describe-function-title](docs/rules/prefer-describe-function-title.md) | enforce using a function as a describe title over an equivalent string | πŸ”§ | | | | +| [prefer-each](docs/rules/prefer-each.md) | enforce using `each` rather than manual loops | | | | | +| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | enforce using the built-in quality matchers | | πŸ’‘ | | | +| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | enforce using expect assertions instead of callbacks | | πŸ’‘ | | | +| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | enforce using `expect().resolves` over `expect(await ...)` syntax | πŸ”§ | | | | +| [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | enforce having hooks in consistent order | | | | | +| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | enforce having hooks before any test cases | | | | | +| [prefer-importing-vitest-globals](docs/rules/prefer-importing-vitest-globals.md) | enforce importing Vitest globals | πŸ”§ | | | | +| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | enforce lowercase titles | πŸ”§ | | | | +| [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | enforce mock resolved/rejected shorthands for promises | πŸ”§ | | | | +| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | enforce including a hint with external snapshots | | | | | +| [prefer-spy-on](docs/rules/prefer-spy-on.md) | enforce using `vi.spyOn` | πŸ”§ | | | | +| [prefer-strict-boolean-matchers](docs/rules/prefer-strict-boolean-matchers.md) | enforce using `toBe(true)` and `toBe(false)` over matchers that coerce types to boolean | πŸ”§ | | | | +| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | enforce strict equal over equal | | πŸ’‘ | | | +| [prefer-to-be](docs/rules/prefer-to-be.md) | enforce using toBe() | πŸ”§ | | | | +| [prefer-to-be-falsy](docs/rules/prefer-to-be-falsy.md) | enforce using toBeFalsy() | πŸ”§ | | | | +| [prefer-to-be-object](docs/rules/prefer-to-be-object.md) | enforce using toBeObject() | πŸ”§ | | | | +| [prefer-to-be-truthy](docs/rules/prefer-to-be-truthy.md) | enforce using `toBeTruthy` | πŸ”§ | | | | +| [prefer-to-contain](docs/rules/prefer-to-contain.md) | enforce using toContain() | πŸ”§ | | | | +| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | enforce using toHaveLength() | πŸ”§ | | | | +| [prefer-todo](docs/rules/prefer-todo.md) | enforce using `test.todo` | πŸ”§ | | | | +| [prefer-vi-mocked](docs/rules/prefer-vi-mocked.md) | require `vi.mocked()` over `fn as Mock` | πŸ”§ | | πŸ’­ | | +| [require-hook](docs/rules/require-hook.md) | require setup and teardown to be within a hook | | | | | +| [require-local-test-context-for-concurrent-snapshots](docs/rules/require-local-test-context-for-concurrent-snapshots.md) | require local Test Context for concurrent snapshot tests | | | | | +| [require-mock-type-parameters](docs/rules/require-mock-type-parameters.md) | enforce using type parameters with vitest mock functions | πŸ”§ | | | | +| [require-to-throw-message](docs/rules/require-to-throw-message.md) | require toThrow() to be called with an error message | | | | | +| [require-top-level-describe](docs/rules/require-top-level-describe.md) | enforce that all tests are in a top-level describe | | | | | +| [valid-describe-callback](docs/rules/valid-describe-callback.md) | enforce valid describe callback | | | | | +| [valid-expect](docs/rules/valid-expect.md) | enforce valid `expect()` usage | πŸ”§ | | | | +| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | require promises that have expectations in their chain to be valid | | | | | +| [valid-title](docs/rules/valid-title.md) | enforce valid titles | πŸ”§ | | | | #### Credits - [eslint-plugin-jest](https://github.com/jest-community/eslint-plugin-jest) - Most of the rules in this plugin are essentially ports of Jest plugin rules with minor modifications + Most of the rules in this plugin are essentially ports of Jest plugin rules with minor modifications ### Licence diff --git a/docs/rules/consistent-test-filename.md b/docs/rules/consistent-test-filename.md index 477b96e6..1c9fda5b 100644 --- a/docs/rules/consistent-test-filename.md +++ b/docs/rules/consistent-test-filename.md @@ -1,6 +1,4 @@ -# Require .spec test file pattern (`vitest/consistent-test-filename`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# consistent-test-filename diff --git a/docs/rules/consistent-test-it.md b/docs/rules/consistent-test-it.md index 1036aa28..b66dd7ed 100644 --- a/docs/rules/consistent-test-it.md +++ b/docs/rules/consistent-test-it.md @@ -1,6 +1,4 @@ -# Enforce using test or it but not both (`vitest/consistent-test-it`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# consistent-test-it πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -12,11 +10,11 @@ Examples of **incorrect** code for this rule: ```js test('it works', () => { - // ... + // ... }) it('it works', () => { - // ... + // ... }) ``` @@ -24,13 +22,13 @@ Examples of **correct** code for this rule: ```js test('it works', () => { - // ... + // ... }) ``` ```js test('it works', () => { - // ... + // ... }) ``` @@ -38,22 +36,16 @@ test('it works', () => { ```json { - "type":"object", - "properties":{ - "fn":{ - "enum":[ - "it", - "test" - ] - }, - "withinDescribe":{ - "enum":[ - "it", - "test" - ] - } - }, - "additionalProperties":false + "type": "object", + "properties": { + "fn": { + "enum": ["it", "test"] + }, + "withinDescribe": { + "enum": ["it", "test"] + } + }, + "additionalProperties": false } ``` @@ -68,23 +60,25 @@ Decides whether to prefer `test` or `it` when used within a `describe` block. ```js /*eslint vitest/consistent-test-it: ["error", {"fn": "test"}]*/ -test('it works', () => { // <-- Valid - // ... +test('it works', () => { + // <-- Valid + // ... }) -test.only('it works', () => { // <-- Valid - // ... +test.only('it works', () => { + // <-- Valid + // ... }) - -it('it works', () => { // <-- Invalid - // ... +it('it works', () => { + // <-- Invalid + // ... }) -it.only('it works', () => { // <-- Invalid - // ... +it.only('it works', () => { + // <-- Invalid + // ... }) ``` The default configuration is top level `test` and all tests nested with `describe` to use `it`. - diff --git a/docs/rules/consistent-vitest-vi.md b/docs/rules/consistent-vitest-vi.md index cb7b06ec..cc460a84 100644 --- a/docs/rules/consistent-vitest-vi.md +++ b/docs/rules/consistent-vitest-vi.md @@ -1,6 +1,4 @@ -# Enforce using vitest or vi but not both (`vitest/consistent-vitest-vi`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# consistent-vitest-vi πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -11,39 +9,36 @@ Examples of **incorrect** code for this rule: ```js -vitest.mock('./src/calculator.ts', { spy: true }); +vitest.mock('./src/calculator.ts', { spy: true }) -vi.stubEnv('NODE_ENV', 'production'); +vi.stubEnv('NODE_ENV', 'production') ``` Examples of **correct** code for this rule: ```js -vi.mock('./src/calculator.ts', { spy: true }); +vi.mock('./src/calculator.ts', { spy: true }) -vi.stubEnv('NODE_ENV', 'production'); +vi.stubEnv('NODE_ENV', 'production') ``` ```js -vitest.mock('./src/calculator.ts', { spy: true }); +vitest.mock('./src/calculator.ts', { spy: true }) -vitest.stubEnv('NODE_ENV', 'production'); +vitest.stubEnv('NODE_ENV', 'production') ``` ## Options ```json { - "type":"object", - "properties":{ - "fn":{ - "enum":[ - "vi", - "vitest" - ] - } - }, - "additionalProperties":false + "type": "object", + "properties": { + "fn": { + "enum": ["vi", "vitest"] + } + }, + "additionalProperties": false } ``` diff --git a/docs/rules/expect-expect.md b/docs/rules/expect-expect.md index 0062825c..6b4e4d5d 100644 --- a/docs/rules/expect-expect.md +++ b/docs/rules/expect-expect.md @@ -1,12 +1,7 @@ -# Enforce having expectation in test body (`vitest/expect-expect`) - -πŸ’Ό This rule is enabled in the βœ… `recommended` config. - -⚠️ This rule _warns_ in the 🌐 `all` config. +# expect-expect - ## Rule Details This rule aims to enforce having at least one expectation in test body to ensure that the test is actually testing something. @@ -15,7 +10,7 @@ Examples of **incorrect** code for this rule: ```js test('myLogic', () => { - console.log('myLogic') + console.log('myLogic') }) test('myLogic', () => {}) @@ -52,20 +47,17 @@ If you're using Vitest's [type-testing feature](https://vitest.dev/guide/testing An array of strings that are the names of the functions that are used for assertions. Function names can also be wildcard patterns like `expect*`,`function.**.expect` or `expect.anything`. - The following is an example of correct code for this rule with the option `assertFunctionNames`: ```js import CheckForMe from 'check-for-me' test('myLogic', () => { - expect("myLogic").toBe("myOutput") + expect('myLogic').toBe('myOutput') }) ``` - ### `additionalTestBlockFunctions` - ```json { "rules": { @@ -85,8 +77,8 @@ The following is an example of correct code for this rule with the option `addit import CheckForMe from 'check-for-me' checkForMe('myLogic', () => { checkForMe('myLogic', () => { - const actual = myLogic() - expect(actual).toBe(true) + const actual = myLogic() + expect(actual).toBe(true) }) }) ``` diff --git a/docs/rules/max-expects.md b/docs/rules/max-expects.md index 8c4f97e0..5d36ef47 100644 --- a/docs/rules/max-expects.md +++ b/docs/rules/max-expects.md @@ -1,6 +1,4 @@ -# Enforce a maximum number of expect per test (`vitest/max-expects`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# max-expects diff --git a/docs/rules/max-nested-describe.md b/docs/rules/max-nested-describe.md index 1a436b08..9bd19b6e 100644 --- a/docs/rules/max-nested-describe.md +++ b/docs/rules/max-nested-describe.md @@ -1,6 +1,4 @@ -# Require describe block to be less than set max value or default value (`vitest/max-nested-describe`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# max-nested-describe @@ -10,9 +8,9 @@ Examples of **incorrect** code for this rule with `max: 1`: ```js describe('outer', () => { - describe('inner', () => { - // ... - }) + describe('inner', () => { + // ... + }) }) ``` @@ -20,7 +18,7 @@ Examples of **correct** code for this rule: ```js describe('inner', () => { - // ... + // ... }) ``` @@ -32,6 +30,6 @@ Maximum number of nested `describe` blocks. ```js { - max: number + max: number } ``` diff --git a/docs/rules/no-alias-methods.md b/docs/rules/no-alias-methods.md index 65768ad7..a84ed79d 100644 --- a/docs/rules/no-alias-methods.md +++ b/docs/rules/no-alias-methods.md @@ -1,12 +1,9 @@ -# Disallow alias methods (`vitest/no-alias-methods`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-alias-methods πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). - ## Rule Details This rule disallows alias methods and forces the use of the original method. @@ -21,7 +18,6 @@ expect(a).toBeCalled() expect(a).toBeCalledTimes(1) ``` - Examples of **correct** code for this rule: ```js diff --git a/docs/rules/no-commented-out-tests.md b/docs/rules/no-commented-out-tests.md index 7e189ada..1a6f4dea 100644 --- a/docs/rules/no-commented-out-tests.md +++ b/docs/rules/no-commented-out-tests.md @@ -1,8 +1,4 @@ -# Disallow commented out tests (`vitest/no-commented-out-tests`) - -πŸ’Ό This rule is enabled in the βœ… `recommended` config. - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-commented-out-tests diff --git a/docs/rules/no-conditional-expect.md b/docs/rules/no-conditional-expect.md index c2697036..f9454ec3 100644 --- a/docs/rules/no-conditional-expect.md +++ b/docs/rules/no-conditional-expect.md @@ -1,6 +1,4 @@ -# Disallow conditional expects (`vitest/no-conditional-expect`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-conditional-expect @@ -13,7 +11,7 @@ Examples of **incorrect** code for this rule: ```ts test('foo', () => { if (true) { - expect(1).toBe(1) + expect(1).toBe(1) } }) ``` diff --git a/docs/rules/no-conditional-in-test.md b/docs/rules/no-conditional-in-test.md index bf3e15da..b096e049 100644 --- a/docs/rules/no-conditional-in-test.md +++ b/docs/rules/no-conditional-in-test.md @@ -1,8 +1,7 @@ -# Disallow conditional tests (`vitest/no-conditional-in-test`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-conditional-in-test + ### Rule Details This rule aims to prevent conditional tests. @@ -12,7 +11,7 @@ Examples of **incorrect** code for this rule: ```js test('my test', () => { if (true) { - doTheThing() + doTheThing() } }) ``` @@ -21,6 +20,6 @@ Examples of **correct** code for this rule: ```js test('my test', () => { - expect(true).toBe(true) + expect(true).toBe(true) }) ``` diff --git a/docs/rules/no-conditional-tests.md b/docs/rules/no-conditional-tests.md index 4337a2b3..21414054 100644 --- a/docs/rules/no-conditional-tests.md +++ b/docs/rules/no-conditional-tests.md @@ -1,6 +1,4 @@ -# Disallow conditional tests (`vitest/no-conditional-tests`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-conditional-tests @@ -10,11 +8,11 @@ Examples of **incorrect** code for this rule: ```js describe('my tests', () => { - if (true) { - it('is awesome', () => { - doTheThing() - }) - } + if (true) { + it('is awesome', () => { + doTheThing() + }) + } }) ``` @@ -22,8 +20,8 @@ Examples of **correct** code for this rule: ```js describe('my tests', () => { - it('is awesome', () => { - doTheThing() - }) + it('is awesome', () => { + doTheThing() + }) }) ``` diff --git a/docs/rules/no-disabled-tests.md b/docs/rules/no-disabled-tests.md index 6d6a09bb..517738c1 100644 --- a/docs/rules/no-disabled-tests.md +++ b/docs/rules/no-disabled-tests.md @@ -1,6 +1,4 @@ -# Disallow disabled tests (`vitest/no-disabled-tests`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-disabled-tests diff --git a/docs/rules/no-done-callback.md b/docs/rules/no-done-callback.md index 02272bfc..d3e99e54 100644 --- a/docs/rules/no-done-callback.md +++ b/docs/rules/no-done-callback.md @@ -1,9 +1,7 @@ -# Disallow using a callback in asynchronous tests and hooks (`vitest/no-done-callback`) +# no-done-callback ❌ This rule is deprecated. -⚠️ This rule _warns_ in the 🌐 `all` config. - πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). @@ -25,13 +23,13 @@ test('foo', (done) => { test('foo', (done) => { setTimeout(() => { - done() + done() }, 1000) }) test('foo', (done) => { setTimeout(() => { - done() + done() }, 1000) }) ``` @@ -48,28 +46,32 @@ test('foo', async () => { }) test('foo', async () => { - await new Promise((resolve) => setTimeout(() => { - resolve() - }, 1000)) + await new Promise((resolve) => + setTimeout(() => { + resolve() + }, 1000), + ) }) test('foo', async () => { - await new Promise((resolve) => setTimeout(() => { - resolve() - }, 1000)) + await new Promise((resolve) => + setTimeout(() => { + resolve() + }, 1000), + ) }) test.concurrent('foo', ({ expect }) => { - expect(1).toMatchSnapshot(); -}); + expect(1).toMatchSnapshot() +}) test.concurrent('foo', (context) => { - context.expect(1).toBe(1); -}); + context.expect(1).toBe(1) +}) describe.concurrent('foo', () => { test('foo', ({ expect }) => { - expect(1).toBe(1); - }); -}); + expect(1).toBe(1) + }) +}) ``` diff --git a/docs/rules/no-duplicate-hooks.md b/docs/rules/no-duplicate-hooks.md index aa5e0163..4013f2ff 100644 --- a/docs/rules/no-duplicate-hooks.md +++ b/docs/rules/no-duplicate-hooks.md @@ -1,6 +1,4 @@ -# Disallow duplicate hooks and teardown hooks (`vitest/no-duplicate-hooks`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-duplicate-hooks @@ -11,7 +9,6 @@ This rule aims to prevent duplicate hooks and teardown hooks. Examples of **incorrect** code for this rule: ```ts - test('foo', () => { beforeEach(() => {}) beforeEach(() => {}) // duplicate beforeEach @@ -25,4 +22,3 @@ test('foo', () => { beforeEach(() => {}) }) ``` - diff --git a/docs/rules/no-focused-tests.md b/docs/rules/no-focused-tests.md index 34b84843..405d4164 100644 --- a/docs/rules/no-focused-tests.md +++ b/docs/rules/no-focused-tests.md @@ -1,6 +1,4 @@ -# Disallow focused tests (`vitest/no-focused-tests`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-focused-tests πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -12,11 +10,11 @@ Examples of **incorrect** code for this rule: ```js it.only('test', () => { - // ... + // ... }) test.only('it', () => { - // ... + // ... }) ``` @@ -24,11 +22,11 @@ Examples of **correct** code for this rule: ```js it('test', () => { - // ... + // ... }) test('it', () => { - /* ... */ + /* ... */ }) ``` @@ -47,8 +45,8 @@ export default [ }, rules: { ...vitest.configs.recommended.all, - 'vitest/no-focused-tests': ['error', { 'fixable': false }] - } - } + 'vitest/no-focused-tests': ['error', { fixable: false }], + }, + }, ] ``` diff --git a/docs/rules/no-hooks.md b/docs/rules/no-hooks.md index e4f9a1d7..06632859 100644 --- a/docs/rules/no-hooks.md +++ b/docs/rules/no-hooks.md @@ -1,6 +1,4 @@ -# Disallow setup and teardown hooks (`vitest/no-hooks`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-hooks @@ -27,36 +25,36 @@ function setupBar(options) { } describe('foo', () => { - let foo; + let foo beforeEach(() => { - foo = setupFoo(); - }); + foo = setupFoo() + }) afterEach(() => { - foo = null; - }); + foo = null + }) it('does something', () => { - expect(foo.doesSomething()).toBe(true); - }); + expect(foo.doesSomething()).toBe(true) + }) describe('with bar', () => { - let bar; + let bar beforeEach(() => { - bar = setupBar(); - }); + bar = setupBar() + }) afterEach(() => { - bar = null; - }); + bar = null + }) it('does something with bar', () => { - expect(foo.doesSomething(bar)).toBe(true); - }); - }); -}); + expect(foo.doesSomething(bar)).toBe(true) + }) + }) +}) ``` Examples of **correct** code for this rule: @@ -74,16 +72,16 @@ function setupBar(options) { describe('foo', () => { it('does something', () => { - const foo = setupFoo(); - expect(foo.doesSomething()).toBe(true); - }); + const foo = setupFoo() + expect(foo.doesSomething()).toBe(true) + }) it('does something with bar', () => { - const foo = setupFoo(); - const bar = setupBar(); - expect(foo.doesSomething(bar)).toBe(true); - }); -}); + const foo = setupFoo() + const bar = setupBar() + expect(foo.doesSomething(bar)).toBe(true) + }) +}) ``` ## Options @@ -121,23 +119,23 @@ function setupFoo(options) { /* ... */ } -let foo; +let foo beforeEach(() => { - foo = setupFoo(); -}); + foo = setupFoo() +}) afterEach(() => { - vi.resetModules(); -}); + vi.resetModules() +}) test('foo does this', () => { // ... -}); +}) test('foo does that', () => { // ... -}); +}) ``` Examples of **correct** code for the `{ "allow": ["afterEach"] }` option: @@ -150,16 +148,16 @@ function setupFoo(options) { } afterEach(() => { - vi.resetModules(); -}); + vi.resetModules() +}) test('foo does this', () => { - const foo = setupFoo(); + const foo = setupFoo() // ... -}); +}) test('foo does that', () => { - const foo = setupFoo(); + const foo = setupFoo() // ... -}); -``` \ No newline at end of file +}) +``` diff --git a/docs/rules/no-identical-title.md b/docs/rules/no-identical-title.md index a9705cf8..0fa6e86c 100644 --- a/docs/rules/no-identical-title.md +++ b/docs/rules/no-identical-title.md @@ -1,8 +1,4 @@ -# Disallow identical titles (`vitest/no-identical-title`) - -πŸ’Ό This rule is enabled in the βœ… `recommended` config. - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-identical-title πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -14,11 +10,11 @@ Examples of **incorrect** code for this rule: ```js it('is awesome', () => { - /* ... */ + /* ... */ }) it('is awesome', () => { - /* ... */ + /* ... */ }) ``` @@ -26,10 +22,10 @@ Examples of **correct** code for this rule: ```js it('is awesome', () => { - /* ... */ + /* ... */ }) it('is very awesome', () => { - /* ... */ + /* ... */ }) ``` diff --git a/docs/rules/no-import-node-test.md b/docs/rules/no-import-node-test.md index 79f27587..355c6c57 100644 --- a/docs/rules/no-import-node-test.md +++ b/docs/rules/no-import-node-test.md @@ -1,8 +1,4 @@ -# Disallow importing `node:test` (`vitest/no-import-node-test`) - -πŸ’Ό This rule is enabled in the βœ… `recommended` config. - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-import-node-test πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-importing-vitest-globals.md b/docs/rules/no-importing-vitest-globals.md index 80725690..6dd640ef 100644 --- a/docs/rules/no-importing-vitest-globals.md +++ b/docs/rules/no-importing-vitest-globals.md @@ -1,4 +1,8 @@ -# Disallow importing Vitest globals (`vitest/no-importing-vitest-globals`) +# no-importing-vitest-globals + +πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). + + ⚠️ This rule _warns_ in the 🌐 `all` config. diff --git a/docs/rules/no-interpolation-in-snapshots.md b/docs/rules/no-interpolation-in-snapshots.md index d92cb269..027c4b0b 100644 --- a/docs/rules/no-interpolation-in-snapshots.md +++ b/docs/rules/no-interpolation-in-snapshots.md @@ -1,6 +1,4 @@ -# Disallow string interpolation in snapshots (`vitest/no-interpolation-in-snapshots`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-interpolation-in-snapshots πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -23,4 +21,4 @@ expect('foo').toMatchSnapshot(`foo ${bar}`) expect('foo').toMatchSnapshot() expect('foo').toMatchSnapshot('foo') expect('foo').toMatchSnapshot(bar) -``` \ No newline at end of file +``` diff --git a/docs/rules/no-large-snapshots.md b/docs/rules/no-large-snapshots.md index aaad8016..1dfae02d 100644 --- a/docs/rules/no-large-snapshots.md +++ b/docs/rules/no-large-snapshots.md @@ -1,6 +1,4 @@ -# Disallow large snapshots (`vitest/no-large-snapshots`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-large-snapshots @@ -8,26 +6,25 @@ This rule aims to prevent large snapshots. - ### Options This rule accepts an object with the following properties: -* `maxSize` (default: `50`): The maximum size of a snapshot. -* `inlineMaxSize` (default: `0`): The maximum size of a snapshot when it is inline. -* `allowedSnapshots` (default: `[]`): The list of allowed snapshots. +- `maxSize` (default: `50`): The maximum size of a snapshot. +- `inlineMaxSize` (default: `0`): The maximum size of a snapshot when it is inline. +- `allowedSnapshots` (default: `[]`): The list of allowed snapshots. ### For example: ```json { "vitest/no-large-snapshots": [ - "error", - { - "maxSize": 50, - "inlineMaxSize": 0, - "allowedSnapshots": [] - } + "error", + { + "maxSize": 50, + "inlineMaxSize": 0, + "allowedSnapshots": [] + } ] } ``` @@ -51,4 +48,3 @@ test('large snapshot', () => { ## When Not To Use It If you don't want to limit the size of your snapshots, you can disable this rule. - diff --git a/docs/rules/no-mocks-import.md b/docs/rules/no-mocks-import.md index 82676302..de478381 100644 --- a/docs/rules/no-mocks-import.md +++ b/docs/rules/no-mocks-import.md @@ -1,6 +1,4 @@ -# Disallow importing from __mocks__ directory (`vitest/no-mocks-import`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-mocks-import diff --git a/docs/rules/no-restricted-matchers.md b/docs/rules/no-restricted-matchers.md index ebd5de37..3bf4b4cf 100644 --- a/docs/rules/no-restricted-matchers.md +++ b/docs/rules/no-restricted-matchers.md @@ -1,6 +1,4 @@ -# Disallow the use of certain matchers (`vitest/no-restricted-matchers`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-restricted-matchers @@ -8,19 +6,17 @@ This rule disallows the use of certain matchers. - ### Forexample - ### Options ```json { "vitest/no-restricted-matchers": [ - "error", - { - "not": null, - } + "error", + { + "not": null + } ] } ``` @@ -35,4 +31,4 @@ Examples of **correct** code for this rule with the above configuration ```js expect(a).toBe(b) -``` \ No newline at end of file +``` diff --git a/docs/rules/no-restricted-vi-methods.md b/docs/rules/no-restricted-vi-methods.md index 9eea038a..5f485c48 100644 --- a/docs/rules/no-restricted-vi-methods.md +++ b/docs/rules/no-restricted-vi-methods.md @@ -1,6 +1,4 @@ -# Disallow specific `vi.` methods (`vitest/no-restricted-vi-methods`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-restricted-vi-methods @@ -36,18 +34,18 @@ For example: Examples of **incorrect** code for this rule with the above configuration ```js -vi.useFakeTimers(); +vi.useFakeTimers() it('calls the callback after 1 second via advanceTimersByTime', () => { // ... - vi.advanceTimersByTime(1000); + vi.advanceTimersByTime(1000) // ... -}); +}) test('plays video', () => { - const spy = vi.spyOn(video, 'play'); + const spy = vi.spyOn(video, 'play') // ... -}); -``` \ No newline at end of file +}) +``` diff --git a/docs/rules/no-standalone-expect.md b/docs/rules/no-standalone-expect.md index b6c3d17e..e123c93f 100644 --- a/docs/rules/no-standalone-expect.md +++ b/docs/rules/no-standalone-expect.md @@ -1,6 +1,4 @@ -# Disallow using `expect` outside of `it` or `test` blocks (`vitest/no-standalone-expect`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-standalone-expect diff --git a/docs/rules/no-test-prefixes.md b/docs/rules/no-test-prefixes.md index 7fd22470..358c3de8 100644 --- a/docs/rules/no-test-prefixes.md +++ b/docs/rules/no-test-prefixes.md @@ -1,6 +1,4 @@ -# Disallow using the `f` and `x` prefixes in favour of `.only` and `.skip` (`vitest/no-test-prefixes`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-test-prefixes πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -11,11 +9,11 @@ Examples of **incorrect** code for this rule: ```js -xdescribe.each([])("foo", function () {}) +xdescribe.each([])('foo', function () {}) ``` Examples of **correct** code for this rule: ```js -describe.skip.each([])("foo", function () {}) +describe.skip.each([])('foo', function () {}) ``` diff --git a/docs/rules/no-test-return-statement.md b/docs/rules/no-test-return-statement.md index db137ab8..33150082 100644 --- a/docs/rules/no-test-return-statement.md +++ b/docs/rules/no-test-return-statement.md @@ -1,6 +1,4 @@ -# Disallow return statements in tests (`vitest/no-test-return-statement`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# no-test-return-statement diff --git a/docs/rules/padding-around-after-all-blocks.md b/docs/rules/padding-around-after-all-blocks.md index 117d4e27..f2d75d7b 100644 --- a/docs/rules/padding-around-after-all-blocks.md +++ b/docs/rules/padding-around-after-all-blocks.md @@ -1,6 +1,4 @@ -# Enforce padding around `afterAll` blocks (`vitest/padding-around-after-all-blocks`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# padding-around-after-all-blocks πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -17,17 +15,17 @@ its scope. Examples of **incorrect** code for this rule: ```js -const someText = 'hoge'; -afterAll(() => {}); -describe('foo', () => {}); +const someText = 'hoge' +afterAll(() => {}) +describe('foo', () => {}) ``` Examples of **correct** code for this rule: ```js -const someText = 'hoge'; +const someText = 'hoge' -afterAll(() => {}); +afterAll(() => {}) -describe('foo', () => {}); +describe('foo', () => {}) ``` diff --git a/docs/rules/padding-around-after-each-blocks.md b/docs/rules/padding-around-after-each-blocks.md index 822a0c7b..5d868b03 100644 --- a/docs/rules/padding-around-after-each-blocks.md +++ b/docs/rules/padding-around-after-each-blocks.md @@ -1,6 +1,4 @@ -# Enforce padding around `afterEach` blocks (`vitest/padding-around-after-each-blocks`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# padding-around-after-each-blocks πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -17,17 +15,17 @@ its scope. Examples of **incorrect** code for this rule: ```js -const someText = 'hoge'; -afterEach(() => {}); -describe('foo', () => {}); +const someText = 'hoge' +afterEach(() => {}) +describe('foo', () => {}) ``` Examples of **correct** code for this rule: ```js -const someText = 'hoge'; +const someText = 'hoge' -afterEach(() => {}); +afterEach(() => {}) -describe('foo', () => {}); +describe('foo', () => {}) ``` diff --git a/docs/rules/padding-around-all.md b/docs/rules/padding-around-all.md index db3bb254..8021cf24 100644 --- a/docs/rules/padding-around-all.md +++ b/docs/rules/padding-around-all.md @@ -1,12 +1,11 @@ -# Enforce padding around vitest functions (`vitest/padding-around-all`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# padding-around-all πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). ## Rule Details + This is a meta rule that simply enables all of the following rules: - [padding-around-after-all-blocks](padding-around-after-all-blocks.md) @@ -15,4 +14,4 @@ This is a meta rule that simply enables all of the following rules: - [padding-around-before-each-blocks](padding-around-before-each-blocks.md) - [padding-around-expect-groups](padding-around-expect-groups.md) - [padding-around-describe-blocks](padding-around-describe-blocks.md) -- [padding-around-test-blocks](padding-around-test-blocks.md) \ No newline at end of file +- [padding-around-test-blocks](padding-around-test-blocks.md) diff --git a/docs/rules/padding-around-before-all-blocks.md b/docs/rules/padding-around-before-all-blocks.md index 716d8ed4..eb3d17c1 100644 --- a/docs/rules/padding-around-before-all-blocks.md +++ b/docs/rules/padding-around-before-all-blocks.md @@ -1,6 +1,4 @@ -# Enforce padding around `beforeAll` blocks (`vitest/padding-around-before-all-blocks`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# padding-around-before-all-blocks πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -17,17 +15,17 @@ its scope. Examples of **incorrect** code for this rule: ```js -const someText = 'hoge'; -beforeAll(() => {}); -describe('foo', () => {}); +const someText = 'hoge' +beforeAll(() => {}) +describe('foo', () => {}) ``` Examples of **correct** code for this rule: ```js -const someText = 'hoge'; +const someText = 'hoge' -beforeAll(() => {}); +beforeAll(() => {}) -describe('foo', () => {}); +describe('foo', () => {}) ``` diff --git a/docs/rules/padding-around-before-each-blocks.md b/docs/rules/padding-around-before-each-blocks.md index dc5b194e..72f0a9e4 100644 --- a/docs/rules/padding-around-before-each-blocks.md +++ b/docs/rules/padding-around-before-each-blocks.md @@ -1,6 +1,4 @@ -# Enforce padding around `beforeEach` blocks (`vitest/padding-around-before-each-blocks`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# padding-around-before-each-blocks πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -17,17 +15,17 @@ its scope. Examples of **incorrect** code for this rule: ```js -const someText = 'hoge'; -beforeEach(() => {}); -describe('foo', () => {}); +const someText = 'hoge' +beforeEach(() => {}) +describe('foo', () => {}) ``` Examples of **correct** code for this rule: ```js -const someText = 'hoge'; +const someText = 'hoge' -beforeEach(() => {}); +beforeEach(() => {}) -describe('foo', () => {}); +describe('foo', () => {}) ``` diff --git a/docs/rules/padding-around-describe-blocks.md b/docs/rules/padding-around-describe-blocks.md index bf1fe20b..5030dea1 100644 --- a/docs/rules/padding-around-describe-blocks.md +++ b/docs/rules/padding-around-describe-blocks.md @@ -1,6 +1,4 @@ -# Enforce padding around `describe` blocks (`vitest/padding-around-describe-blocks`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# padding-around-describe-blocks πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/padding-around-expect-groups.md b/docs/rules/padding-around-expect-groups.md index 62858ca5..ff2a20a8 100644 --- a/docs/rules/padding-around-expect-groups.md +++ b/docs/rules/padding-around-expect-groups.md @@ -1,6 +1,4 @@ -# Enforce padding around `expect` groups (`vitest/padding-around-expect-groups`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# padding-around-expect-groups πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -18,25 +16,25 @@ Examples of **incorrect** code for this rule: ```js test('test', () => { - let abc = 123; - expect(abc).toEqual(123); - expect(123).toEqual(abc); - abc = 456; - expect(abc).toEqual(456); -}); + let abc = 123 + expect(abc).toEqual(123) + expect(123).toEqual(abc) + abc = 456 + expect(abc).toEqual(456) +}) ``` Examples of **correct** code for this rule: ```js test('test', () => { - let abc = 123; + let abc = 123 - expect(abc).toEqual(123); - expect(123).toEqual(abc); + expect(abc).toEqual(123) + expect(123).toEqual(abc) - abc = 456; + abc = 456 - expect(abc).toEqual(456); -}); + expect(abc).toEqual(456) +}) ``` diff --git a/docs/rules/padding-around-test-blocks.md b/docs/rules/padding-around-test-blocks.md index 20a67ed4..20453fa9 100644 --- a/docs/rules/padding-around-test-blocks.md +++ b/docs/rules/padding-around-test-blocks.md @@ -1,6 +1,4 @@ -# Enforce padding around `test` blocks (`vitest/padding-around-test-blocks`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# padding-around-test-blocks πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/prefer-called-times.md b/docs/rules/prefer-called-times.md new file mode 100644 index 00000000..432d431b --- /dev/null +++ b/docs/rules/prefer-called-times.md @@ -0,0 +1,31 @@ +# prefer-called-times + +πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). + + + +## Rule Details + +This rule aims to enforce the use of `toBeCalledTimes(1)` or `toHaveBeenCalledTimes(1)` over `toBeCalledOnce()` or `toHaveBeenCalledOnce()`. + +Examples of **incorrect** code for this rule: + +```ts +test('foo', () => { + const mock = vi.fn() + mock('foo') + expect(mock).toBeCalledOnce() + expect(mock).toHaveBeenCalledOnce() +}) +``` + +Examples of **correct** code for this rule: + +```ts +test('foo', () => { + const mock = vi.fn() + mock('foo') + expect(mock).toBeCalledTimes(1) + expect(mock).toHaveBeenCalledTimes(1) +}) +``` diff --git a/docs/rules/prefer-called-with.md b/docs/rules/prefer-called-with.md index cc26bf5c..952389e0 100644 --- a/docs/rules/prefer-called-with.md +++ b/docs/rules/prefer-called-with.md @@ -1,6 +1,4 @@ -# Enforce using `toBeCalledWith()` or `toHaveBeenCalledWith()` (`vitest/prefer-called-with`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-called-with πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/prefer-comparison-matcher.md b/docs/rules/prefer-comparison-matcher.md index 95f8333e..d3592cd3 100644 --- a/docs/rules/prefer-comparison-matcher.md +++ b/docs/rules/prefer-comparison-matcher.md @@ -1,6 +1,4 @@ -# Enforce using the built-in comparison matchers (`vitest/prefer-comparison-matcher`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-comparison-matcher πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -16,19 +14,19 @@ This rule checks for comparisons in a test that could be replaced with one of th Examples of **incorrect** code for this rule: ```js -expect(x > 5).toBe(true); -expect(x < 7).not.toEqual(true); -expect(x <= y).toStrictEqual(true); +expect(x > 5).toBe(true) +expect(x < 7).not.toEqual(true) +expect(x <= y).toStrictEqual(true) ``` Examples of **correct** code for this rule: ```js -expect(x).toBeGreaterThan(5); -expect(x).not.toBeLessThanOrEqual(7); -expect(x).toBeLessThanOrEqual(y); +expect(x).toBeGreaterThan(5) +expect(x).not.toBeLessThanOrEqual(7) +expect(x).toBeLessThanOrEqual(y) // special case - see below -expect(x < 'Carl').toBe(true); +expect(x < 'Carl').toBe(true) // Rule only works on inters and big integers -``` \ No newline at end of file +``` diff --git a/docs/rules/prefer-describe-function-title.md b/docs/rules/prefer-describe-function-title.md index 69df6648..061b8210 100644 --- a/docs/rules/prefer-describe-function-title.md +++ b/docs/rules/prefer-describe-function-title.md @@ -1,4 +1,4 @@ -# Enforce using a function as a describe title over an equivalent string (`vitest/prefer-describe-function-title`) +# prefer-describe-function-title πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -11,16 +11,16 @@ This rule aims to enforce passing a named function to `describe()` instead of an Passing named functions means the correct title will be used even if the function is renamed. This rule will report if a string is passed to a `describe()` block if: -* The string matches a function imported into the file -* That function's name also matches the test file's name +- The string matches a function imported into the file +- That function's name also matches the test file's name Examples of **incorrect** code for this rule: ```ts // myFunction.test.js -import { myFunction } from "./myFunction" +import { myFunction } from './myFunction' -describe("myFunction", () => { +describe('myFunction', () => { // ... }) ``` @@ -29,7 +29,7 @@ Examples of **correct** code for this rule: ```ts // myFunction.test.js -import { myFunction } from "./myFunction" +import { myFunction } from './myFunction' describe(myFunction, () => { // ... diff --git a/docs/rules/prefer-each.md b/docs/rules/prefer-each.md index 8f75266a..0105794a 100644 --- a/docs/rules/prefer-each.md +++ b/docs/rules/prefer-each.md @@ -1,6 +1,4 @@ -# Enforce using `each` rather than manual loops (`vitest/prefer-each`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-each @@ -8,7 +6,7 @@ // bad for (const item of items) { describe(item, () => { - expect(item).toBe('foo') + expect(item).toBe('foo') }) } @@ -16,4 +14,4 @@ for (const item of items) { describe.each(items)('item', (item) => { expect(item).toBe('foo') }) -``` \ No newline at end of file +``` diff --git a/docs/rules/prefer-equality-matcher.md b/docs/rules/prefer-equality-matcher.md index 1da7bc7a..78230c5f 100644 --- a/docs/rules/prefer-equality-matcher.md +++ b/docs/rules/prefer-equality-matcher.md @@ -1,6 +1,4 @@ -# Enforce using the built-in quality matchers (`vitest/prefer-equality-matcher`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-equality-matcher πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). @@ -13,11 +11,9 @@ This rule aims to enforce the use of the built-in equality matchers. Examples of **incorrect** code for this rule: ```ts - // bad - expect(1 == 1).toBe(1) - - - // bad - expect(1).toEqual(1) +// bad +expect(1 == 1).toBe(1) -``` \ No newline at end of file +// bad +expect(1).toEqual(1) +``` diff --git a/docs/rules/prefer-expect-assertions.md b/docs/rules/prefer-expect-assertions.md index 511b78ec..31e4217a 100644 --- a/docs/rules/prefer-expect-assertions.md +++ b/docs/rules/prefer-expect-assertions.md @@ -1,6 +1,4 @@ -# Enforce using expect assertions instead of callbacks (`vitest/prefer-expect-assertions`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-expect-assertions πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). @@ -18,26 +16,26 @@ Examples of **incorrect** code for this rule: ```js test('no assertions', () => { // ... -}); +}) test('assertions not first', () => { - expect(true).toBe(true); + expect(true).toBe(true) // ... -}); +}) ``` Examples of **correct** code for this rule: ```js test('assertions first', () => { - expect.assertions(1); + expect.assertions(1) // ... -}); +}) test('assertions first', () => { - expect.hasAssertions(); + expect.hasAssertions() // ... -}); +}) ``` ## Options @@ -73,21 +71,21 @@ when this option is enabled the following code will be considered incorrect: ```js test('assertions first', () => { - for (let i = 0; i < 10; i++) { - expect(i).toBeLessThan(10); - } -}); + for (let i = 0; i < 10; i++) { + expect(i).toBeLessThan(10) + } +}) ``` To fix this, you'll need to add `expect.assertions(1)` or `expect.hasAssertions()` as the first expression: ```js test('assertions first', () => { - expect.hasAssertions(); - for (let i = 0; i < 10; i++) { - expect(i).toBeLessThan(10); - } -}); + expect.hasAssertions() + for (let i = 0; i < 10; i++) { + expect(i).toBeLessThan(10) + } +}) ``` `onlyFunctionsWithExpectInCallback` @@ -98,19 +96,19 @@ when this option is enabled the following code will be considered incorrect: ```js test('assertions first', () => { - fetchData((data) => { - expect(data).toBe('peanut butter'); - }); -}); + fetchData((data) => { + expect(data).toBe('peanut butter') + }) +}) ``` To fix this, you'll need to add `expect.assertions(1)` or `expect.hasAssertions()` as the first expression: ```js test('assertions first', () => { - expect.assertions(1); - fetchData((data) => { - expect(data).toBe('peanut butter'); - }); -}); + expect.assertions(1) + fetchData((data) => { + expect(data).toBe('peanut butter') + }) +}) ``` diff --git a/docs/rules/prefer-expect-resolves.md b/docs/rules/prefer-expect-resolves.md index 1c8eefd5..15fe886a 100644 --- a/docs/rules/prefer-expect-resolves.md +++ b/docs/rules/prefer-expect-resolves.md @@ -1,15 +1,17 @@ -# Enforce using `expect().resolves` over `expect(await ...)` syntax (`vitest/prefer-expect-resolves`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-expect-resolves πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). ```ts -// bad -it('passes', async () => { expect(await someValue()).toBe(true); }); +// bad +it('passes', async () => { + expect(await someValue()).toBe(true) +}) -// good -it('passes', async () => { await expect(someValue()).resolves.toBe(true); }); +// good +it('passes', async () => { + await expect(someValue()).resolves.toBe(true) +}) ``` diff --git a/docs/rules/prefer-hooks-in-order.md b/docs/rules/prefer-hooks-in-order.md index 1dce314c..f94860e4 100644 --- a/docs/rules/prefer-hooks-in-order.md +++ b/docs/rules/prefer-hooks-in-order.md @@ -1,30 +1,28 @@ -# Enforce having hooks in consistent order (`vitest/prefer-hooks-in-order`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-hooks-in-order -```js - // consistent order of hooks - ['beforeAll', 'beforeEach', 'afterEach', 'afterAll'] +```js +// consistent order of hooks +;['beforeAll', 'beforeEach', 'afterEach', 'afterAll'] ``` ```js - // bad - afterAll(() => { - removeMyDatabase(); - }); - beforeAll(() => { - createMyDatabase(); - }); +// bad +afterAll(() => { + removeMyDatabase() +}) +beforeAll(() => { + createMyDatabase() +}) ``` ```js - // good - beforeAll(() => { - createMyDatabase(); - }); - afterAll(() => { - removeMyDatabase(); - }); -``` \ No newline at end of file +// good +beforeAll(() => { + createMyDatabase() +}) +afterAll(() => { + removeMyDatabase() +}) +``` diff --git a/docs/rules/prefer-hooks-on-top.md b/docs/rules/prefer-hooks-on-top.md index 1337f0c7..5d66e76f 100644 --- a/docs/rules/prefer-hooks-on-top.md +++ b/docs/rules/prefer-hooks-on-top.md @@ -1,31 +1,29 @@ -# Enforce having hooks before any test cases (`vitest/prefer-hooks-on-top`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-hooks-on-top + ```ts // bad describe('foo', () => { it('bar', () => { - // ... + // ... }) beforeEach(() => { - // ... + // ... }) }) - // good describe('foo', () => { beforeEach(() => { - // ... + // ... }) it('bar', () => { - // ... + // ... }) }) -``` \ No newline at end of file +``` diff --git a/docs/rules/prefer-importing-vitest-globals.md b/docs/rules/prefer-importing-vitest-globals.md index 418ac526..ba36c2b0 100644 --- a/docs/rules/prefer-importing-vitest-globals.md +++ b/docs/rules/prefer-importing-vitest-globals.md @@ -1,4 +1,8 @@ -# Prefer importing Vitest globals (`vitest/prefer-importing-vitest-globals`) +# prefer-importing-vitest-globals + +πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). + + ⚠️ This rule _warns_ in the 🌐 `all` config. diff --git a/docs/rules/prefer-lowercase-title.md b/docs/rules/prefer-lowercase-title.md index 582bf094..46264058 100644 --- a/docs/rules/prefer-lowercase-title.md +++ b/docs/rules/prefer-lowercase-title.md @@ -1,6 +1,4 @@ -# Enforce lowercase titles (`vitest/prefer-lowercase-title`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-lowercase-title πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -12,7 +10,7 @@ Examples of **incorrect** code for this rule: ```js test('It works', () => { - // ... + // ... }) ``` @@ -20,44 +18,39 @@ Examples of **correct** code for this rule: ```js test('it works', () => { - // ... + // ... }) ``` - ### Options ```json { - "type":"object", - "properties":{ - "ignore":{ - "type":"array", - "items":{ - "enum":[ - "describe", - "test", - "it" - ] - }, - "additionalProperties":false + "type": "object", + "properties": { + "ignore": { + "type": "array", + "items": { + "enum": ["describe", "test", "it"] }, - "allowedPrefixes":{ - "type":"array", - "items":{ - "type":"string" - }, - "additionalItems":false + "additionalProperties": false + }, + "allowedPrefixes": { + "type": "array", + "items": { + "type": "string" }, - "ignoreTopLevelDescribe":{ - "type":"boolean", - "default":false - }, - "lowercaseFirstCharacterOnly":{ - "type":"boolean", - "default":true - } - }, - "additionalProperties":false + "additionalItems": false + }, + "ignoreTopLevelDescribe": { + "type": "boolean", + "default": false + }, + "lowercaseFirstCharacterOnly": { + "type": "boolean", + "default": true + } + }, + "additionalProperties": false } -``` \ No newline at end of file +``` diff --git a/docs/rules/prefer-mock-promise-shorthand.md b/docs/rules/prefer-mock-promise-shorthand.md index a94bc064..5dcbea79 100644 --- a/docs/rules/prefer-mock-promise-shorthand.md +++ b/docs/rules/prefer-mock-promise-shorthand.md @@ -1,10 +1,9 @@ -# Enforce mock resolved/rejected shorthands for promises (`vitest/prefer-mock-promise-shorthand`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-mock-promise-shorthand πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). + ```ts // bad vi.fn().mockReturnValue(Promise.reject(42)) diff --git a/docs/rules/prefer-snapshot-hint.md b/docs/rules/prefer-snapshot-hint.md index c8350dc8..a059fb71 100644 --- a/docs/rules/prefer-snapshot-hint.md +++ b/docs/rules/prefer-snapshot-hint.md @@ -1,6 +1,4 @@ -# Enforce including a hint with external snapshots (`vitest/prefer-snapshot-hint`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-snapshot-hint @@ -22,53 +20,52 @@ Examples of **incorrect** code for this rule with the `always` option: ```ts const snapshotOutput = ({ stdout, stderr }) => { - expect(stdout).toMatchSnapshot(); - expect(stderr).toMatchSnapshot(); -}; + expect(stdout).toMatchSnapshot() + expect(stderr).toMatchSnapshot() +} describe('cli', () => { describe('--version flag', () => { it('prints the version', async () => { - snapshotOutput(await runCli(['--version'])); - }); - }); + snapshotOutput(await runCli(['--version'])) + }) + }) describe('--config flag', () => { it('reads the config', async () => { const { stdout, parsedConfig } = await runCli([ '--config', 'vitest.config.js', - ]); + ]) - expect(stdout).toMatchSnapshot(); - expect(parsedConfig).toMatchSnapshot(); - }); + expect(stdout).toMatchSnapshot() + expect(parsedConfig).toMatchSnapshot() + }) it('prints nothing to stderr', async () => { - const { stderr } = await runCli(['--config', 'vitest.config.js']); + const { stderr } = await runCli(['--config', 'vitest.config.js']) - expect(stderr).toMatchSnapshot(); - }); + expect(stderr).toMatchSnapshot() + }) describe('when the file does not exist', () => { it('throws an error', async () => { await expect( runCli(['--config', 'does-not-exist.js']), - ).rejects.toThrowErrorMatchingSnapshot(); - }); - }); - }); -}); + ).rejects.toThrowErrorMatchingSnapshot() + }) + }) + }) +}) ``` - Examples of **correct** code for this rule with the `always` option: ```ts const snapshotOutput = ({ stdout, stderr }, hints) => { - expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`); - expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`); -}; + expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`) + expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`) +} describe('cli', () => { describe('--version flag', () => { @@ -76,79 +73,78 @@ describe('cli', () => { snapshotOutput(await runCli(['--version']), { stdout: 'version string', stderr: 'empty', - }); - }); - }); + }) + }) + }) describe('--config flag', () => { it('reads the config', async () => { - const { stdout } = await runCli(['--config', 'vitest.config.js']); + const { stdout } = await runCli(['--config', 'vitest.config.js']) - expect(stdout).toMatchSnapshot({}, 'stdout: config settings'); - }); + expect(stdout).toMatchSnapshot({}, 'stdout: config settings') + }) it('prints nothing to stderr', async () => { - const { stderr } = await runCli(['--config', 'vitest.config.js']); + const { stderr } = await runCli(['--config', 'vitest.config.js']) - expect(stderr).toMatchInlineSnapshot(); - }); + expect(stderr).toMatchInlineSnapshot() + }) describe('when the file does not exist', () => { it('throws an error', async () => { await expect( runCli(['--config', 'does-not-exist.js']), - ).rejects.toThrowErrorMatchingSnapshot('stderr: config error'); - }); - }); - }); -}); + ).rejects.toThrowErrorMatchingSnapshot('stderr: config error') + }) + }) + }) +}) ``` - #### `multi` (default) Examples of **incorrect** code for the `'multi'` option: ```ts const snapshotOutput = ({ stdout, stderr }) => { - expect(stdout).toMatchSnapshot(); - expect(stderr).toMatchSnapshot(); -}; + expect(stdout).toMatchSnapshot() + expect(stderr).toMatchSnapshot() +} describe('cli', () => { describe('--version flag', () => { it('prints the version', async () => { - snapshotOutput(await runCli(['--version'])); - }); - }); + snapshotOutput(await runCli(['--version'])) + }) + }) describe('--config flag', () => { it('reads the config', async () => { const { stdout, parsedConfig } = await runCli([ '--config', 'vitest.config.js', - ]); + ]) - expect(stdout).toMatchSnapshot(); - expect(parsedConfig).toMatchSnapshot(); - }); + expect(stdout).toMatchSnapshot() + expect(parsedConfig).toMatchSnapshot() + }) it('prints nothing to stderr', async () => { - const { stderr } = await runCli(['--config', 'vitest.config.js']); + const { stderr } = await runCli(['--config', 'vitest.config.js']) - expect(stderr).toMatchSnapshot(); - }); - }); -}); + expect(stderr).toMatchSnapshot() + }) + }) +}) ``` Examples of **correct** code for the `'multi'` option: ```ts const snapshotOutput = ({ stdout, stderr }, hints) => { - expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`); - expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`); -}; + expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`) + expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`) +} describe('cli', () => { describe('--version flag', () => { @@ -156,30 +152,30 @@ describe('cli', () => { snapshotOutput(await runCli(['--version']), { stdout: 'version string', stderr: 'empty', - }); - }); - }); + }) + }) + }) describe('--config flag', () => { it('reads the config', async () => { - const { stdout } = await runCli(['--config', 'vitest.config.js']); + const { stdout } = await runCli(['--config', 'vitest.config.js']) - expect(stdout).toMatchSnapshot(); - }); + expect(stdout).toMatchSnapshot() + }) it('prints nothing to stderr', async () => { - const { stderr } = await runCli(['--config', 'vitest.config.js']); + const { stderr } = await runCli(['--config', 'vitest.config.js']) - expect(stderr).toMatchInlineSnapshot(); - }); + expect(stderr).toMatchInlineSnapshot() + }) describe('when the file does not exist', () => { it('throws an error', async () => { await expect( runCli(['--config', 'does-not-exist.js']), - ).rejects.toThrowErrorMatchingSnapshot(); - }); - }); - }); -}); + ).rejects.toThrowErrorMatchingSnapshot() + }) + }) + }) +}) ``` diff --git a/docs/rules/prefer-spy-on.md b/docs/rules/prefer-spy-on.md index ce85e338..54fce5f8 100644 --- a/docs/rules/prefer-spy-on.md +++ b/docs/rules/prefer-spy-on.md @@ -1,6 +1,4 @@ -# Enforce using `vi.spyOn` (`vitest/prefer-spy-on`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-spy-on πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -11,13 +9,13 @@ This rule triggers a warning if an object's property is overwritten with a vitest mock. ```ts -Date.now = vi.fn(); -Date.now = vi.fn(() => 10); +Date.now = vi.fn() +Date.now = vi.fn(() => 10) ``` These patterns would not be considered warnings: ```ts -vi.spyOn(Date, 'now'); -vi.spyOn(Date, 'now').mockImplementation(() => 10); +vi.spyOn(Date, 'now') +vi.spyOn(Date, 'now').mockImplementation(() => 10) ``` diff --git a/docs/rules/prefer-strict-boolean-matchers.md b/docs/rules/prefer-strict-boolean-matchers.md index f4aad8c7..547513b1 100644 --- a/docs/rules/prefer-strict-boolean-matchers.md +++ b/docs/rules/prefer-strict-boolean-matchers.md @@ -1,6 +1,4 @@ -# Enforce using `toBe(true)` and `toBe(false)` over matchers that coerce types to boolean (`vitest/prefer-strict-boolean-matchers`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-strict-boolean-matchers πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -15,7 +13,6 @@ expectTypeOf(foo).toBeTruthy() expect(foo).toBeFalsy() expectTypeOf(foo).toBeFalsy() - // good expect(foo).toBe(true) expectTypeOf(foo).toBe(true) diff --git a/docs/rules/prefer-strict-equal.md b/docs/rules/prefer-strict-equal.md index 4fbb06fb..cbf152dc 100644 --- a/docs/rules/prefer-strict-equal.md +++ b/docs/rules/prefer-strict-equal.md @@ -1,6 +1,4 @@ -# Enforce strict equal over equal (`vitest/prefer-strict-equal`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-strict-equal πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). @@ -9,9 +7,8 @@ ```ts // bad -expect(something).toEqual(somethingElse); +expect(something).toEqual(somethingElse) // good -expect(something).toStrictEqual(somethingElse); - -``` \ No newline at end of file +expect(something).toStrictEqual(somethingElse) +``` diff --git a/docs/rules/prefer-to-be-falsy.md b/docs/rules/prefer-to-be-falsy.md index 57479201..724412a0 100644 --- a/docs/rules/prefer-to-be-falsy.md +++ b/docs/rules/prefer-to-be-falsy.md @@ -1,6 +1,4 @@ -# Enforce using toBeFalsy() (`vitest/prefer-to-be-falsy`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-to-be-falsy πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -23,4 +21,3 @@ Examples of **correct** code for this rule: expect(foo).toBeFalsy() expectTypeOf(foo).toBeFalsy() ``` - diff --git a/docs/rules/prefer-to-be-object.md b/docs/rules/prefer-to-be-object.md index 8412f27a..c4482ca1 100644 --- a/docs/rules/prefer-to-be-object.md +++ b/docs/rules/prefer-to-be-object.md @@ -1,13 +1,12 @@ -# Enforce using toBeObject() (`vitest/prefer-to-be-object`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-to-be-object πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). + ```js -expectTypeOf({}).not.toBeInstanceOf(Object); +expectTypeOf({}).not.toBeInstanceOf(Object) // should be -expectTypeOf({}).not.toBeObject(); -``` \ No newline at end of file +expectTypeOf({}).not.toBeObject() +``` diff --git a/docs/rules/prefer-to-be-truthy.md b/docs/rules/prefer-to-be-truthy.md index 6bc4cffc..83a5ef27 100644 --- a/docs/rules/prefer-to-be-truthy.md +++ b/docs/rules/prefer-to-be-truthy.md @@ -1,6 +1,4 @@ -# Enforce using `toBeTruthy` (`vitest/prefer-to-be-truthy`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-to-be-truthy πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/prefer-to-be.md b/docs/rules/prefer-to-be.md index ae697f28..98a94f9f 100644 --- a/docs/rules/prefer-to-be.md +++ b/docs/rules/prefer-to-be.md @@ -1,12 +1,10 @@ -# Enforce using toBe() (`vitest/prefer-to-be`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-to-be πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). -### Correct +### Correct ```ts import { test } from 'vitest' @@ -16,7 +14,7 @@ test('foo', () => { }) ``` -### Incorrect +### Incorrect ```ts import { test } from 'vitest' @@ -25,4 +23,3 @@ test('foo', () => { expect(1).toEqual(1) }) ``` - \ No newline at end of file diff --git a/docs/rules/prefer-to-contain.md b/docs/rules/prefer-to-contain.md index 3583aedc..c1206b5c 100644 --- a/docs/rules/prefer-to-contain.md +++ b/docs/rules/prefer-to-contain.md @@ -1,27 +1,21 @@ -# Enforce using toContain() (`vitest/prefer-to-contain`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-to-contain πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). - This rule triggers a warning if `toBe()`, `toEqual()` or `toStrickEqual()` is used to assert object inclusion in an array. - The following patterns are considered warnings: - ```ts -expect(a.includes(b)).toBe(true); -expect(a.includes(b)).toEqual(true); -expect(a.includes(b)).toStrictEqual(true); +expect(a.includes(b)).toBe(true) +expect(a.includes(b)).toEqual(true) +expect(a.includes(b)).toStrictEqual(true) ``` - The following patterns are not considered warnings: ```ts -expect(a).toContain(b); -``` \ No newline at end of file +expect(a).toContain(b) +``` diff --git a/docs/rules/prefer-to-have-length.md b/docs/rules/prefer-to-have-length.md index 21fc71a1..8e50495e 100644 --- a/docs/rules/prefer-to-have-length.md +++ b/docs/rules/prefer-to-have-length.md @@ -1,14 +1,13 @@ -# Enforce using toHaveLength() (`vitest/prefer-to-have-length`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-to-have-length πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). + ```js // bad -expect(files.length).toStrictEqual(1); +expect(files.length).toStrictEqual(1) // good -expect(files).toHaveLength(1); -``` \ No newline at end of file +expect(files).toHaveLength(1) +``` diff --git a/docs/rules/prefer-todo.md b/docs/rules/prefer-todo.md index 322d73bf..09484f5d 100644 --- a/docs/rules/prefer-todo.md +++ b/docs/rules/prefer-todo.md @@ -1,6 +1,4 @@ -# Enforce using `test.todo` (`vitest/prefer-todo`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-todo πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -13,7 +11,7 @@ When tests are empty it's better to mark them as `test.todo` as it will be highl The following pattern is considered a warning: ```js -test('foo'); +test('foo') test('foo', () => {}) test.skip('foo', () => {}) ``` @@ -21,5 +19,5 @@ test.skip('foo', () => {}) The following pattern is not considered a warning: ```js -test.todo('foo'); +test.todo('foo') ``` diff --git a/docs/rules/prefer-vi-mocked.md b/docs/rules/prefer-vi-mocked.md index c9f5d5bd..a2b5f82e 100644 --- a/docs/rules/prefer-vi-mocked.md +++ b/docs/rules/prefer-vi-mocked.md @@ -1,9 +1,9 @@ -# Prefer `vi.mocked()` over `fn as Mock` (`vitest/prefer-vi-mocked`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# prefer-vi-mocked πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). +πŸ’­ This rule requires [type information](https://typescript-eslint.io/linting/typed-linting). + When working with mocks of functions using Vitest, it's recommended to use the @@ -22,18 +22,18 @@ Restricted types: The following patterns are warnings: ```typescript -(foo as Mock).mockReturnValue(1); -const mock = (foo as Mock).mockReturnValue(1); -(foo as unknown as Mock).mockReturnValue(1); -(Obj.foo as Mock).mockReturnValue(1); -([].foo as Mock).mockReturnValue(1); +;(foo as Mock).mockReturnValue(1) +const mock = (foo as Mock).mockReturnValue(1) +;(foo as unknown as Mock).mockReturnValue(1) +;(Obj.foo as Mock).mockReturnValue(1) +;([].foo as Mock).mockReturnValue(1) ``` The following patterns are not warnings: ```js -vi.mocked(foo).mockReturnValue(1); -const mock = vi.mocked(foo).mockReturnValue(1); -vi.mocked(Obj.foo).mockReturnValue(1); -vi.mocked([].foo).mockReturnValue(1); -``` \ No newline at end of file +vi.mocked(foo).mockReturnValue(1) +const mock = vi.mocked(foo).mockReturnValue(1) +vi.mocked(Obj.foo).mockReturnValue(1) +vi.mocked([].foo).mockReturnValue(1) +``` diff --git a/docs/rules/require-hook.md b/docs/rules/require-hook.md index 0f5e0452..be70f634 100644 --- a/docs/rules/require-hook.md +++ b/docs/rules/require-hook.md @@ -1,12 +1,10 @@ -# Require setup and teardown to be within a hook (`vitest/require-hook`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# require-hook -It's common when writing tests to need to perform a particular setup work before and after a test suite run. Because Vitest executes all `describe` handlers in a test file _before_ it executes any of the actual tests, it's important to ensure setup and teardown work is done inside `before*` and `after*` handlers respectively, rather than inside the `describe` blocks. +It's common when writing tests to need to perform a particular setup work before and after a test suite run. Because Vitest executes all `describe` handlers in a test file _before_ it executes any of the actual tests, it's important to ensure setup and teardown work is done inside `before*` and `after*` handlers respectively, rather than inside the `describe` blocks. -## Details +## Details This rule flags any expression that is either at the toplevel of a test file or directly within the body of a `describe` except the following: @@ -18,52 +16,48 @@ This rule flags any expression that is either at the toplevel of a test file or This rule flags any function within in a `describe` block and suggest wrapping them in one of the four lifecycle hooks. - The following patterns are considered warnings: ```ts import { database } from './api' describe('foo', () => { - database.connect() + database.connect() - test('bar', () => { - // ... - }) + test('bar', () => { + // ... + }) - database.disconnect() + database.disconnect() }) ``` - - The following patterns are not warnings: - - ```ts describe('foo', () => { - before(() => { - database.connect() - }) + before(() => { + database.connect() + }) - test('bar', () => { - // ... - }) + test('bar', () => { + // ... + }) }) ``` - ## Options If there are methods that you want to call outside of hooks and tests, you can mark them as allowed using the `allowedFunctionCalls` option. - ```json { - "vitest/require-hook": ["error", { - "allowedFunctionCalls": ["database.connect"] - }] + "vitest/require-hook": [ + "error", + { + "allowedFunctionCalls": ["database.connect"] + } + ] } ``` @@ -73,12 +67,12 @@ The following patterns are not warnings because `database.connect` is allowed: import { database } from './api' describe('foo', () => { - database.connect() + database.connect() - test('bar', () => { - // ... - }) + test('bar', () => { + // ... + }) - database.disconnect() + database.disconnect() }) ``` diff --git a/docs/rules/require-local-test-context-for-concurrent-snapshots.md b/docs/rules/require-local-test-context-for-concurrent-snapshots.md index 291e6326..6bf695de 100644 --- a/docs/rules/require-local-test-context-for-concurrent-snapshots.md +++ b/docs/rules/require-local-test-context-for-concurrent-snapshots.md @@ -1,8 +1,4 @@ -# Require local Test Context for concurrent snapshot tests (`vitest/require-local-test-context-for-concurrent-snapshots`) - -πŸ’Ό This rule is enabled in the βœ… `recommended` config. - -⚠️ This rule _warns_ in the 🌐 `all` config. +# require-local-test-context-for-concurrent-snapshots @@ -12,13 +8,13 @@ Examples of **incorrect** code for this rule: ```js test.concurrent('myLogic', () => { - expect(true).toMatchSnapshot(); + expect(true).toMatchSnapshot() }) describe.concurrent('something', () => { - test('myLogic', () => { - expect(true).toMatchInlineSnapshot(); - }) + test('myLogic', () => { + expect(true).toMatchInlineSnapshot() + }) }) ``` diff --git a/docs/rules/require-mock-type-parameters.md b/docs/rules/require-mock-type-parameters.md index da4687d5..d9708b42 100644 --- a/docs/rules/require-mock-type-parameters.md +++ b/docs/rules/require-mock-type-parameters.md @@ -1,6 +1,6 @@ -# Enforce using type parameters with vitest mock functions (`vitest/require-mock-type-parameters`) +# require-mock-type-parameters -⚠️ This rule _warns_ in the 🌐 `all` config. +πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -34,9 +34,12 @@ test('foo', () => { ```json { - "vitest/require-hook": ["error", { - "checkImportFunctions": false - }] + "vitest/require-hook": [ + "error", + { + "checkImportFunctions": false + } + ] } ``` diff --git a/docs/rules/require-to-throw-message.md b/docs/rules/require-to-throw-message.md index 590eb146..d5828586 100644 --- a/docs/rules/require-to-throw-message.md +++ b/docs/rules/require-to-throw-message.md @@ -1,6 +1,4 @@ -# Require toThrow() to be called with an error message (`vitest/require-to-throw-message`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# require-to-throw-message @@ -12,13 +10,13 @@ The following patterns are considered warnings: ```js test('foo', () => { expect(() => { - throw new Error('foo') + throw new Error('foo') }).toThrow() }) test('foo', () => { expect(() => { - throw new Error('foo') + throw new Error('foo') }).toThrowError() }) ``` @@ -28,13 +26,13 @@ The following patterns are not considered warnings: ```js test('foo', () => { expect(() => { - throw new Error('foo') + throw new Error('foo') }).toThrow('foo') }) test('foo', () => { expect(() => { - throw new Error('foo') + throw new Error('foo') }).toThrowError('foo') }) -``` \ No newline at end of file +``` diff --git a/docs/rules/require-top-level-describe.md b/docs/rules/require-top-level-describe.md index be318827..0d77f634 100644 --- a/docs/rules/require-top-level-describe.md +++ b/docs/rules/require-top-level-describe.md @@ -1,62 +1,50 @@ -# Enforce that all tests are in a top-level describe (`vitest/require-top-level-describe`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# require-top-level-describe This rule triggers warning if a test case (`test` and `it`) or a hook (`beforeAll`, `beforeEach`, `afterEach`, `afterAll`) is not located in a top-level `describe` block. - ## Options -This rule accepts an object with the following properties: +This rule accepts an object with the following properties: - `maxNumberOfTopLevelDescribes`: The maximum number of top-level tests allowed in a file. Defaults to `Infinity`. Allowing any number of top-level describe blocks. ```json { - "vitest/require-top-level-describe": [ - "error", - { - "maxNumberOfTopLevelDescribes": 2 - } - ] + "vitest/require-top-level-describe": [ + "error", + { + "maxNumberOfTopLevelDescribes": 2 + } + ] } ``` - - The following patterns are considered warnings: ```js test('foo', () => {}) beforeEach(() => { - describe('bar', () => { - test('baz', () => {}) - }) + describe('bar', () => { + test('baz', () => {}) + }) }) - - ``` The following patterns are not considered warnings: ```js describe('foo', () => { - test('bar', () => {}) + test('bar', () => {}) }) describe('foo', () => { - beforeEach(() => { - describe('bar', () => { - test('baz', () => {}) - }) - }) + beforeEach(() => { + describe('bar', () => { + test('baz', () => {}) + }) + }) }) - ``` - - - - diff --git a/docs/rules/valid-describe-callback.md b/docs/rules/valid-describe-callback.md index eb28f33e..6090eedb 100644 --- a/docs/rules/valid-describe-callback.md +++ b/docs/rules/valid-describe-callback.md @@ -1,50 +1,43 @@ -# Enforce valid describe callback (`vitest/valid-describe-callback`) - -πŸ’Ό This rule is enabled in the βœ… `recommended` config. - -⚠️ This rule _warns_ in the 🌐 `all` config. +# valid-describe-callback - This rule validates the second parameter of a `describe()` function is a callback. - should not contain parameters -- should not contain any `return` statements +- should not contain any `return` statements The following are considered warnings: ```js // callback function parameters are not allowed -describe("myfunc", done => { - // +describe('myfunc', (done) => { + // }) - -describe("myfunc", () => { - // no return statements are allowed in a block of a callback function - return Promise.resolve().then(() => { - // - }) +describe('myfunc', () => { + // no return statements are allowed in a block of a callback function + return Promise.resolve().then(() => { + // + }) }) // returning a value from a describe block is not allowed -describe("myfunc", () => - it("should do something", () => { - // - }) -) +describe('myfunc', () => + it('should do something', () => { + // + })) ``` The following are not considered warnings: ```js -describe("myfunc", async () => { - // +describe('myfunc', async () => { + // }) -describe("myfunc", () => { - it("should do something", () => { - // - }) +describe('myfunc', () => { + it('should do something', () => { + // + }) }) ``` diff --git a/docs/rules/valid-expect-in-promise.md b/docs/rules/valid-expect-in-promise.md index 2b08aeff..ddfcb5bb 100644 --- a/docs/rules/valid-expect-in-promise.md +++ b/docs/rules/valid-expect-in-promise.md @@ -1,6 +1,4 @@ -# Require promises that have expectations in their chain to be valid (`vitest/valid-expect-in-promise`) - -⚠️ This rule _warns_ in the 🌐 `all` config. +# valid-expect-in-promise @@ -11,41 +9,39 @@ The following patterns is considered warning: ```js test('promise test', async () => { something().then((value) => { - expect(value).toBe('red'); - }); -}); + expect(value).toBe('red') + }) +}) test('promises test', () => { const onePromise = something().then((value) => { - expect(value).toBe('red'); - }); + expect(value).toBe('red') + }) const twoPromise = something().then((value) => { - expect(value).toBe('blue'); - }); + expect(value).toBe('blue') + }) - return Promise.any([onePromise, twoPromise]); -}); + return Promise.any([onePromise, twoPromise]) +}) ``` The following pattern is not warning: ```js - test('promise test', async () => { await something().then((value) => { - expect(value).toBe('red'); - }); -}); + expect(value).toBe('red') + }) +}) test('promises test', () => { const onePromise = something().then((value) => { - expect(value).toBe('red'); - }); + expect(value).toBe('red') + }) const twoPromise = something().then((value) => { - expect(value).toBe('blue'); - }); + expect(value).toBe('blue') + }) - return Promise.all([onePromise, twoPromise]); -}); - -``` \ No newline at end of file + return Promise.all([onePromise, twoPromise]) +}) +``` diff --git a/docs/rules/valid-expect.md b/docs/rules/valid-expect.md index 6e98836f..9dca26bd 100644 --- a/docs/rules/valid-expect.md +++ b/docs/rules/valid-expect.md @@ -1,8 +1,4 @@ -# Enforce valid `expect()` usage (`vitest/valid-expect`) - -πŸ’Ό This rule is enabled in the βœ… `recommended` config. - -⚠️ This rule _warns_ in the 🌐 `all` config. +# valid-expect πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -14,74 +10,69 @@ This rule triggers a warning if `expect` is called with no argument or with more 1. `alwaysAwait` - - Type: `boolean` - - Default: `false` - - - Enforce `expect` to be called with an `await` expression. +- Type: `boolean` +- Default: `false` - ```js - // βœ… good - await expect(Promise.resolve(1)).resolves.toBe(1) - await expect(Promise.reject(1)).rejects.toBe(1) +- Enforce `expect` to be called with an `await` expression. - // ❌ bad - expect(Promise.resolve(1)).resolves.toBe(1) - expect(Promise.reject(1)).rejects.toBe(1) - ``` + ```js + // βœ… good + await expect(Promise.resolve(1)).resolves.toBe(1) + await expect(Promise.reject(1)).rejects.toBe(1) + // ❌ bad + expect(Promise.resolve(1)).resolves.toBe(1) + expect(Promise.reject(1)).rejects.toBe(1) + ``` 2. `asyncMatchers` - - Type: `string[]` - - Default: `[]` +- Type: `string[]` +- Default: `[]` - - ```js - { +```js +{ "vitest/valid-expect": ["error", { "asyncMatchers": ["toBeResolved", "toBeRejected"] }] - } - ``` - - avoid using asyncMatchers with `expect`: - +} +``` +avoid using asyncMatchers with `expect`: 3. `minArgs` - - Type: `number` - - Default: `1` - - - Enforce `expect` to be called with at least `minArgs` arguments. +- Type: `number` +- Default: `1` - ```js - // βœ… good - expect(1).toBe(1) - expect(1, 2).toBe(1) - expect(1, 2, 3).toBe(1) +- Enforce `expect` to be called with at least `minArgs` arguments. - // ❌ bad - expect().toBe(1) - expect(1).toBe() - ``` + ```js + // βœ… good + expect(1).toBe(1) + expect(1, 2).toBe(1) + expect(1, 2, 3).toBe(1) + + // ❌ bad + expect().toBe(1) + expect(1).toBe() + ``` 4. `maxArgs` - - Type: `number` - - Default: `1` +- Type: `number` +- Default: `1` - - Enforce `expect` to be called with at most `maxArgs` arguments. - - Exception: `expect(value, "message")` is allowed. +- Enforce `expect` to be called with at most `maxArgs` arguments. +- Exception: `expect(value, "message")` is allowed. - ```js - // βœ… good - expect(1).toBe(1) - expect(1, "expect value to be one").toBe(1) - const message = "expect value to be one" - expect(1, `Error Message: ${message}`).toBe(1) - - - // ❌ bad - expect(1, 2, 3, 4).toBe(1) - ``` + ```js + // βœ… good + expect(1).toBe(1) + expect(1, 'expect value to be one').toBe(1) + const message = 'expect value to be one' + expect(1, `Error Message: ${message}`).toBe(1) + + // ❌ bad + expect(1, 2, 3, 4).toBe(1) + ``` diff --git a/docs/rules/valid-title.md b/docs/rules/valid-title.md index 049e0c5d..9a0cab80 100644 --- a/docs/rules/valid-title.md +++ b/docs/rules/valid-title.md @@ -1,8 +1,4 @@ -# Enforce valid titles (`vitest/valid-title`) - -πŸ’Ό This rule is enabled in the βœ… `recommended` config. - -⚠️ This rule _warns_ in the 🌐 `all` config. +# valid-title πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). @@ -19,13 +15,13 @@ This rule has an object option: ```json { "vitest/valid-title": [ - "error", - { - "ignoreTypeOfDescribeName": false, - "allowArguments": false, - "disallowedWords": ["skip", "only"], - "mustNotMatch": ["^\\s+$", "^\\s*\\d+\\s*$"], - "mustMatch": ["^\\s*\\w+\\s*$"] + "error", + { + "ignoreTypeOfDescribeName": false, + "allowArguments": false, + "disallowedWords": ["skip", "only"], + "mustNotMatch": ["^\\s+$", "^\\s*\\d+\\s*$"], + "mustMatch": ["^\\s*\\w+\\s*$"] } ] } @@ -40,7 +36,7 @@ Examples of **incorrect** code for this rule with the `{ "ignoreTypeOfDescribeNa ```js describe(1, () => { it('should be a number', () => { - expect(1).toBe(1) + expect(1).toBe(1) }) }) ``` @@ -50,7 +46,7 @@ Examples of **correct** code for this rule with the `{ "ignoreTypeOfDescribeName ```js describe('1', () => { it('should be a number', () => { - expect(1).toBe(1) + expect(1).toBe(1) }) }) ``` @@ -63,9 +59,7 @@ Examples of **correct** code for this rule with the `{ "allowArguments": false } ```js describe('name', () => { - it('name', () => { - - }) + it('name', () => {}) }) ``` @@ -73,9 +67,7 @@ Examples of **correct** code for this rule with the `{ "allowArguments": true }` ```js describe(foo, () => { - it(hoge, () => { - - }) + it(hoge, () => {}) }) ``` @@ -88,7 +80,7 @@ Examples of **incorrect** code for this rule with the `{ "disallowedWords": ["sk ```js describe('foo', () => { it.skip('should be skipped', () => { - expect(1).toBe(1) + expect(1).toBe(1) }) }) ``` @@ -98,7 +90,7 @@ Examples of **correct** code for this rule with the `{ "disallowedWords": ["skip ```js describe('foo', () => { it('should be skipped', () => { - expect(1).toBe(1) + expect(1).toBe(1) }) }) ``` @@ -112,7 +104,7 @@ Examples of **incorrect** code for this rule with the `{ "mustNotMatch": ["^\\s+ ```js describe('foo', () => { it(' ', () => { - expect(1).toBe(1) + expect(1).toBe(1) }) }) ``` @@ -120,10 +112,9 @@ describe('foo', () => { Examples of **correct** code for this rule with the `{ "mustNotMatch": ["^\\s+$", "^\\s*\\d+\\s*$"] }` option: ```js - describe('foo', () => { it('should be a number', () => { - expect(1).toBe(1) + expect(1).toBe(1) }) }) ``` @@ -145,7 +136,7 @@ Examples of **incorrect** code for this rule with the `{ "mustMatch": ["^\\s*\\w ```js describe('foo', () => { it(' ', () => { - expect(1).toBe(1) + expect(1).toBe(1) }) }) ``` @@ -153,10 +144,9 @@ describe('foo', () => { Examples of **correct** code for this rule with the `{ "mustMatch": ["^\\s*\\w+\\s*$"] }` option: ```js - describe('foo', () => { it('should be a number', () => { - expect(1).toBe(1) + expect(1).toBe(1) }) }) ``` @@ -190,22 +180,22 @@ Note: If you'd like to use a function or class names inside `describe`, `test` o To enable typechecking for vitest make sure settings key is added in your configuration ```js -import vitest from "eslint-plugin-vitest"; +import vitest from 'eslint-plugin-vitest' export default [ - { - files: ["tests/**"], - plugins: { - vitest - }, - rules: { - ...vitest.configs.recommended.rules - }, - settings: { + { + files: ['tests/**'], + plugins: { + vitest, + }, + rules: { + ...vitest.configs.recommended.rules, + }, + settings: { vitest: { - typecheck: true - } - } - } + typecheck: true, + }, + }, + }, ] ``` diff --git a/eslint.config.js b/eslint.config.js index 83b88675..c917a01c 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -15,7 +15,7 @@ export default [ }, }, { - ignores: ['dist/**/*', '**/*.md'], + files: ['**/*.test.*'], plugins: { vitest, }, diff --git a/package.json b/package.json index 3bc071e3..32ee7e35 100644 --- a/package.json +++ b/package.json @@ -39,17 +39,17 @@ ], "scripts": { "build": "unbuild --config unbuild.config.ts", - "lint:eslint-docs": "pnpm build && eslint-doc-generator --check", + "lint:eslint-docs": "eslint-doc-generator --check", "lint:js": "eslint .", "lint": "concurrently --prefixColors auto \"pnpm:lint:*\"", "release": "bumpp package.json --commit --push --tag && pnpm build && pnpm publish", "stub": "unbuild --stub", "test:ci": "npm run format:check && vitest run", - "format:check": "npx prettier 'src/**/*.ts' --check", - "format:fix": "npx prettier '**/*.{ts,js}' --write", + "format:check": "prettier 'src/**/*.ts' --check", + "format:fix": "prettier '**/*.{ts,js}' --write", "test": "vitest", "generate": "tsx scripts/generate.ts", - "update:eslint-docs": "pnpm build && eslint-doc-generator", + "build:docs": "pnpm build && eslint-doc-generator", "tsc": "tsc --noEmit" }, "dependencies": { @@ -68,8 +68,8 @@ "concurrently": "^9.1.2", "eslint": "^9.20.0", "eslint-config-prettier": "^10.1.5", - "eslint-doc-generator": "^2.0.2", - "eslint-plugin-eslint-plugin": "^6.4.0", + "eslint-doc-generator": "^2.2.2", + "eslint-plugin-eslint-plugin": "^6.5.0", "eslint-remote-tester": "^4.0.1", "eslint-remote-tester-repositories": "^2.0.0", "prettier": "^3.5.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 08851983..2eaf9956 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -49,11 +49,11 @@ importers: specifier: ^10.1.5 version: 10.1.5(eslint@9.20.0(jiti@2.4.2)) eslint-doc-generator: - specifier: ^2.0.2 - version: 2.0.2(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3) + specifier: ^2.2.2 + version: 2.2.2(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3) eslint-plugin-eslint-plugin: - specifier: ^6.4.0 - version: 6.4.0(eslint@9.20.0(jiti@2.4.2)) + specifier: ^6.5.0 + version: 6.5.0(eslint@9.20.0(jiti@2.4.2)) eslint-remote-tester: specifier: ^4.0.1 version: 4.0.1(eslint@9.20.0(jiti@2.4.2))(importx@0.3.11)(jiti@2.4.2) @@ -1301,9 +1301,9 @@ packages: resolution: {integrity: sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ==} engines: {node: '>=16'} - commander@12.1.0: - resolution: {integrity: sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==} - engines: {node: '>=18'} + commander@14.0.0: + resolution: {integrity: sha512-2uM9rYjPvyq39NwLRqaiLtWHyDC1FvryJDa2ATTVims5YAS4PupsEQsDvP14FqhFr0P49CYDugi59xaxJlTXRA==} + engines: {node: '>=20'} commander@7.2.0: resolution: {integrity: sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==} @@ -1522,15 +1522,15 @@ packages: peerDependencies: eslint: '>=7.0.0' - eslint-doc-generator@2.0.2: - resolution: {integrity: sha512-zdPXjnOKuxDf2Wn66aV86IvltboiNDBTgzYZKUmpGEx83anJG2ubptgvajfKhTidfL8QEfDcy9ha4M1uC0HwoQ==} + eslint-doc-generator@2.2.2: + resolution: {integrity: sha512-LBr0Nz1AZnkifkOMyE0sfx+IvS/V+TK1Sp8fCYDdk4Eb5gZCpEcK4t/ImT23oJAwso26rkHzBCRMrd/bc7bddQ==} engines: {node: ^18.18.0 || ^20.9.0 || >=22.0.0} hasBin: true peerDependencies: eslint: '>= 8.57.1' - eslint-plugin-eslint-plugin@6.4.0: - resolution: {integrity: sha512-X94/hr7DnckX68wE6Qqeo3DsZndZSclfoewjwD249yG5z2EAOl3UGUohLIgOpmbUjcFv6AlfW3wxBnOiWkS1Iw==} + eslint-plugin-eslint-plugin@6.5.0: + resolution: {integrity: sha512-DT8YpcXDtMBcBZN39JlkHGurHKU8eYFLavTrnowQLeNwqe/diRUsllsftgD/7dZ2/ItabNLLF2/EYapE1H+G7Q==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: '>=8.23.0' @@ -3725,7 +3725,7 @@ snapshots: commander@11.1.0: {} - commander@12.1.0: {} + commander@14.0.0: {} commander@7.2.0: {} @@ -4028,12 +4028,12 @@ snapshots: dependencies: eslint: 9.20.0(jiti@2.4.2) - eslint-doc-generator@2.0.2(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3): + eslint-doc-generator@2.2.2(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3): dependencies: '@typescript-eslint/utils': 8.24.1(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3) ajv: 8.13.0 change-case: 5.4.4 - commander: 12.1.0 + commander: 14.0.0 cosmiconfig: 9.0.0(typescript@5.7.3) deepmerge: 4.3.1 dot-prop: 9.0.0 @@ -4048,7 +4048,7 @@ snapshots: - supports-color - typescript - eslint-plugin-eslint-plugin@6.4.0(eslint@9.20.0(jiti@2.4.2)): + eslint-plugin-eslint-plugin@6.5.0(eslint@9.20.0(jiti@2.4.2)): dependencies: '@eslint-community/eslint-utils': 4.4.0(eslint@9.20.0(jiti@2.4.2)) eslint: 9.20.0(jiti@2.4.2) diff --git a/src/index.ts b/src/index.ts index 0792fb38..204543e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,6 +69,7 @@ import validExpectInPromise, { RULE_NAME as validExpectInPromiseName } from './r import preferStrictBooleanMatchers, { RULE_NAME as preferStrictBooleanMatchersName } from './rules/prefer-strict-boolean-matchers' import requireMockTypeParameters, { RULE_NAME as requireMockTypeParametersName } from './rules/require-mock-type-parameters' import preferCalledOnce, { RULE_NAME as preferCalledOnceName } from './rules/prefer-called-once' +import preferCalledTimes, { RULE_NAME as preferCalledTimesName } from './rules/prefer-called-times' const createConfig = (rules: R) => Object.keys(rules).reduce((acc, ruleName) => { @@ -159,7 +160,8 @@ const allRules = { [requireMockTypeParametersName]: 'warn', [noImportingVitestGlobalsName]: 'off', [preferImportingVitestGlobalsName]: 'warn', - [preferCalledOnceName]: 'warn' + [preferCalledOnceName]: 'warn', + [preferCalledTimesName]: 'warn' } as const const recommended = { @@ -173,12 +175,7 @@ const recommended = { [noImportNodeTestName]: 'error', } as const -const plugin = { - meta: { - name: 'vitest', - version, - }, - rules: { +const rules = { [lowerCaseTitleName]: lowerCaseTitle, [maxNestedDescribeName]: maxNestedDescribe, [noIdenticalTitleName]: noIdenticalTitle, @@ -248,8 +245,16 @@ const plugin = { [requireMockTypeParametersName]: requireMockTypeParameters, [noImportingVitestGlobalsName]: noImportingVitestGlobals, [preferImportingVitestGlobalsName]: preferImportingVitestGlobals, - [preferCalledOnceName]: preferCalledOnce + [preferCalledOnceName]: preferCalledOnce, + [preferCalledTimesName]: preferCalledTimes +} + +const plugin = { + meta: { + name: 'vitest', + version, }, + rules, environments: { env: { globals: { @@ -274,10 +279,8 @@ const plugin = { }, }, configs: { - 'legacy-recommended': createConfigLegacy(recommended), - 'legacy-all': createConfigLegacy(allRules), - recommended: { - name: '@vitest/recommended', + recommended: { + name: 'vitest/recommended', plugins: { get vitest() { return plugin @@ -286,7 +289,7 @@ const plugin = { rules: createConfig(recommended), }, all: { - name: '@vitest/all', + name: 'vitest/all', plugins: { get vitest() { return plugin @@ -294,6 +297,8 @@ const plugin = { }, rules: createConfig(allRules), }, + 'legacy-recommended': createConfigLegacy(recommended), + 'legacy-all': createConfigLegacy(allRules), env: { name: '@vitest/env', languageOptions: { diff --git a/src/rules/expect-expect.ts b/src/rules/expect-expect.ts index e698f2a4..8c52761a 100644 --- a/src/rules/expect-expect.ts +++ b/src/rules/expect-expect.ts @@ -21,7 +21,7 @@ export default createEslintRule({ type: 'suggestion', docs: { description: 'enforce having expectation in test body', - recommended: false, + recommended: true, }, schema: [ { diff --git a/src/rules/no-commented-out-tests.ts b/src/rules/no-commented-out-tests.ts index 874ac371..08464b37 100644 --- a/src/rules/no-commented-out-tests.ts +++ b/src/rules/no-commented-out-tests.ts @@ -17,7 +17,7 @@ export default createEslintRule({ docs: { description: 'disallow commented out tests', requiresTypeChecking: false, - recommended: false, + recommended: true, }, messages: { noCommentedOutTests: diff --git a/src/rules/no-identical-title.ts b/src/rules/no-identical-title.ts index d29fa634..ee8fe363 100644 --- a/src/rules/no-identical-title.ts +++ b/src/rules/no-identical-title.ts @@ -29,7 +29,7 @@ export default createEslintRule({ type: 'problem', docs: { description: 'disallow identical titles', - recommended: false, + recommended: true, }, fixable: 'code', schema: [], diff --git a/src/rules/no-import-node-test.ts b/src/rules/no-import-node-test.ts index 7d85602a..47ff3858 100644 --- a/src/rules/no-import-node-test.ts +++ b/src/rules/no-import-node-test.ts @@ -9,7 +9,7 @@ export default createEslintRule({ meta: { docs: { description: 'disallow importing `node:test`', - recommended: false, + recommended: true, }, type: 'suggestion', messages: { diff --git a/src/rules/prefer-called-times.ts b/src/rules/prefer-called-times.ts new file mode 100644 index 00000000..57ecae6f --- /dev/null +++ b/src/rules/prefer-called-times.ts @@ -0,0 +1,56 @@ +import { createEslintRule, getAccessorValue } from '../utils' +import { parseVitestFnCall } from '../utils/parse-vitest-fn-call' + +export const RULE_NAME = 'prefer-called-times' +type MESSAGE_IDS = 'preferCalledTimes' +type Options = [] + +export default createEslintRule({ + name: RULE_NAME, + meta: { + docs: { + description: + 'enforce using `toBeCalledTimes(1)` or `toHaveBeenCalledTimes(1)`', + recommended: false, + }, + messages: { + preferCalledTimes: 'Prefer {{ replacedMatcherName }}(1)', + }, + type: 'suggestion', + fixable: 'code', + schema: [], + }, + defaultOptions: [], + create(context) { + return { + CallExpression(node) { + const vitestFnCall = parseVitestFnCall(node, context) + + if (vitestFnCall?.type !== 'expect') return + + const { matcher } = vitestFnCall + const matcherName = getAccessorValue(matcher) + + if (['toBeCalledOnce', 'toHaveBeenCalledOnce'].includes(matcherName)) { + const replacedMatcherName = matcherName.replace('Once', 'Times') + + context.report({ + data: { replacedMatcherName }, + messageId: 'preferCalledTimes', + node: matcher, + fix: (fixer) => [ + fixer.replaceText(matcher, replacedMatcherName), + fixer.insertTextAfterRange( + [ + vitestFnCall.matcher.range[0], + vitestFnCall.matcher.range[1] + 1, + ], + '1', + ), + ], + }) + } + }, + } + }, +}) diff --git a/src/rules/require-local-test-context-for-concurrent-snapshots.ts b/src/rules/require-local-test-context-for-concurrent-snapshots.ts index ba1aa1ce..57dfe1d2 100644 --- a/src/rules/require-local-test-context-for-concurrent-snapshots.ts +++ b/src/rules/require-local-test-context-for-concurrent-snapshots.ts @@ -12,7 +12,7 @@ export default createEslintRule({ meta: { docs: { description: 'require local Test Context for concurrent snapshot tests', - recommended: false, + recommended: true, }, messages: { requireLocalTestContext: 'Use local Test Context instead', diff --git a/src/rules/valid-describe-callback.ts b/src/rules/valid-describe-callback.ts index 0ff52fac..fe5bef16 100644 --- a/src/rules/valid-describe-callback.ts +++ b/src/rules/valid-describe-callback.ts @@ -62,7 +62,7 @@ export default createEslintRule({ type: 'problem', docs: { description: 'enforce valid describe callback', - recommended: false, + recommended: true, }, messages: { nameAndCallback: 'Describe requires a name and callback arguments', diff --git a/src/rules/valid-expect.ts b/src/rules/valid-expect.ts index 0f724e6d..f9a543f7 100644 --- a/src/rules/valid-expect.ts +++ b/src/rules/valid-expect.ts @@ -134,7 +134,7 @@ export default createEslintRule< meta: { docs: { description: 'enforce valid `expect()` usage', - recommended: false, + recommended: true, }, messages: { tooManyArgs: 'Expect takes at most {{ amount}} argument{{ s }}', diff --git a/src/rules/valid-title.ts b/src/rules/valid-title.ts index 6758a909..8aa7a087 100644 --- a/src/rules/valid-title.ts +++ b/src/rules/valid-title.ts @@ -124,7 +124,7 @@ export default createEslintRule({ meta: { docs: { description: 'enforce valid titles', - recommended: false, + recommended: true, }, messages: { titleMustBeString: diff --git a/tests/prefer-called-times.test.ts b/tests/prefer-called-times.test.ts new file mode 100644 index 00000000..0822ac8d --- /dev/null +++ b/tests/prefer-called-times.test.ts @@ -0,0 +1,94 @@ +import rule, { RULE_NAME } from '../src/rules/prefer-called-times' +import { ruleTester } from './ruleTester' + +ruleTester.run(RULE_NAME, rule, { + valid: [ + 'expect(fn).toBeCalledTimes(1);', + 'expect(fn).toHaveBeenCalledTimes(1);', + 'expect(fn).toBeCalledTimes(2);', + 'expect(fn).toHaveBeenCalledTimes(2);', + 'expect(fn).toBeCalledTimes(expect.anything());', + 'expect(fn).toHaveBeenCalledTimes(expect.anything());', + 'expect(fn).not.toBeCalledTimes(2);', + 'expect(fn).rejects.not.toBeCalledTimes(1);', + 'expect(fn).not.toHaveBeenCalledTimes(1);', + 'expect(fn).resolves.not.toHaveBeenCalledTimes(1);', + 'expect(fn).toBeCalledTimes(0);', + 'expect(fn).toHaveBeenCalledTimes(0);', + 'expect(fn);', + ], + invalid: [ + { + code: 'expect(fn).toBeCalledOnce();', + errors: [ + { + messageId: 'preferCalledTimes', + data: { replacedMatcherName: 'toBeCalledTimes' }, + column: 12, + line: 1, + }, + ], + output: 'expect(fn).toBeCalledTimes(1);', + }, + { + code: 'expect(fn).toHaveBeenCalledOnce();', + errors: [ + { + messageId: 'preferCalledTimes', + data: { replacedMatcherName: 'toHaveBeenCalledTimes' }, + column: 12, + line: 1, + }, + ], + output: 'expect(fn).toHaveBeenCalledTimes(1);', + }, + { + code: 'expect(fn).not.toBeCalledOnce();', + errors: [ + { + messageId: 'preferCalledTimes', + data: { replacedMatcherName: 'toBeCalledTimes' }, + column: 16, + line: 1, + }, + ], + output: 'expect(fn).not.toBeCalledTimes(1);', + }, + { + code: 'expect(fn).not.toHaveBeenCalledOnce();', + errors: [ + { + messageId: 'preferCalledTimes', + data: { replacedMatcherName: 'toHaveBeenCalledTimes' }, + column: 16, + line: 1, + }, + ], + output: 'expect(fn).not.toHaveBeenCalledTimes(1);', + }, + { + code: 'expect(fn).resolves.toBeCalledOnce();', + errors: [ + { + messageId: 'preferCalledTimes', + data: { replacedMatcherName: 'toBeCalledTimes' }, + column: 21, + line: 1, + }, + ], + output: 'expect(fn).resolves.toBeCalledTimes(1);', + }, + { + code: 'expect(fn).resolves.toHaveBeenCalledOnce();', + errors: [ + { + messageId: 'preferCalledTimes', + data: { replacedMatcherName: 'toHaveBeenCalledTimes' }, + column: 21, + line: 1, + }, + ], + output: 'expect(fn).resolves.toHaveBeenCalledTimes(1);', + }, + ], +})