t.skipJsErrors Method

Main article: Skip JavaScript Errors

TestCafe tests fail when a page yields a JavaScript error. The skipJsErrors method lets you ignore JavaScript errors during the test run.

What makes this action different

TestCafe offers multiple ways to ignore JavaScript errors. The t.skipJsErrors action is unique because it lets you ignore JavaScript errors at specific points during test execution.

  • Use the t.skipJsErrors action to ignore JavaScript errors from that point in the test. Specify additional options (message, url, stack) to only ignore specific error messages.

    await t.skipJsErrors();
    
  • The setting automatically resets when the test ends. If you want to disable this behavior before the end of the test, or override an existing skipJsError setting, pass false to the t.skipJsErrors action:

    await t.skipJsErrors(false);
    

Important

Errors are signs of malfunction. Do not ignore errors that you can fix.
If a page outputs unwarranted error messages, modify your application to prevent this behavior.
Use the skipJsErrors option to silence errors that you cannot act upon.

In this article

You can skip all JavaScript errors that occur, or only skip errors that meet specific criteria.

Read the following section to learn how to disable the default skipJsErrors setting:

Read the following section to learn about other ways to skip JavaScript errors:

Skip all errors

If you do not specify additional options, TestCafe ignores all JavaScript errors.

fixture `Authentication tests`
    .page `https://devexpress.github.io/testcafe/`

test('Click a button', async t => {
    await t
        .skipJsErrors() // Ignore all JavaScript errors from this point
        .click('#button')
        .skipJsErrors(false) // Until this point
});

Specify options to filter errors by string or regular expression.

Warning

Enclose regular expressions in forward slashes to avoid strict matches for special characters.

Skip errors by message

If you specify the message option, TestCafe ignores JavaScript errors with specific messages. The message option accepts strings or regular expressions.

fixture `Authentication tests`
    .page `https://devexpress.github.io/testcafe/`

test('Click a button', async t => {
    await t
        .skipJsErrors({message: /.*User.*/ig})
        .click('#button');
});

Skip errors by URL

If you specify the pageUrl option, TestCafe ignores JavaScript errors on specific pages. The pageUrl option accepts strings or regular expressions.

fixture `Authentication tests`
    .page `https://devexpress.github.io/testcafe/`

test('Click a button', async t => {
    await t
        .skipJsErrors({pageUrl: /.*.html/}) // Only ignore errors that occur on pages with the html extension
        .click('#button');
});

Skip errors by stack

If you specify the stack option, TestCafe ignores JavaScript errors with specific call stacks. The stack option accepts strings or regular expressions.

fixture `Authentication tests`
    .page `https://devexpress.github.io/testcafe/`

test('Click a button', async t => {
    await t
        .skipJsErrors({stack: /.*jquery.*/}) 
        .click('#button');
});

Skip errors by multiple criteria

Specify several arguments to skip errors that fit multiple criteria at once — for example, errors with a specific message and a specific call stack.

Note

Specify a callback function to set complex rules — for example, to skip errors that match only one criterion of multiple.

fixture `Authentication tests`
    .page `https://devexpress.github.io/testcafe/`

test('Click a button', async t => {
    await t
        .skipJsErrors({pageUrl: 'http://example.com', stack: /.*jquery.*/})
        .click('#button');
});

Use custom logic to skip errors

Create a callback function with custom logic and pass it to the skipJsErrors method.

The callback function can access the error’s message, call stack, and URL. TestCafe executes the function client-side.

Make sure that the function returns a Boolean value — true if you want to skip the error; false otherwise.

The following example skips errors with either the specified message or the specified stack:

t.skipJsErrors(({ message, stack, pageUrl }) => {
    return message === 'Invalid User ID' || stack.includes('jquery')
})

Pass additional data to the callback function

The function accepts three arguments out of the box: message, stack, and pageUrl. To pass additional data to the function, create an object with dependency data. Then, pass an object with both the function and your dependencies to the skipJsErrors method.

skipJsErrors(
    {
        fn: ({ message, stack, pageUrl }) => {
        // This code can reference 'key1' and 'key2'
        },
        dependencies: {key1: value, key2: value2}
    }
);

Disable skipJsErrors

Pass false to the skipJsError method to disable skipJsError settings that are greater in scope.

fixture `Authentication tests`
    .page `https://devexpress.github.io/testcafe/`

test('Click a button', async t => {
    await t.skipJsErrors(false) // overrides the test-wide setting
}).skipJsErrors(true);