Test.skipJsErrors Method
Main article: Skip JavaScript Errors
TestCafe tests fail when a page yields a JavaScript error.
- Use the
test.skipJsErrors
method to ignore JavaScript errors for specific tests. - If you set a fixture-wide or global
skipJsError
directive, you can disable this setting for a specific test.
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.
You can skip all JavaScript errors that occur, or only skip errors that meet specific criteria.
- Skip all errors
- Skip errors by message
- Skip errors by URL
- Skip errors by stack
- Skip errors by multiple criteria
- Use custom logic to skip errors
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.click('#button');
}).skipJsErrors();
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.click('#button');
}).skipJsErrors({
message: /.*User.*/ig
});
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.click('#button');
}).skipJsErrors({
pageUrl: /.*.*html/ // Ignores all pages with the html extension
});
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.click('#button');
}).skipJsErrors({
stack: /.*jquery.*/ig;
});
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.click('#button');
}).skipJsErrors({
message: /.*User ID.*/ig,
stack: /.*jquery.*/;
});
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:
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')
}
);
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/`
.skipJsErrors()
test('Click a button', async t => {
await t.click('#button');
}).skipJsErrors(false); // overrides the fixture-wide setting
Related methods
- Use the fixture.skipJsErrors method to ignore JavaScript errors for individual fixtures.
- Use the t.skipJsErrors action to ignore JavaScript errors at specific points in the test.
- Use the CLI flag, the configuration file property, or the Test Runner API to ignore JavaScript errors throughout your entire test suite.