Documentation

QUnit/raises

From jQuery JavaScript Library

Jump to: navigation, search

« Back to QUnit

raises( block, [expected][message] )

Assertion to test if a callback throws an exception when run.
Doesn't do any check for the type of exception or message (may be added in the future, when a good API is found). Currently just executes the callback argument, then calling ok(false) when no exception is thrown, otherwise calling ok(true), in the try-block.
Arguments:

blockFunction
Callback to execute, expecting it to throw an exception. Gets called with default scope (window) and no arguments.
expected (Optional)Function
An optional verification. Can be a regex to test the thrown exception. Can be a constructor function, tested with instanceof against the exception. Can be a callback - called with the exception as the first argument, return true for a valid exception.
message (Optional)String
A message to output with the assertion result.

Examples:
Example test for raises(). Test will pass, as the provided callback always throws an Error.

test("a test", function() {
  raises(function() {
    throw new Error("failing test");
  }, "must throw error to pass");
});

Checks if the error thrown is an instance of CustomError.

test("a test", function() {
  function CustomError() {};
  raises(function() {
    throw new CustomError();
  }, CustomError, "must throw error to pass");
});

NameType