Skip to content

Commit

Permalink
chore: add assertIsPrice() test (denoland#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua authored Sep 12, 2023
1 parent c8b6fb4 commit b4ba280
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions utils/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ export const stripe = new Stripe(STRIPE_SECRET_KEY!, {
httpClient: Stripe.createFetchHttpClient(),
});

/**
* Asserts that the value is strictly a {@linkcode Stripe.Price} object.
*
* @example
* ```ts
* import { assertIsPrice } from "@/utils/stripe.ts";
*
* assertIsPrice(undefined); // Throws AssertionError
* assertIsPrice(null); // Throws AssertionError
* assertIsPrice("not a price"); // Throws AssertionError
* ```
*/
export function assertIsPrice(value: unknown): asserts value is Stripe.Price {
if (value === undefined || value === null || typeof value === "string") {
throw new AssertionError(
Expand Down
11 changes: 11 additions & 0 deletions utils/stripe_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import { AssertionError, assertThrows } from "std/assert/mod.ts";
import { assertIsPrice } from "./stripe.ts";

Deno.test("[stripe] assertIsPrice()", () => {
const message =
"Default price must be of type `Stripe.Price`. Please run the `deno task init:stripe` as the README instructs.";
assertThrows(() => assertIsPrice(undefined), AssertionError, message);
assertThrows(() => assertIsPrice(null), AssertionError, message);
assertThrows(() => assertIsPrice("not a price"), AssertionError, message);
});

0 comments on commit b4ba280

Please sign in to comment.