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

[docs] Describe the allowUnawaitedPromise assertion option #2355

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions docs/articles/documentation/test-api/assertions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This topic consists of the following sections.
* [Smart Assertion Query Mechanism](#smart-assertion-query-mechanism)
* [Assertion options](#assertion-options)
* [options.timeout](#optionstimeout)
* [options.allowUnawaitedPromise](#optionsallowunawaitedpromise)

## Assertion Structure

Expand All @@ -26,6 +27,8 @@ or a [client function](../obtaining-data-from-the-client/README.md) promise.
TestCafe automatically waits for node state properties to obtain a value and for client functions to execute.
See [Smart Assertion Query Mechanism](#smart-assertion-query-mechanism) for details.

> You cannot pass a regular promise to the `expect` method unless the [options.allowUnawaitedPromise](#optionsallowunawaitedpromise) option is enabled.

Next is an [assertion method](assertion-api.md). Assertion methods accept an expected value
and, optionally, other arguments.

Expand Down Expand Up @@ -62,9 +65,9 @@ You can perform the required assertions immediately after test action is execute
![Synchronous Functional Testing](../../../images/assertions/synchronous-testing.png)

Functional tests are asynchronous on the web. This means that we cannot get the expected changes immediately after an end-user's actions.
+For example, it can take time for the tested web page to send a request to the server for the required data, or an end-user's action launches an animation after which the web page reaches its final state.
+All these intervals cannot be pre-calculated because they depend on various factors: computer performance,
network connection speed, etc. In this case, if we perform assertions immediately after the test action finished,we can get an indefinite result.
For example, it can take time for the tested web page to send a request to the server for the required data, or an end-user's action launches an animation after which the web page reaches its final state.
All these intervals cannot be pre-calculated because they depend on various factors: computer performance,
network connection speed, etc. In this case, if we perform assertions immediately after the test action finished, we can get an indefinite result.

![Asynchronous Functional Testing](../../../images/assertions/asynchronous-testing.png)

Expand Down Expand Up @@ -141,4 +144,18 @@ await t.expect(Selector('#elementId').innerText).eql('text', 'check element text
```

> In addition to built-in assertions, you also can use assertions from Node's built-in [assert](https://nodejs.org/api/assert.html) module or 3rd-party library (for example [chai](http://chaijs.com/)).
> In this case, you specify the time required to complete asynchronous actions using the [t.wait(timeout)](../pausing-the-test.md) method.
> In this case, you specify the time required to complete asynchronous actions using the [t.wait(timeout)](../pausing-the-test.md) method.

### options.allowUnawaitedPromise

Allows a regular promise to be passed to the assertion's `expect` method.

By default, only promises returned by the [selectors](../selecting-page-elements/selectors/using-selectors.md#define-assertion-actual-value)
and [client functions](../obtaining-data-from-the-client/README.md) can be passed as the assertion's actual value.
They trigger the [Smart Assertion Query Mechanism](#smart-assertion-query-mechanism).
If you pass a regular unawaited promise, this will tell TestCafe to compare the promise itself with the expected value.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, It should throws a warning/error as well. /cc @AndreyBelym

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it throws

In case this is what you need, set the `allowUnawaitedPromise` option to `true`.

```js
await t.expect(doSomethingAsync()).ok('check that a promise is returned', { allowUnawaitedPromise: true });
```