t.eval Method

Creates a client function and executes it immediately (without saving). Returns a promise-wrapped value and cannot be chained with other methods of the test controller.

t.eval(fn [, options]) → Promise<any>
Parameter Type Description
fn Function A function to be executed on the client side.
options (optional) Object See Options.

The following example shows how to get a document’s URI with t.eval.

fixture`TestController.eval`
    .page`https://devexpress.github.io/testcafe/example/`;

test('Check URL', async t => {
    const docURI = await t.eval(() => document.documentURI);

    await t.expect(docURI).eql('https://devexpress.github.io/testcafe/example/');
});

Important

Always place eval in a separate call. eval returns a promise-wrapped value instead of a local context so you cannot chain other methods of the test controller after eval. If you chain eval on to other methods of the test controller, their promises don’t ever resolve which breaks the execution order.

fixture`TestController.eval`
    .page`https://devexpress.github.io/testcafe/example/`;

const timeout = 1000;

test('Reload right away', async t => {
    const startTime = Date.now();

    await t
        .wait(timeout)
        .eval(() => location.reload(true));
    // The timeout is skipped and the action executes right away.
    // Do not chain eval on to other methods of the test controller
    await t.expect(Date.now() - startTime > timeout).notOk();
});

test('Reload after a timeout', async t => {
    const startTime = Date.now();

    await t.wait(timeout);
    await t.eval(() => location.reload(true));
    // Passes after a timeout
    await t.expect(Date.now() - startTime).gt(timeout);
});

Options

options.dependencies

Type: Object

Use the dependencies option to pass variables and helper functions to client functions.

The dependencies object may include the following items:

The clientFunction from the following example obtains a Selector query from a dependency:

import { ClientFunction, Selector } from 'testcafe';

const articleHeader = Selector('header>h1');

const getArticleHeaderHTML = ClientFunction(() => articleHeader().innerHTML, {
    dependencies: { articleHeader },
});

Note

Selector queries within client functions are not subject to the framework’s built-in waiting mechanisms. TestCafe executes them synchronously and does not engage any applicable timeouts.

TestCafe adds client function dependencies at runtime. TypeScript users may encounter the following compilation errror:

Error: TypeScript compilation failed.
Cannot find name 'dependencyFoo'.

Use the // @ts-ignore directive to suppress the error.

options.boundTestRun

Type: Object

Use the boundTestRun option to call a client function from a Node.js callback. To use this option, assign the current test controller to the boundTestRun option.

For details, see Trigger Client Functions from Node.js Callbacks.