Created
November 3, 2024 13:16
-
-
Save pongo/156b9ac6d28cabfeeba966efc945ed09 to your computer and use it in GitHub Desktop.
debug assert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function debugAssert(condition: any, message?: string | (() => string)): asserts condition; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isProduction = process.env.NODE_ENV === "production"; | |
const prefix = "Assertion failed"; | |
function _debugAssert(condition, message) { | |
if (isProduction) { | |
return; | |
} | |
if (condition) { | |
return; | |
} | |
const provided = typeof message === "function" ? message() : message; | |
const value = provided ? `${prefix}: ${provided}` : prefix; | |
throw new Error(value); | |
} | |
/** @type {import("./debug-assert.d.ts").debugAssert} */ | |
export const debugAssert = isProduction ? () => {} : _debugAssert; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { beforeEach, describe, it } from "node:test"; | |
import assert from "node:assert/strict"; | |
import { debugAssert } from "./debug-assert.js"; | |
describe("debugAssert", () => { | |
beforeEach(() => { | |
assert(process.env.NODE_ENV !== "production"); | |
}); | |
// it("should not throw in production mode", async () => { | |
// const originalEnv = process.env.NODE_ENV; | |
// process.env.NODE_ENV = "production"; | |
// // @ts-expect-error we must refresh import | |
// const { debugAssert } = await import("./debug-assert.js?e=prod"); | |
// assert.doesNotThrow(() => debugAssert(false)); | |
// process.env.NODE_ENV = originalEnv; | |
// }); | |
it("should not throw when condition is truthy", () => { | |
assert.doesNotThrow(() => debugAssert(typeof "str" === "string")); | |
}); | |
it("should throw an error when condition is falsy", () => { | |
assert.throws(() => debugAssert(false, "Test message"), { | |
name: "Error", | |
message: "Assertion failed: Test message", | |
}); | |
}); | |
it("should throw an error with default message if none provided", () => { | |
assert.throws(() => debugAssert(false), { | |
message: "Assertion failed", | |
}); | |
}); | |
it("should evaluate message function when provided as a function", () => { | |
const messageFunction = () => "Function message"; | |
assert.throws(() => debugAssert(false, messageFunction()), { | |
message: "Assertion failed: Function message", | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment