Skip to content

Commit

Permalink
fix(valid-describe-callback): remove noAsyncDescribeCallback (#517)
Browse files Browse the repository at this point in the history
* fix(valid-describe-callback): remove `noAsyncDescribeCallback`

* docs: fix example code
  • Loading branch information
azu authored Aug 26, 2024
1 parent dfbf02a commit 117312f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 58 deletions.
11 changes: 4 additions & 7 deletions docs/rules/valid-describe-callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@

This rule validates the second parameter of a `describe()` function is a callback.

- should not be async
- should not contain parameters
- should not contain any `return` statements

The following are considered warnings:

```js
// async callback functions are not allowed
describe("myfunc", async () => {
//
})

// callback function parameters are not allowed
describe("myfunc", done => {
//
Expand All @@ -43,9 +37,12 @@ describe("myfunc", () => {
The following are not considered warnings:

```js
describe("myfunc", async () => {
//
})
describe("myfunc", () => {
it("should do something", () => {
//
})
})
```
```
9 changes: 0 additions & 9 deletions src/rules/valid-describe-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const RULE_NAME = 'valid-describe-callback'
type MESSAGE_IDS =
| 'nameAndCallback'
| 'secondArgumentMustBeFunction'
| 'noAsyncDescribeCallback'
| 'unexpectedDescribeArgument'
| 'unexpectedReturnInDescribe'

Expand All @@ -33,7 +32,6 @@ export default createEslintRule<Options, MESSAGE_IDS>({
messages: {
nameAndCallback: 'Describe requires a name and callback arguments',
secondArgumentMustBeFunction: 'Second argument must be a function',
noAsyncDescribeCallback: 'Describe callback cannot be async',
unexpectedDescribeArgument: 'Unexpected argument in describe callback',
unexpectedReturnInDescribe: 'Unexpected return statement in describe callback'
},
Expand Down Expand Up @@ -75,13 +73,6 @@ export default createEslintRule<Options, MESSAGE_IDS>({
return
}

if (callback.async) {
context.report({
messageId: 'noAsyncDescribeCallback',
node: callback
})
}

if (vitestFnCall.members.every(s => getAccessorValue(s) !== 'each')
&& callback.params.length) {
context.report({
Expand Down
61 changes: 19 additions & 42 deletions tests/valid-describe-callback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ ruleTester.run(RULE_NAME, rule, {
foo | foe
${1} | ${2}
\`('$something', ({ foo, foe }) => {});
`,
// https://github.com/vitest-dev/eslint-plugin-vitest/issues/511
'describe("foo", async () => {})',
'describe("foo", async function () {})',
'xdescribe("foo", async function () {})',
'fdescribe("foo", async function () {})',
'describe.only("foo", async function () {})',
'describe.skip("foo", async function () {})',
`describe('sample case', () => {
it('works', () => {
expect(true).toEqual(true);
});
describe('async', () => {
it('works', async () => {
await new Promise(setImmediate);
expect(true).toEqual(true);
});
});
});
`
],
invalid: [
Expand Down Expand Up @@ -90,46 +109,6 @@ ruleTester.run(RULE_NAME, rule, {
code: 'describe()',
errors: [{ messageId: 'nameAndCallback', line: 1, column: 1 }]
},
{
code: 'describe("foo", async () => {})',
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 17 }]
},
{
code: 'describe("foo", async function () {})',
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 17 }]
},
{
code: 'xdescribe("foo", async function () {})',
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 18 }]
},
{
code: 'fdescribe("foo", async function () {})',
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 18 }]
},
{
code: 'describe.only("foo", async function () {})',
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 22 }]
},
{
code: 'describe.skip("foo", async function () {})',
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 22 }]
},
{
code: `
describe('sample case', () => {
it('works', () => {
expect(true).toEqual(true);
});
describe('async', async () => {
await new Promise(setImmediate);
it('breaks', () => {
throw new Error('Fail');
});
});
});
`,
errors: [{ messageId: 'noAsyncDescribeCallback', line: 6, column: 25 }]
},
{
code: `
describe('foo', function () {
Expand Down Expand Up @@ -179,7 +158,6 @@ ruleTester.run(RULE_NAME, rule, {
})
`,
errors: [
{ messageId: 'noAsyncDescribeCallback', line: 2, column: 24 },
{ messageId: 'unexpectedReturnInDescribe', line: 6, column: 9 }
]
},
Expand Down Expand Up @@ -214,7 +192,6 @@ ruleTester.run(RULE_NAME, rule, {
{
code: 'describe("foo", async function (done) {})',
errors: [
{ messageId: 'noAsyncDescribeCallback', line: 1, column: 17 },
{ messageId: 'unexpectedDescribeArgument', line: 1, column: 17 }
]
}
Expand Down

0 comments on commit 117312f

Please sign in to comment.