// if not passing an error, catch all. assert.throws(makeBlock(thrower, TypeError), TypeError);
// when passing a type, only catch errors of the appropriate type
let threw = false; try { assert.throws(makeBlock(thrower, TypeError), Assert.AssertionError);
} catch (e) {
threw = true; assert.ok(e instanceof TypeError, "type");
} assert.equal( true,
threw, "Assert.throws with an explicit error is eating extra errors", Assert.AssertionError
);
threw = false;
function ifError(err) { if (err) { throw err;
}
} assert.throws(function () {
ifError(new Error("test error"));
}, /test error/);
// make sure that validating using constructor really works
threw = false; try { assert.throws(function () { thrownew Error({});
}, Array);
} catch (e) {
threw = true;
} assert.ok(threw, "wrong constructor validation");
// use a RegExp to validate error message assert.throws(makeBlock(thrower, TypeError), /test/);
// use a fn to validate error object assert.throws(makeBlock(thrower, TypeError), function (err) { if (err instanceof TypeError && /test/.test(err)) { returntrue;
} returnfalse;
}); // do the same with an arrow function assert.throws(makeBlock(thrower, TypeError), err => { if (err instanceof TypeError && /test/.test(err)) { returntrue;
} returnfalse;
});
// Need to JSON.stringify so that their length is > 128 characters.
let longArray0 = Array.from(Array(50), (v, i) => i);
let longArray1 = longArray0.concat([51]); try { assert.deepEqual(longArray0, longArray1);
} catch (e) {
let message = e.toString(); // Just check that they're both entirely present in the message assert.ok(message.includes(JSON.stringify(longArray0))); assert.ok(message.includes(JSON.stringify(longArray1)));
}
// Test XPCShell-test integration:
ok(true, "OK, this went well");
deepEqual(/a/g, /a/g, "deep equal should work on RegExp");
deepEqual(/a/gim, /a/gim, "deep equal should work on RegExp");
deepEqual(
{ a: 4, b: "1" },
{ b: "1", a: 4 }, "deep equal should work on regular Object"
);
deepEqual(a1, a2, "deep equal should work on Array with Object properties");
assert.less(1, 2); assert.throws(
() => assert.less(2, 2),
e => e == "AssertionError: 2 < 2"
);
assert.lessOrEqual(2, 2); assert.throws(
() => assert.lessOrEqual(2, 1),
e => e == "AssertionError: 2 <= 1"
);
assert.throws(
() => assert.greater(NaN, 0),
e => e == "AssertionError: 'NaN' is not a number or date."
);
assert.throws(
() => assert.greater(0, NaN),
e => e == "AssertionError: 'NaN' is not a number or date."
);
let now = new Date();
let firefoxReleaseDate = new Date("2004-11-09"); assert.less(firefoxReleaseDate, now); assert.throws(
() => assert.less(now, now),
e => e == `AssertionError: "${now.toJSON()}" < "${now.toJSON()}"`
);
assert.lessOrEqual(now, now); assert.greaterOrEqual(now, now); assert.throws(
() => assert.greaterOrEqual(firefoxReleaseDate, now),
e =>
e ==
`AssertionError: "${firefoxReleaseDate.toJSON()}" >= "${now.toJSON()}"`
);
// Invalid date: assert.throws(
() => assert.greater(firefoxReleaseDate, new Date("invalid")),
e => e == "AssertionError: 'Invalid Date' is not a number or date."
);
/* ---- stringMatches ---- */ assert.stringMatches("hello world", /llo\s/); assert.stringMatches("hello world", "llo\\s"); assert.throws(
() => assert.stringMatches("hello world", /foo/),
/^AssertionError: "hello world" matches "\/foo\/"/
); assert.throws(
() => assert.stringMatches(5, /foo/),
/^AssertionError: Expected a string for lhs, but "5" isn't a string./
); assert.throws(
() => assert.stringMatches("foo bar", "+"),
/^AssertionError: Expected a valid regular expression for rhs, but "\+" isn't one./
);
/* ---- stringContains ---- */ assert.stringContains("hello world", "llo"); assert.throws(
() => assert.stringContains(5, "foo"),
/^AssertionError: Expected a string for both lhs and rhs, but either "5" or "foo" is not a string./
);
});
add_task(async function test_rejects() {
let assert = newAssert();
// A helper function to test failures.
async function checkRejectsFails(err, expected) { try {
await assert.rejects(Promise.reject(err), expected);
ok(false, "should have thrown");
} catch (ex) {
deepEqual(ex, err, "Assert.rejects threw the original unexpected error");
}
}
// A "throwable" error that's not an actual Error().
let SomeErrorLikeThing = function () {};
// The actual tests...
// An explicit error object: // An instance to check against.
await assert.rejects(Promise.reject(new Error("oh no")), Error, "rejected"); // A regex to match against the message.
await assert.rejects(Promise.reject(new Error("oh no")), /oh no/, "rejected");
// Failure cases: // An instance to check against that doesn't match.
await checkRejectsFails(new Error("something else"), SomeErrorLikeThing); // A regex that doesn't match.
await checkRejectsFails(new Error("something else"), /oh no/);
// A non-rejection should also be an assertion failure: try {
await assert.rejects(Promise.resolve(), /./, "ReSoLvEd");
ok(false, "should have rejected");
} catch (ex) {
deepEqual(ex.message, "Missing expected exception ReSoLvEd");
}
});
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.