Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: assert that async expects are awaited or returned (#255)
Fixes #54
Closes #254
  • Loading branch information
yatki authored and SimenB committed Jul 20, 2019
1 parent 3ee3874 commit 7ae98f5
Show file tree
Hide file tree
Showing 17 changed files with 674 additions and 42 deletions.
63 changes: 62 additions & 1 deletion docs/rules/valid-expect.md
Expand Up @@ -20,8 +20,56 @@ or when a matcher function was not called, e.g.:
expect(true).toBeDefined;
```

or when an async assertion was not `await`ed or returned, e.g.:

```js
expect(Promise.resolve('Hi!')).resolves.toBe('Hi!');
```

This rule is enabled by default.

## Options

```js
{
type: 'object',
properties: {
alwaysAwait: {
type: 'boolean',
default: false,
},
},
additionalProperties: false,
}
```

### `alwaysAwait`

Enforces to use `await` inside block statements. Using `return` will trigger a
warning. Returning one line statements with arrow functions is _always allowed_.

Examples of **incorrect** code for the { "alwaysAwait": **true** } option:

```js
// alwaysAwait: true
test('test1', async () => {
await expect(Promise.resolve(2)).resolves.toBeDefined();
return expect(Promise.resolve(1)).resolves.toBe(1); // `return` statement will trigger a warning
});
```

Examples of **correct** code for the { "alwaysAwait": **true** } option:

```js
// alwaysAwait: true
test('test1', async () => {
await expect(Promise.resolve(2)).resolves.toBeDefined();
await expect(Promise.resolve(1)).resolves.toBe(1);
});

test('test2', () => expect(Promise.resolve(2)).resolves.toBe(2));
```

### Default configuration

The following patterns are considered warnings:
Expand All @@ -33,6 +81,12 @@ expect('something', 'else');
expect('something');
expect(true).toBeDefined;
expect(Promise.resolve('hello')).resolves;
expect(Promise.resolve('hello')).resolves.toEqual('hello');
Promise.resolve(expect(Promise.resolve('hello')).resolves.toEqual('hello'));
Promise.all([
expect(Promise.resolve('hello')).resolves.toEqual('hello'),
expect(Promise.resolve('hi')).resolves.toEqual('hi'),
]);
```

The following patterns are not warnings:
Expand All @@ -41,5 +95,12 @@ The following patterns are not warnings:
expect('something').toEqual('something');
expect([1, 2, 3]).toEqual([1, 2, 3]);
expect(true).toBeDefined();
expect(Promise.resolve('hello')).resolves.toEqual('hello');
await expect(Promise.resolve('hello')).resolves.toEqual('hello');
await Promise.resolve(
expect(Promise.resolve('hello')).resolves.toEqual('hello'),
);
await Promise.all(
expect(Promise.resolve('hello')).resolves.toEqual('hello'),
expect(Promise.resolve('hi')).resolves.toEqual('hi'),
);
```
1 change: 1 addition & 0 deletions src/rules/__tests__/no-alias-methods.test.js
Expand Up @@ -17,6 +17,7 @@ ruleTester.run('no-alias-methods', rule, {
'expect(a).toHaveNthReturnedWith()',
'expect(a).toThrow()',
'expect(a).rejects;',
'expect(a);',
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions src/rules/__tests__/no-truthy-falsy.test.js
Expand Up @@ -13,6 +13,7 @@ ruleTester.run('no-truthy-falsy', rule, {
'expect("anything").not.toEqual(true);',
'expect(Promise.resolve({})).resolves.toBe(true);',
'expect(Promise.reject({})).rejects.toBe(true);',
'expect(a);',
],

invalid: [
Expand Down
Expand Up @@ -15,6 +15,7 @@ ruleTester.run('prefer-called-with', rule, {
'expect(fn).not.toHaveBeenCalledWith();',
'expect(fn).toBeCalledTimes(0);',
'expect(fn).toHaveBeenCalledTimes(0);',
'expect(fn);',
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions src/rules/__tests__/prefer-strict-equal.test.js
Expand Up @@ -7,6 +7,7 @@ ruleTester.run('prefer-strict-equal', rule, {
valid: [
'expect(something).toStrictEqual(somethingElse);',
"a().toEqual('b')",
'expect(a);',
],
invalid: [
{
Expand Down
1 change: 1 addition & 0 deletions src/rules/__tests__/prefer-to-contain.test.js
Expand Up @@ -23,6 +23,7 @@ ruleTester.run('prefer-to-contain', rule, {
`expect(a.test(b)).resolves.toEqual(true)`,
`expect(a.test(b)).resolves.not.toEqual(true)`,
`expect(a).not.toContain(b)`,
'expect(a);',
],
invalid: [
{
Expand Down
1 change: 1 addition & 0 deletions src/rules/__tests__/prefer-to-have-length.test.js
Expand Up @@ -10,6 +10,7 @@ ruleTester.run('prefer-to-have-length', rule, {
'expect(result).toBe(true);',
`expect(user.getUserName(5)).resolves.toEqual('Paul')`,
`expect(user.getUserName(5)).rejects.toEqual('Paul')`,
'expect(a);',
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions src/rules/__tests__/require-tothrow-message.test.js
Expand Up @@ -57,6 +57,7 @@ ruleTester.run('require-tothrow-message', rule, {
await expect(throwErrorAsync()).resolves.not.toThrow();
await expect(throwErrorAsync()).resolves.not.toThrowError();
})`,
'expect(a);',
],

invalid: [
Expand Down

0 comments on commit 7ae98f5

Please sign in to comment.