Skip to content

Commit 7ae98f5

Browse files
yatkiSimenB
authored andcommitted
feat: assert that async expects are awaited or returned (#255)
Fixes #54 Closes #254
1 parent 3ee3874 commit 7ae98f5

17 files changed

+674
-42
lines changed

docs/rules/valid-expect.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,56 @@ or when a matcher function was not called, e.g.:
2020
expect(true).toBeDefined;
2121
```
2222

23+
or when an async assertion was not `await`ed or returned, e.g.:
24+
25+
```js
26+
expect(Promise.resolve('Hi!')).resolves.toBe('Hi!');
27+
```
28+
2329
This rule is enabled by default.
2430

31+
## Options
32+
33+
```js
34+
{
35+
type: 'object',
36+
properties: {
37+
alwaysAwait: {
38+
type: 'boolean',
39+
default: false,
40+
},
41+
},
42+
additionalProperties: false,
43+
}
44+
```
45+
46+
### `alwaysAwait`
47+
48+
Enforces to use `await` inside block statements. Using `return` will trigger a
49+
warning. Returning one line statements with arrow functions is _always allowed_.
50+
51+
Examples of **incorrect** code for the { "alwaysAwait": **true** } option:
52+
53+
```js
54+
// alwaysAwait: true
55+
test('test1', async () => {
56+
await expect(Promise.resolve(2)).resolves.toBeDefined();
57+
return expect(Promise.resolve(1)).resolves.toBe(1); // `return` statement will trigger a warning
58+
});
59+
```
60+
61+
Examples of **correct** code for the { "alwaysAwait": **true** } option:
62+
63+
```js
64+
// alwaysAwait: true
65+
test('test1', async () => {
66+
await expect(Promise.resolve(2)).resolves.toBeDefined();
67+
await expect(Promise.resolve(1)).resolves.toBe(1);
68+
});
69+
70+
test('test2', () => expect(Promise.resolve(2)).resolves.toBe(2));
71+
```
72+
2573
### Default configuration
2674

2775
The following patterns are considered warnings:
@@ -33,6 +81,12 @@ expect('something', 'else');
3381
expect('something');
3482
expect(true).toBeDefined;
3583
expect(Promise.resolve('hello')).resolves;
84+
expect(Promise.resolve('hello')).resolves.toEqual('hello');
85+
Promise.resolve(expect(Promise.resolve('hello')).resolves.toEqual('hello'));
86+
Promise.all([
87+
expect(Promise.resolve('hello')).resolves.toEqual('hello'),
88+
expect(Promise.resolve('hi')).resolves.toEqual('hi'),
89+
]);
3690
```
3791

3892
The following patterns are not warnings:
@@ -41,5 +95,12 @@ The following patterns are not warnings:
4195
expect('something').toEqual('something');
4296
expect([1, 2, 3]).toEqual([1, 2, 3]);
4397
expect(true).toBeDefined();
44-
expect(Promise.resolve('hello')).resolves.toEqual('hello');
98+
await expect(Promise.resolve('hello')).resolves.toEqual('hello');
99+
await Promise.resolve(
100+
expect(Promise.resolve('hello')).resolves.toEqual('hello'),
101+
);
102+
await Promise.all(
103+
expect(Promise.resolve('hello')).resolves.toEqual('hello'),
104+
expect(Promise.resolve('hi')).resolves.toEqual('hi'),
105+
);
45106
```

src/rules/__tests__/no-alias-methods.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ruleTester.run('no-alias-methods', rule, {
1717
'expect(a).toHaveNthReturnedWith()',
1818
'expect(a).toThrow()',
1919
'expect(a).rejects;',
20+
'expect(a);',
2021
],
2122

2223
invalid: [

src/rules/__tests__/no-truthy-falsy.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ruleTester.run('no-truthy-falsy', rule, {
1313
'expect("anything").not.toEqual(true);',
1414
'expect(Promise.resolve({})).resolves.toBe(true);',
1515
'expect(Promise.reject({})).rejects.toBe(true);',
16+
'expect(a);',
1617
],
1718

1819
invalid: [

src/rules/__tests__/prefer-called-with.js renamed to src/rules/__tests__/prefer-called-with.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ruleTester.run('prefer-called-with', rule, {
1515
'expect(fn).not.toHaveBeenCalledWith();',
1616
'expect(fn).toBeCalledTimes(0);',
1717
'expect(fn).toHaveBeenCalledTimes(0);',
18+
'expect(fn);',
1819
],
1920

2021
invalid: [

src/rules/__tests__/prefer-strict-equal.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ruleTester.run('prefer-strict-equal', rule, {
77
valid: [
88
'expect(something).toStrictEqual(somethingElse);',
99
"a().toEqual('b')",
10+
'expect(a);',
1011
],
1112
invalid: [
1213
{

src/rules/__tests__/prefer-to-contain.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ruleTester.run('prefer-to-contain', rule, {
2323
`expect(a.test(b)).resolves.toEqual(true)`,
2424
`expect(a.test(b)).resolves.not.toEqual(true)`,
2525
`expect(a).not.toContain(b)`,
26+
'expect(a);',
2627
],
2728
invalid: [
2829
{

src/rules/__tests__/prefer-to-have-length.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ruleTester.run('prefer-to-have-length', rule, {
1010
'expect(result).toBe(true);',
1111
`expect(user.getUserName(5)).resolves.toEqual('Paul')`,
1212
`expect(user.getUserName(5)).rejects.toEqual('Paul')`,
13+
'expect(a);',
1314
],
1415

1516
invalid: [

src/rules/__tests__/require-tothrow-message.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ ruleTester.run('require-tothrow-message', rule, {
5757
await expect(throwErrorAsync()).resolves.not.toThrow();
5858
await expect(throwErrorAsync()).resolves.not.toThrowError();
5959
})`,
60+
'expect(a);',
6061
],
6162

6263
invalid: [

0 commit comments

Comments
 (0)