Skip to content

Commit

Permalink
feat(prefer-expect-assertions): add asyncOnly option to prefer-expect…
Browse files Browse the repository at this point in the history
…-assertions rule
  • Loading branch information
Mario Campa committed Sep 25, 2020
1 parent c8a040a commit 01e41de
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
14 changes: 14 additions & 0 deletions docs/rules/prefer-expect-assertions.md
Expand Up @@ -55,3 +55,17 @@ test('my test', () => {
expect(someThing()).toEqual('foo');
});
```

## Options

#### `asyncOnly`

Run this rule only on async test functions

```json
{
"rules": {
"jest/prefer-expect-assertions": ["error", { "asyncOnly": true }]
}
}
```
39 changes: 38 additions & 1 deletion src/rules/__tests__/prefer-expect-assertions.test.ts
Expand Up @@ -6,7 +6,7 @@ import rule from '../prefer-expect-assertions';
const ruleTester = new TSESLint.RuleTester({
parser: resolveFrom(require.resolve('eslint'), 'espree'),
parserOptions: {
ecmaVersion: 2015,
ecmaVersion: 2017,
},
});

Expand Down Expand Up @@ -205,6 +205,21 @@ ruleTester.run('prefer-expect-assertions', rule, {
},
],
},
{
code: dedent`
it("it1", async function() {
expect(someValue).toBe(true)
})
`,
options: [{ asyncOnly: true }],
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 1,
},
],
},
],

valid: [
Expand All @@ -223,5 +238,27 @@ ruleTester.run('prefer-expect-assertions', rule, {
'test("it1")',
'itHappensToStartWithIt("foo", function() {})',
'testSomething("bar", function() {})',
'it(async () => {expect.assertions(0);})',
{
code: dedent`
it("it1", async () => {
expect.assertions(1);
expect(someValue).toBe(true)
})
`,
options: [{ asyncOnly: true }],
},
{
code: dedent`
it("it1", function() {
expect(someValue).toBe(true)
})
`,
options: [{ asyncOnly: true }],
},
{
code: 'it("it1", () => {})',
options: [{ asyncOnly: true }],
},
],
});
25 changes: 21 additions & 4 deletions src/rules/prefer-expect-assertions.ts
Expand Up @@ -46,6 +46,9 @@ interface PreferExpectAssertionsCallExpression extends TSESTree.CallExpression {
TSESTree.ArrowFunctionExpression & { body: TSESTree.BlockStatement },
];
}
interface RuleOptions {
asyncOnly?: boolean;
}

type MessageIds =
| 'hasAssertionsTakesNoArguments'
Expand All @@ -61,7 +64,7 @@ const suggestions: Array<[MessageIds, string]> = [
['suggestAddingAssertions', 'expect.assertions();'],
];

export default createRule<[], MessageIds>({
export default createRule<[RuleOptions], MessageIds>({
name: __filename,
meta: {
docs: {
Expand All @@ -85,14 +88,28 @@ export default createRule<[], MessageIds>({
suggestRemovingExtraArguments: 'Remove extra arguments',
},
type: 'suggestion',
schema: [],
schema: [
{
type: 'object',
properties: {
asyncOnly: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
defaultOptions: [],
create(context) {
defaultOptions: [{ asyncOnly: false }],
create(context, [options]) {
return {
'CallExpression[callee.name=/^(it|test)$/][arguments.1.body.body]'(
node: PreferExpectAssertionsCallExpression,
) {
if (options.asyncOnly && !node.arguments[1].async) {
return;
}

const testFuncBody = node.arguments[1].body.body;

if (!isFirstLineExprStmt(testFuncBody)) {
Expand Down

0 comments on commit 01e41de

Please sign in to comment.