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.


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

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


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.


const timeout = 1000;

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

    await t
        .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);



Type: Object

The dependencies option contains functions, variables, or objects used by the client function internally. Properties of the dependencies object are added to the client function’s scope as variables.

Dependencies passed to ClientFunction must be Selectors, ClientFunctions or a serializable object.

The following code sample demonstrates a client function (getArticleHeaderHTML) that calls a selector (articleHeader) internally. TestCafe passes this selector to getArticleHeaderHTML as a dependency.

import { ClientFunction, Selector } from 'testcafe';

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

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


When a client function calls a selector internally, the selector does not wait for the element to appear in the DOM and is executed at once, like a client function.

When dependencies are passed to a client function, TypeScript cannot find them during compilation. This happens because dependencies are added to the function’s scope at runtime and can cause an error:

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

Add the // @ts-ignore TypeScript comment to suppress this error.


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 Call Client Functions from Node.js Callbacks.