Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jest-community/eslint-plugin-jest
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v24.0.2
Choose a base ref
...
head repository: jest-community/eslint-plugin-jest
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v24.1.0
Choose a head ref
  • 8 commits
  • 10 files changed
  • 6 contributors

Commits on Sep 27, 2020

  1. Copy the full SHA
    a64d505 View commit details

Commits on Sep 28, 2020

  1. Copy the full SHA
    e05e381 View commit details

Commits on Oct 1, 2020

  1. Copy the full SHA
    985018e View commit details
  2. Copy the full SHA
    081fc8f View commit details

Commits on Oct 2, 2020

  1. Copy the full SHA
    aa6540e View commit details

Commits on Oct 5, 2020

  1. Copy the full SHA
    387d970 View commit details
  2. feat(prefer-expect-assertions): add onlyFunctionsWithAsyncKeyword o…

    …ption (#677)
    
    Co-authored-by: Mario Campa <mcampa@twitter.com>
    mcampa and Mario Campa authored Oct 5, 2020
    Copy the full SHA
    d0cea37 View commit details
  3. chore(release): 24.1.0 [skip ci]

    # [24.1.0](v24.0.2...v24.1.0) (2020-10-05)
    
    ### Features
    
    * **prefer-expect-assertions:** add `onlyFunctionsWithAsyncKeyword` option ([#677](#677)) ([d0cea37](d0cea37))
    semantic-release-bot committed Oct 5, 2020
    Copy the full SHA
    dde5e6e View commit details
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ jobs:
key: ubuntu-latest-node-12.x-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
ubuntu-latest-node-12.x-yarn-
- uses: actions/setup-node@v2.1.1
- uses: actions/setup-node@v2.1.2
with:
node-version: 12.x
- name: install
@@ -45,6 +45,6 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Danger
uses: danger/danger-js@10.4.0
uses: danger/danger-js@10.5.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ jobs:
restore-keys: |
ubuntu-latest-node-12.x-yarn-
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2.1.1
uses: actions/setup-node@v2.1.2
with:
node-version: ${{ matrix.node-version }}
- name: install with eslint v${{matrix.eslint-version }}
@@ -73,7 +73,7 @@ jobs:
hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-${{ matrix.node-version }}-yarn-
- uses: actions/setup-node@v2.1.1
- uses: actions/setup-node@v2.1.2
with:
node-version: 12.x
- name: install
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ jobs:
key: ubuntu-latest-node-12.x-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
ubuntu-latest-node-12.x-yarn-
- uses: actions/setup-node@v2.1.1
- uses: actions/setup-node@v2.1.2
with:
node-version: 12.x
- name: install
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [24.1.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.0.2...v24.1.0) (2020-10-05)


### Features

* **prefer-expect-assertions:** add `onlyFunctionsWithAsyncKeyword` option ([#677](https://github.com/jest-community/eslint-plugin-jest/issues/677)) ([d0cea37](https://github.com/jest-community/eslint-plugin-jest/commit/d0cea37ae0a8ab07b8082cedbaaf161bcc94c405))

## [24.0.2](https://github.com/jest-community/eslint-plugin-jest/compare/v24.0.1...v24.0.2) (2020-09-20)


42 changes: 42 additions & 0 deletions docs/rules/prefer-expect-assertions.md
Original file line number Diff line number Diff line change
@@ -55,3 +55,45 @@ test('my test', () => {
expect(someThing()).toEqual('foo');
});
```

## Options

#### `onlyFunctionsWithAsyncKeyword`

When `true`, this rule will only warn for tests that use the `async` keyword.

```json
{
"rules": {
"jest/prefer-expect-assertions": [
"warn",
{ "onlyFunctionsWithAsyncKeyword": true }
]
}
}
```

When `onlyFunctionsWithAsyncKeyword` option is set to `true`, the following
pattern would be a warning:

```js
test('my test', async () => {
const result = await someAsyncFunc();
expect(result).toBe('foo');
});
```

While the following patterns would not be considered warnings:

```js
test('my test', () => {
const result = someFunction();
expect(result).toBe('foo');
});

test('my test', async () => {
expect.assertions(1);
const result = await someAsyncFunc();
expect(result).toBe('foo');
});
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-jest",
"version": "24.0.2",
"version": "24.1.0",
"description": "Eslint rules for Jest",
"keywords": [
"eslint",
62 changes: 39 additions & 23 deletions src/rules/__tests__/expect-expect.test.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import {
AST_NODE_TYPES,
TSESLint,
} from '@typescript-eslint/experimental-utils';
import dedent from 'dedent';
import resolveFrom from 'resolve-from';
import rule from '../expect-expect';

@@ -19,8 +20,12 @@ ruleTester.run('expect-expect', rule, {
'it("should pass", () => somePromise().then(() => expect(true).toBeDefined()))',
'it("should pass", myTest); function myTest() { expect(true).toBeDefined() }',
{
code:
'test("should pass", () => { expect(true).toBeDefined(); foo(true).toBe(true); })',
code: dedent`
test('should pass', () => {
expect(true).toBeDefined();
foo(true).toBe(true);
});
`,
options: [{ assertFunctionNames: ['expect', 'foo'] }],
},
{
@@ -32,23 +37,34 @@ ruleTester.run('expect-expect', rule, {
options: [{ assertFunctionNames: ['expect\\$'] }],
},
{
code: `test('verifies expect method call', () => new Foo().expect(123));`,
code: "test('verifies expect method call', () => new Foo().expect(123));",
options: [{ assertFunctionNames: ['Foo.expect'] }],
},
{
code: `test('verifies deep expect method call', () => tester.foo().expect(123));`,
code: dedent`
test('verifies deep expect method call', () => {
tester.foo().expect(123);
});
`,
options: [{ assertFunctionNames: ['tester.foo.expect'] }],
},
{
code: `test('verifies recursive expect method call', () => tester.foo().bar().expect(456));`,
code: dedent`
test('verifies chained expect method call', () => {
tester
.foo()
.bar()
.expect(456);
});
`,
options: [{ assertFunctionNames: ['tester.foo.bar.expect'] }],
},
{
code: [
'test("verifies the function call", () => {',
' td.verify(someFunctionCall())',
'})',
].join('\n'),
code: dedent`
test("verifies the function call", () => {
td.verify(someFunctionCall())
})
`,
options: [{ assertFunctionNames: ['td.verify'] }],
},
{
@@ -125,41 +141,41 @@ ruleTester.run('expect-expect', rule, {
ruleTester.run('wildcards', rule, {
valid: [
{
code: `test('should pass', () => tester.foo().expect(123));`,
code: "test('should pass', () => tester.foo().expect(123));",
options: [{ assertFunctionNames: ['tester.*.expect'] }],
},
{
code: `test('should pass **', () => tester.foo().expect(123));`,
code: "test('should pass **', () => tester.foo().expect(123));",
options: [{ assertFunctionNames: ['**'] }],
},
{
code: `test('should pass *', () => tester.foo().expect(123));`,
code: "test('should pass *', () => tester.foo().expect(123));",
options: [{ assertFunctionNames: ['*'] }],
},
{
code: `test('should pass', () => tester.foo().expect(123));`,
code: "test('should pass', () => tester.foo().expect(123));",
options: [{ assertFunctionNames: ['tester.**'] }],
},
{
code: `test('should pass', () => tester.foo().expect(123));`,
code: "test('should pass', () => tester.foo().expect(123));",
options: [{ assertFunctionNames: ['tester.*'] }],
},
{
code: `test('should pass', () => tester.foo().bar().expectIt(456));`,
code: "test('should pass', () => tester.foo().bar().expectIt(456));",
options: [{ assertFunctionNames: ['tester.**.expect*'] }],
},
{
code: `test('should pass', () => request.get().foo().expect(456));`,
code: "test('should pass', () => request.get().foo().expect(456));",
options: [{ assertFunctionNames: ['request.**.expect'] }],
},
{
code: `test('should pass', () => request.get().foo().expect(456));`,
code: "test('should pass', () => request.get().foo().expect(456));",
options: [{ assertFunctionNames: ['request.**.e*e*t'] }],
},
],
invalid: [
{
code: `test('should fail', () => request.get().foo().expect(456));`,
code: "test('should fail', () => request.get().foo().expect(456));",
options: [{ assertFunctionNames: ['request.*.expect'] }],
errors: [
{
@@ -169,7 +185,7 @@ ruleTester.run('wildcards', rule, {
],
},
{
code: `test('should fail', () => request.get().foo().bar().expect(456));`,
code: "test('should fail', () => request.get().foo().bar().expect(456));",
options: [{ assertFunctionNames: ['request.foo**.expect'] }],
errors: [
{
@@ -179,7 +195,7 @@ ruleTester.run('wildcards', rule, {
],
},
{
code: `test('should fail', () => tester.request(123));`,
code: "test('should fail', () => tester.request(123));",
options: [{ assertFunctionNames: ['request.*'] }],
errors: [
{
@@ -189,7 +205,7 @@ ruleTester.run('wildcards', rule, {
],
},
{
code: `test('should fail', () => request(123));`,
code: "test('should fail', () => request(123));",
options: [{ assertFunctionNames: ['request.*'] }],
errors: [
{
@@ -199,7 +215,7 @@ ruleTester.run('wildcards', rule, {
],
},
{
code: `test('should fail', () => request(123));`,
code: "test('should fail', () => request(123));",
options: [{ assertFunctionNames: ['request.**'] }],
errors: [
{
39 changes: 38 additions & 1 deletion src/rules/__tests__/prefer-expect-assertions.test.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});

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

valid: [
@@ -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: [{ onlyFunctionsWithAsyncKeyword: true }],
},
{
code: dedent`
it("it1", function() {
expect(someValue).toBe(true)
})
`,
options: [{ onlyFunctionsWithAsyncKeyword: true }],
},
{
code: 'it("it1", () => {})',
options: [{ onlyFunctionsWithAsyncKeyword: true }],
},
],
});
25 changes: 21 additions & 4 deletions src/rules/prefer-expect-assertions.ts
Original file line number Diff line number Diff line change
@@ -46,6 +46,9 @@ interface PreferExpectAssertionsCallExpression extends TSESTree.CallExpression {
TSESTree.ArrowFunctionExpression & { body: TSESTree.BlockStatement },
];
}
interface RuleOptions {
onlyFunctionsWithAsyncKeyword?: boolean;
}

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

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

if (!isFirstLineExprStmt(testFuncBody)) {
1,060 changes: 552 additions & 508 deletions yarn.lock

Large diffs are not rendered by default.