t.expect Method
This method accepts the actual value.
You can pass a value, a Selector’s DOM node state property or a client function promise. TestCafe automatically waits for node state properties to obtain a value and for client functions to execute. See Smart Assertion Query Mechanism for details.
The value passed as the actual
parameter should be a data type the assertion method accepts.
Note
You cannot pass a regular promise to the expect
method unless the options.allowUnawaitedPromise option is enabled.
Only promises the selectors and client functions return can be passed as the assertion’s actual value. If you pass a regular unawaited promise, TestCafe throws an error.
If you need to assert a regular promise, set the allowUnawaitedPromise
option to true
.
await t.expect(new Promise(resolve => setTimeout(resolve, 100))).ok('check that a promise is returned', { allowUnawaitedPromise: true });
The expect
method is followed by an assertion method that accepts an expected value
and optional arguments.
The following assertion methods are available:
- Deep Equal
- Not Deep Equal
- Ok
- Not Ok
- Contains
- Not Contains
- Type of
- Not Type of
- Greater than
- Greater than or Equal to
- Less than
- Less than or Equal to
- Within
- Not Within
- Match
- Not Match
The code snippet below demonstrates how to use expect
method in a test:
import { Selector } from 'testcafe';
fixture`TestController.expect`
.page`https://devexpress.github.io/testcafe/example/`;
test('Check property of element', async t => {
const developerNameInput = Selector('#developer-name');
await t
.expect(developerNameInput.value).eql('', 'input is empty')
.typeText(developerNameInput, 'Peter Parker')
.expect(developerNameInput.value).contains('Peter', 'input contains text "Peter"');
});