Skip to content

Commit ccd7e88

Browse files
authoredDec 11, 2023
feat: new rule no-import-node-test (#317)
* feat: new rule `no-import-node-test` * chore: docs * chore: update
1 parent 68c2b8f commit ccd7e88

File tree

5 files changed

+97
-3
lines changed

5 files changed

+97
-3
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export default [
138138
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | | 🌐 | 🔧 | |
139139
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | 🌐 | | |
140140
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles || | 🔧 | |
141+
| [no-import-node-test](docs/rules/no-import-node-test.md) | Disallow importing `node:test` | | 🌐 | 🔧 | |
141142
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation in snapshots | | 🌐 | 🔧 | |
142143
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | Disallow large snapshots | | 🌐 | | |
143144
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow importing from __mocks__ directory | | 🌐 | | |

‎docs/rules/no-import-node-test.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Disallow importing `node:test` (`vitest/no-import-node-test`)
2+
3+
⚠️ This rule _warns_ in the 🌐 `all` config.
4+
5+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
6+
7+
<!-- end auto-generated rule header -->
8+
9+
## Rule Details
10+
11+
This rule warns when `node:test` is imported (usually accidentally). With `--fix`, it will replace the import with `vitest`.
12+
13+
Examples of **incorrect** code for this rule:
14+
15+
```ts
16+
import { test } from 'node:test'
17+
import { expect } from 'vitest'
18+
19+
test('foo', () => {
20+
expect(1).toBe(1)
21+
})
22+
```
23+
24+
Examples of **correct** code for this rule:
25+
26+
```ts
27+
import { test, expect } from 'vitest'
28+
29+
test('foo', () => {
30+
expect(1).toBe(1)
31+
})
32+
```

‎src/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import consistentTestFilename, { RULE_NAME as useConsistentTestFilename } from '
1212
import maxExpect, { RULE_NAME as maxExpectName } from './rules/max-expects'
1313
import noAliasMethod, { RULE_NAME as noAliasMethodName } from './rules/no-alias-methods'
1414
import noCommentedOutTests, { RULE_NAME as noCommentedOutTestsName } from './rules/no-commented-out-tests'
15-
import noConditonalExpect, { RULE_NAME as noConditonalExpectName } from './rules/no-conditional-expect'
15+
import noConditionalExpect, { RULE_NAME as noConditionalExpectName } from './rules/no-conditional-expect'
16+
import noImportNodeTest, { RULE_NAME as noImportNodeTestName } from './rules/no-import-node-test'
1617
import noConditionalInTest, { RULE_NAME as noConditionalInTestName } from './rules/no-conditional-in-test'
1718
import noDisabledTests, { RULE_NAME as noDisabledTestsName } from './rules/no-disabled-tests'
1819
import noDoneCallback, { RULE_NAME as noDoneCallbackName } from './rules/no-done-callback'
@@ -71,7 +72,7 @@ const allRules = {
7172
[useConsistentTestFilename]: 'warn',
7273
[maxExpectName]: 'warn',
7374
[noAliasMethodName]: 'warn',
74-
[noConditonalExpectName]: 'warn',
75+
[noConditionalExpectName]: 'warn',
7576
[noConditionalInTestName]: 'warn',
7677
[noDisabledTestsName]: 'warn',
7778
[noDoneCallbackName]: 'warn',
@@ -83,6 +84,7 @@ const allRules = {
8384
[noStandaloneExpectName]: 'warn',
8485
[noTestPrefixesName]: 'warn',
8586
[noTestReturnStatementName]: 'warn',
87+
[noImportNodeTestName]: 'warn',
8688
[preferCalledWithName]: 'warn',
8789
[preferToBeFalsyName]: 'warn',
8890
[preferToBeObjectName]: 'warn',
@@ -134,7 +136,7 @@ export default {
134136
[maxExpectName]: maxExpect,
135137
[noAliasMethodName]: noAliasMethod,
136138
[noCommentedOutTestsName]: noCommentedOutTests,
137-
[noConditonalExpectName]: noConditonalExpect,
139+
[noConditionalExpectName]: noConditionalExpect,
138140
[noConditionalInTestName]: noConditionalInTest,
139141
[noDisabledTestsName]: noDisabledTests,
140142
[noDoneCallbackName]: noDoneCallback,
@@ -146,6 +148,7 @@ export default {
146148
[noStandaloneExpectName]: noStandaloneExpect,
147149
[noTestPrefixesName]: noTestPrefixes,
148150
[noTestReturnStatementName]: noTestReturnStatement,
151+
[noImportNodeTestName]: noImportNodeTest,
149152
[preferCalledWithName]: preferCalledWith,
150153
[validTitleName]: validTitle,
151154
[validExpectName]: validExpect,

‎src/rules/no-import-node-test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { createEslintRule } from '../utils'
2+
3+
export const RULE_NAME = 'no-import-node-test'
4+
export type MESSAGE_IDS = 'noImportNodeTest'
5+
export type Options = []
6+
7+
export default createEslintRule<Options, MESSAGE_IDS>({
8+
name: RULE_NAME,
9+
meta: {
10+
docs: {
11+
description: 'Disallow importing `node:test`',
12+
recommended: 'warn'
13+
},
14+
type: 'suggestion',
15+
messages: {
16+
noImportNodeTest: 'Import from `vitest` instead of `node:test`'
17+
},
18+
fixable: 'code',
19+
schema: []
20+
},
21+
defaultOptions: [],
22+
create(context) {
23+
return {
24+
ImportDeclaration(node) {
25+
if (node.source.value === 'node:test') {
26+
context.report({
27+
messageId: 'noImportNodeTest',
28+
node,
29+
fix: fixer => fixer.replaceText(
30+
node.source,
31+
node.source.raw.replace('node:test', 'vitest')
32+
)
33+
})
34+
}
35+
}
36+
}
37+
}
38+
})

‎tests/no-import-node-test.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import rule, { RULE_NAME } from '../src/rules/no-import-node-test'
2+
import { ruleTester } from './ruleTester'
3+
4+
ruleTester.run(RULE_NAME, rule, {
5+
valid: [
6+
'import { test } from "vitest"'
7+
],
8+
invalid: [
9+
{
10+
code: 'import { test } from "node:test"',
11+
output: 'import { test } from "vitest"',
12+
errors: [{ messageId: 'noImportNodeTest' }]
13+
},
14+
{
15+
code: 'import * as foo from \'node:test\'',
16+
output: 'import * as foo from \'vitest\'',
17+
errors: [{ messageId: 'noImportNodeTest' }]
18+
}
19+
]
20+
})

0 commit comments

Comments
 (0)
Please sign in to comment.