Skip to content

Commit

Permalink
add documentation about testing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jjspace committed Dec 9, 2024
1 parent 79acb71 commit 26c7ffa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
20 changes: 16 additions & 4 deletions Documentation/Contributors/TestingGuide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,22 +441,34 @@ 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:

```javascript
it("fromDegrees throws with no latitude", function () {
// expect(function() {
Cartesian3.fromDegrees(0.0);
Cartesian3.fromDegrees(0.0, undefined);
// }).toThrowDeveloperError();
});
```
Expand Down
10 changes: 6 additions & 4 deletions packages/engine/Specs/Core/Cartesian3Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 26c7ffa

Please sign in to comment.