Skip JavaScript Errors

TestCafe tests fail when a page yields a JavaScript error. If you enable the skipJsErrors option, TestCafe deliberately ignores JavaScript errors and lets tests proceed.

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.

Overview

Methods

  • Use the t.skipJsErrors action to ignore JavaScript errors at specific points in the test.

Options

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

Conflict resolution

Skip all errors

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

Examples

  • CLI
    testcafe chrome test.js -e
    
  • Configuration file
    {
        "skipJsErrors": true
    }
    
  • Test Runner API
        const runner = await runner.run({skipJsErrors: true});
    
  • Fixture method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
        .skipJsErrors();
    
  • Test method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
    
    test('Click a button', async t => {
        await t.click('#button');
    }).skipJsErrors();
    
  • Test action
    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.

Examples

  • CLI
    testcafe chrome test.js -e message='/.*User ID.*/ig'
    
  • Configuration file
    {
        "skipJsErrors": {
            "message": /.*User ID.*/ig
        }
    }
    
  • Test Runner API
    const runner = await runner.run({
            skipJsErrors: { message: /.*User ID.*/ }
    });
    
  • Fixture method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
        .skipJsErrors({
            message: /.*User.*/ig
        });
    
  • Test method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
    
    test('Click a button', async t => {
        await t.click('#button');
    }).skipJsErrors({
            message: /.*User.*/ig
        });
    
  • Test action
    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.

Examples

  • CLI
    testcafe chrome test.js -e pageUrl='/.*.*html/'
    
  • Configuration file
    {
        "skipJsErrors": {
            "pageUrl": /.*.*html/
        }
    }
    
  • Test Runner API
    const runner = await runner.run({
        skipJsErrors: {pageUrl: /.*.*html/}
    });
    
  • Fixture method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
        .skipJsErrors({
            pageUrl: /.*.*html/ // Ignores all pages with the html extension
        });
    
  • Test method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
    
    test('Click a button', async t => {
        await t.click('#button');
    }).skipJsErrors({
            pageUrl: /.*.*html/ // Ignores all pages with the html extension
        });
    
  • Test action
    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.

Examples

  • CLI
    testcafe chrome test.js -e stack='/.*jquery.*/g'
    
  • Configuration file
    {
        "skipJsErrors": {
            "stack": /.*jquery.*/ig
        }
    }
    
  • Test Runner API
    const runner = await runner.run({
        skipJsErrors: { stack: /.*jquery.*/ }
    });
    
  • Fixture method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
        .skipJsErrors({
            stack: /.*jquery.*/ig
        });
    
  • Test method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
    
    test('Click a button', async t => {
        await t.click('#button');
    }).skipJsErrors({
            stack: /.*jquery.*/ig;
        });
    
  • Test action
    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.

Examples

  • CLI
    testcafe chrome test.js -e stack='/.*jquery.*/', message='/.*User ID.*/ig'
    
  • Configuration file
    {
        "skipJsErrors": {
            "stack": "/.*jquery.*/",
            "message": "/.*User ID.*/ig"
        }
    }
    
  • Test Runner API
    const runner = await runner.run({
        skipJsErrors: {
        stack: /.*jquery.*/,
        message: /.*User ID.*/
        }
    });
    
  • Fixture method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
        .skipJsErrors({
            stack: /.*jquery.*/,
            message: /.*User ID.*/ig
        });
    
  • Test method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
    
    test('Click a button', async t => {
        await t.click('#button');
    }).skipJsErrors({
            message: /.*User ID.*/ig,
            stack: /.*jquery.*/;
        });
    
  • Test action
    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:

Examples

  • CLI The command line interface does not support this capability.
  • Configuration file

    Use a JavaScript configuration file to define a callback function with custom logic:

    const callbackFunction = {
        fn:           ({ message }) => message.includes('User') || stack.includes('jquery');
    };
    
    module.exports = {
        skipJsErrors: callbackFunction,
    };
    
  • Test Runner API

    runner.run({
    
        skipJsErrors: ({ message, stack, pageUrl }) => message.includes('User') || stack.includes('jquery');
    
    });
    
  • Fixture method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
        .skipJsErrors(
            ({ message, stack, pageUrl }) => {
                return message === 'Invalid User ID' || stack.includes('jquery')
            }
        );
    
  • Test method
    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
    
    test('Click a button', async t => {
        await t.click('#button');
    }).skipJsErrors(
            ({ message, stack, pageUrl }) => {
                return message ==='Invalid User ID' || stack.includes('jquery')
            }
        );
    
  • Test action
    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 fixture.skipJsErrors method, the test.skipJsErrors method, or the t.skipJsErrors action to disable a skipJsError setting that is greater in scope:

Examples

  • Fixture method

    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
        .skipJsErrors(false); // overrides any global setting
    
  • Test method

    fixture `Authentication tests`
        .page `https://devexpress.github.io/testcafe/`
        .skipJsErrors()
    
    test('Click a button', async t => {
            await t.click('#button');
        }).skipJsErrors(false); // overrides the fixture-wide setting
    
  • Test action

    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);