Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert that async expects are awaited or returned - Closes #54, #254 #255

Merged
merged 27 commits into from Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9a060f3
fix(utility-functions): fix & add new functions
yatki May 9, 2019
763f6dd
refactor: update existing rules
yatki May 9, 2019
b3dcfcc
feat: improve valid-expect to check async assertions
yatki May 9, 2019
c12d647
feat: enable alwaysAwait option for valid-expect
yatki May 9, 2019
4d86484
chore: disable alwaysAwait & update tests
yatki May 9, 2019
656a31b
chore: remove redundant comment
yatki May 9, 2019
425c360
chore: improve code readability
yatki May 9, 2019
36ffd8c
fix(utility functions): add property checks
yatki May 18, 2019
cf055a4
feat: add validation for promise.x statements
yatki May 18, 2019
b817a33
test: extend tests for promise.x usages for valid-expect
yatki May 18, 2019
5e7009f
chore: enable valid-expect options by default
yatki May 18, 2019
517513e
chore: add default option values to valid-expect
yatki May 19, 2019
d2d8feb
test: update tests for valid-expect
yatki May 19, 2019
e406140
fix: preset option value for valid-expect
yatki May 19, 2019
55a9851
test: improve code coverage
yatki May 19, 2019
035880d
chore: rename ignoreInPromise option to allowPromiseMethods
yatki May 25, 2019
baa25e7
docs: add valid-expect option details
yatki May 25, 2019
82696ba
docs: improve valid-expect docs
yatki May 28, 2019
8c321bc
revert: remove allowPromiseMethods option from valid-expect
yatki May 28, 2019
ee6f2cc
refactor: improve variable declarations
yatki May 28, 2019
1634f15
chore: use messageIds for async assertion messages
yatki Jul 15, 2019
ff4d639
chore: add failing test
SimenB Jul 16, 2019
faa2f7a
feat: add expectCaseWithParent util function
yatki Jul 19, 2019
973bcf4
fix: rename prefer-called-with-test & add test case
yatki Jul 19, 2019
84e054e
fix: use right util function for rules & add test case
yatki Jul 19, 2019
ef9db48
Merge branch 'master' into 54-valid-async-expect
SimenB Jul 20, 2019
e6d19ef
chore: fix test
SimenB Jul 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'),
);
```