Skip to content

Commit

Permalink
feat: allow passing arguments as title (#187)
Browse files Browse the repository at this point in the history
* feat: allow passing arguments as title

* docs

* chore: downgrade node types

* Revert "chore: downgrade node types"

This reverts commit fdc4954.
  • Loading branch information
so1ve committed May 22, 2023
1 parent ea17f67 commit 6d6e797
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 18 deletions.
19 changes: 18 additions & 1 deletion docs/rules/valid-title.md
Expand Up @@ -19,7 +19,8 @@ This rule has an object option:
"vitest/valid-title": [
"error",
{
"ignoreTypeOfDescribeName": false,
"ignoreTypeOfDescribeName": false,
"allowArguments": false,
"disallowedWords": ["skip", "only"],
"mustNotMatch": ["^\\s+$", "^\\s*\\d+\\s*$"],
"mustMatch": ["^\\s*\\w+\\s*$"]
Expand Down Expand Up @@ -52,6 +53,22 @@ describe('1', () => {
})
```

### `allowArguments`

If `true`, the rule ignores the arguments of `describe` function.

Examples of **correct** code for this rule with the `{ "allowArguments": false }` option:

```js
describe('name', () => {})
```

Examples of **correct** code for this rule with the `{ "allowArguments": true }` option:

```js
describe(foo, () => {})
```

### `disallowedWords`

An array of words that are not allowed in the test title.
Expand Down
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions src/rules/valid-title.test.ts
Expand Up @@ -99,6 +99,51 @@ describe(RULE_NAME, () => {
})
})

it(`${RULE_NAME} - support passing an argument as title`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: 'it(foo, () => {});',
options: [
{
allowArguments: true
}
]
},
{
code: 'describe(bar, () => {});',
options: [
{
allowArguments: true
}
]
},
{
code: 'test(baz, () => {});',
options: [
{
allowArguments: true
}
]
}
],
invalid: [
{
code: 'test(bar, () => {});',
options: [{ allowArguments: false }],
errors: [
{
messageId: 'titleMustBeString',
data: { word: 'correct' },
column: 6,
line: 1
}
]
}
]
})
})

it(`${RULE_NAME} - mustMatch & mustNotMatch options`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [
Expand Down
10 changes: 8 additions & 2 deletions src/rules/valid-title.ts
Expand Up @@ -39,6 +39,7 @@ const MatcherAndMessageSchema: JSONSchema.JSONSchema7 = {

type Options = {
ignoreTypeOfDescribeName?: boolean;
allowArguments?: boolean;
disallowedWords?: string[];
mustNotMatch?:
| Partial<Record<MatcherGroups, string | MatcherAndMessage>>
Expand Down Expand Up @@ -119,6 +120,10 @@ export default createEslintRule<Options, MESSAGE_IDS>({
type: 'boolean',
default: false
},
allowArguments: {
type: 'boolean',
default: false
},
disallowedWords: {
type: 'array',
items: { type: 'string' }
Expand All @@ -144,10 +149,11 @@ export default createEslintRule<Options, MESSAGE_IDS>({
],
fixable: 'code'
},
defaultOptions: [{ ignoreTypeOfDescribeName: false, disallowedWords: [] }],
defaultOptions: [{ ignoreTypeOfDescribeName: false, allowArguments: false, disallowedWords: [] }],
create(context, [
{
ignoreTypeOfDescribeName,
allowArguments,
disallowedWords = [],
mustNotMatch,
mustMatch
Expand All @@ -166,7 +172,7 @@ export default createEslintRule<Options, MESSAGE_IDS>({

const [argument] = node.arguments

if (!argument) return
if (!argument || (allowArguments && argument.type === AST_NODE_TYPES.Identifier)) return

if (!isStringNode(argument)) {
if (argument.type === AST_NODE_TYPES.BinaryExpression &&
Expand Down

0 comments on commit 6d6e797

Please sign in to comment.