Skip to content

Commit

Permalink
feat: new rule no-import-node-test (#317)
Browse files Browse the repository at this point in the history
* feat: new rule `no-import-node-test`

* chore: docs

* chore: update
  • Loading branch information
antfu authored Dec 11, 2023
1 parent 68c2b8f commit ccd7e88
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export default [
| [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-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 | | 🌐 | | |
Expand Down
32 changes: 32 additions & 0 deletions docs/rules/no-import-node-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Disallow importing `node:test` (`vitest/no-import-node-test`)

⚠️ 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).

<!-- end auto-generated rule header -->

## Rule Details

This rule warns when `node:test` is imported (usually accidentally). With `--fix`, it will replace the import with `vitest`.

Examples of **incorrect** code for this rule:

```ts
import { test } from 'node:test'
import { expect } from 'vitest'

test('foo', () => {
expect(1).toBe(1)
})
```

Examples of **correct** code for this rule:

```ts
import { test, expect } from 'vitest'

test('foo', () => {
expect(1).toBe(1)
})
```
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import consistentTestFilename, { RULE_NAME as useConsistentTestFilename } from '
import maxExpect, { RULE_NAME as maxExpectName } from './rules/max-expects'
import noAliasMethod, { RULE_NAME as noAliasMethodName } from './rules/no-alias-methods'
import noCommentedOutTests, { RULE_NAME as noCommentedOutTestsName } from './rules/no-commented-out-tests'
import noConditonalExpect, { RULE_NAME as noConditonalExpectName } from './rules/no-conditional-expect'
import noConditionalExpect, { RULE_NAME as noConditionalExpectName } from './rules/no-conditional-expect'
import noImportNodeTest, { RULE_NAME as noImportNodeTestName } from './rules/no-import-node-test'
import noConditionalInTest, { RULE_NAME as noConditionalInTestName } from './rules/no-conditional-in-test'
import noDisabledTests, { RULE_NAME as noDisabledTestsName } from './rules/no-disabled-tests'
import noDoneCallback, { RULE_NAME as noDoneCallbackName } from './rules/no-done-callback'
Expand Down Expand Up @@ -71,7 +72,7 @@ const allRules = {
[useConsistentTestFilename]: 'warn',
[maxExpectName]: 'warn',
[noAliasMethodName]: 'warn',
[noConditonalExpectName]: 'warn',
[noConditionalExpectName]: 'warn',
[noConditionalInTestName]: 'warn',
[noDisabledTestsName]: 'warn',
[noDoneCallbackName]: 'warn',
Expand All @@ -83,6 +84,7 @@ const allRules = {
[noStandaloneExpectName]: 'warn',
[noTestPrefixesName]: 'warn',
[noTestReturnStatementName]: 'warn',
[noImportNodeTestName]: 'warn',
[preferCalledWithName]: 'warn',
[preferToBeFalsyName]: 'warn',
[preferToBeObjectName]: 'warn',
Expand Down Expand Up @@ -134,7 +136,7 @@ export default {
[maxExpectName]: maxExpect,
[noAliasMethodName]: noAliasMethod,
[noCommentedOutTestsName]: noCommentedOutTests,
[noConditonalExpectName]: noConditonalExpect,
[noConditionalExpectName]: noConditionalExpect,
[noConditionalInTestName]: noConditionalInTest,
[noDisabledTestsName]: noDisabledTests,
[noDoneCallbackName]: noDoneCallback,
Expand All @@ -146,6 +148,7 @@ export default {
[noStandaloneExpectName]: noStandaloneExpect,
[noTestPrefixesName]: noTestPrefixes,
[noTestReturnStatementName]: noTestReturnStatement,
[noImportNodeTestName]: noImportNodeTest,
[preferCalledWithName]: preferCalledWith,
[validTitleName]: validTitle,
[validExpectName]: validExpect,
Expand Down
38 changes: 38 additions & 0 deletions src/rules/no-import-node-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createEslintRule } from '../utils'

export const RULE_NAME = 'no-import-node-test'
export type MESSAGE_IDS = 'noImportNodeTest'
export type Options = []

export default createEslintRule<Options, MESSAGE_IDS>({
name: RULE_NAME,
meta: {
docs: {
description: 'Disallow importing `node:test`',
recommended: 'warn'
},
type: 'suggestion',
messages: {
noImportNodeTest: 'Import from `vitest` instead of `node:test`'
},
fixable: 'code',
schema: []
},
defaultOptions: [],
create(context) {
return {
ImportDeclaration(node) {
if (node.source.value === 'node:test') {
context.report({
messageId: 'noImportNodeTest',
node,
fix: fixer => fixer.replaceText(
node.source,
node.source.raw.replace('node:test', 'vitest')
)
})
}
}
}
}
})
20 changes: 20 additions & 0 deletions tests/no-import-node-test.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import rule, { RULE_NAME } from '../src/rules/no-import-node-test'
import { ruleTester } from './ruleTester'

ruleTester.run(RULE_NAME, rule, {
valid: [
'import { test } from "vitest"'
],
invalid: [
{
code: 'import { test } from "node:test"',
output: 'import { test } from "vitest"',
errors: [{ messageId: 'noImportNodeTest' }]
},
{
code: 'import * as foo from \'node:test\'',
output: 'import * as foo from \'vitest\'',
errors: [{ messageId: 'noImportNodeTest' }]
}
]
})

0 comments on commit ccd7e88

Please sign in to comment.