From 26c7ffa749895d1098a2dfef42c6a60f156396ec Mon Sep 17 00:00:00 2001 From: jjspace <8007967+jjspace@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:08:55 -0500 Subject: [PATCH] add documentation about testing errors --- .../Contributors/TestingGuide/README.md | 20 +++++++++++++++---- packages/engine/Specs/Core/Cartesian3Spec.js | 10 ++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Documentation/Contributors/TestingGuide/README.md b/Documentation/Contributors/TestingGuide/README.md index 915c8bf38d38..ffde3a98870a 100644 --- a/Documentation/Contributors/TestingGuide/README.md +++ b/Documentation/Contributors/TestingGuide/README.md @@ -441,14 +441,26 @@ In addition to testing success cases, we also test all failure cases. The custom ```javascript it("fromDegrees throws with no latitude", function () { expect(function () { - Cartesian3.fromDegrees(0.0); - }).toThrowDeveloperError(); + Cartesian3.fromDegrees(0.0, undefined); + }).toThrowDeveloperError( + "Expected latitude to be typeof number, actual typeof was undefined", + ); }); ``` Above, `Cartesian3.fromDegrees` is expected to throw a `DeveloperError` because it expects longitude and latitude arguments, and only longitude is provided. -Tips: +#### Tips + +- When testing for exceptions it is recommended to test for the expected error message to verify that the test is triggering the correct error. This can be achieved either with the full error message, like above, or with a regular expression that will match the error message like this: + +```javascript +it("fromDegrees throws with no latitude", function () { + expect(function () { + Cartesian3.fromDegrees(0.0, undefined); + }).toThrowDeveloperError(/Expected latitude to be/); +}); +``` - When testing for exceptions, put only code that is expected to trigger the exception inside the function passed to `expect()`, in case setup code unintentionally throws an exception. - To verify the right exception is thrown, it is often useful to comment out the `expect` call when first running the test, for example: @@ -456,7 +468,7 @@ Tips: ```javascript it("fromDegrees throws with no latitude", function () { // expect(function() { - Cartesian3.fromDegrees(0.0); + Cartesian3.fromDegrees(0.0, undefined); // }).toThrowDeveloperError(); }); ``` diff --git a/packages/engine/Specs/Core/Cartesian3Spec.js b/packages/engine/Specs/Core/Cartesian3Spec.js index d2cae1feef9d..a0441473c06d 100644 --- a/packages/engine/Specs/Core/Cartesian3Spec.js +++ b/packages/engine/Specs/Core/Cartesian3Spec.js @@ -1241,14 +1241,16 @@ describe("Core/Cartesian3", function () { it("fromDegrees throws with no longitude", function () { expect(function () { - Cartesian3.fromDegrees(); - }).toThrowDeveloperError(); + Cartesian3.fromDegrees(undefined, undefined); + }).toThrowDeveloperError(/Expected longitude to be/); }); it("fromDegrees throws with no latitude", function () { expect(function () { - Cartesian3.fromDegrees(1); - }).toThrowDeveloperError(); + Cartesian3.fromDegrees(1, undefined); + }).toThrowDeveloperError( + "Expected latitude to be typeof number, actual typeof was undefined", + ); }); it("fromDegrees works works with default ellipsoid", function () {