Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow DeveloperError Jasmine matchers to accept regex for messages #12338

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions Specs/addDefaultMatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ function makeAsyncThrowFunction(debug, Type, name) {
.catch((e) => {
let result = e instanceof Type || e.name === name;
if (defined(message)) {
result = result && util.equals(e.message, message);
if (typeof message === "string") {
result = result && e.message === message;
} else {
// if the expected message is a regular expression check it against the error message
// this matches how the builtin .toRejectWithError(Error, /message/) works
// https://github.com/jasmine/jasmine/blob/main/src/core/matchers/toThrowError.js
result = result && message.test(e.message);
}
}
return {
pass: result,
Expand Down Expand Up @@ -92,7 +99,7 @@ function makeThrowFunction(debug, Type, name) {
if (debug) {
return function (util) {
return {
compare: function (actual, expected) {
compare: function (actual, message) {
// based on the built-in Jasmine toThrow matcher
let result = false;
let exception;
Expand All @@ -110,20 +117,29 @@ function makeThrowFunction(debug, Type, name) {
if (exception) {
result = exception instanceof Type || exception.name === name;
}
if (defined(message)) {
if (typeof message === "string") {
result = result && exception.message === message;
} else {
// if the expected message is a regular expression check it against the error message
// this matches how the builtin .toRejectWithError(Error, /message/) works
// https://github.com/jasmine/jasmine/blob/main/src/core/matchers/toThrowError.js
result = result && message.test(exception.message);
}
}

let message;
let testMessage;
if (result) {
message = [
`Expected function not to throw ${name} , but it threw`,
exception.message || exception,
].join(" ");
testMessage = `Expected function not to throw ${name} , but it threw ${exception.message || exception}`;
} else {
message = `Expected function to throw ${name}.`;
testMessage = defined(message)
? `Expected to throw with ${name}: ${message}, but it was thrown with ${exception}`
: `Expected function to throw with ${name}.`;
}

return {
pass: result,
message: message,
message: testMessage,
};
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe("Scene/GoogleEarthEnterpriseImageryProvider", function () {
it("fromMetadata throws without metadata", function () {
expect(() =>
GoogleEarthEnterpriseImageryProvider.fromMetadata(),
).toThrowDeveloperError("");
).toThrowDeveloperError(/metadata is required/);
});

it("fromMetadata throws if there isn't imagery", async function () {
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/Specs/Scene/ImageryLayerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe(

it("fromProviderAsync throws without provider promise", function () {
expect(() => ImageryLayer.fromProviderAsync()).toThrowDeveloperError(
"expected",
/Expected imageryProviderPromise to be typeof object/,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("Scene/MapboxStyleImageryProvider", function () {
it("requires the styleId to be specified", function () {
expect(function () {
return new MapboxStyleImageryProvider({ accessToken: "test-token" });
}).toThrowDeveloperError("styleId is required");
}).toThrowDeveloperError("options.styleId is required.");
});

it("returns valid value for hasAlphaChannel", function () {
Expand Down