diff --git a/modules/llrt_assert/src/lib.rs b/modules/llrt_assert/src/lib.rs index 5c91b96805..9e1853d4ae 100644 --- a/modules/llrt_assert/src/lib.rs +++ b/modules/llrt_assert/src/lib.rs @@ -1,13 +1,13 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -use llrt_utils::module::{export_default, ModuleInfo}; +use llrt_utils::module::ModuleInfo; use rquickjs::{ module::{Declarations, Exports, ModuleDef}, - prelude::{Func, Opt}, - Ctx, Exception, Result, Type, Value, + prelude::Opt, + Ctx, Exception, Function, Result, Type, Value, }; -fn assert(ctx: Ctx, value: Value, message: Opt) -> Result<()> { +fn ok(ctx: Ctx, value: Value, message: Opt) -> Result<()> { match value.type_of() { Type::Bool => { if value.as_bool().unwrap() { @@ -58,11 +58,12 @@ impl ModuleDef for AssertModule { } fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> { - export_default(ctx, exports, |default| { - default.set("ok", Func::from(assert))?; + let ok_function = Function::new(ctx.clone(), ok)?; + ok_function.set("ok", ok_function.clone())?; - Ok(()) - }) + exports.export("ok", ok_function.clone())?; + exports.export("default", ok_function)?; + Ok(()) } } diff --git a/tests/unit/assert.test.ts b/tests/unit/assert.test.ts index 3ee3cd15d8..a4cfd08442 100644 --- a/tests/unit/assert.test.ts +++ b/tests/unit/assert.test.ts @@ -1,4 +1,4 @@ -import * as assert from "assert"; +import assert from "assert"; describe("assert.ok", () => { it("Should be returned 'undefined' (So it's not an error)", () => { @@ -61,3 +61,13 @@ describe("assert.ok", () => { } }); }); + +describe("assert", () => { + it("Should be returned 'undefined' (So it's not an error)", () => { + expect(assert(true)).toBeUndefined(); + expect(assert(1)).toBeUndefined(); + expect(assert("non-empty string")).toBeUndefined(); + expect(assert([])).toBeUndefined(); + expect(assert({})).toBeUndefined(); + }); +}); diff --git a/types/assert.d.ts b/types/assert.d.ts index 9d6aae6ac7..78260ef24c 100644 --- a/types/assert.d.ts +++ b/types/assert.d.ts @@ -2,6 +2,11 @@ * The `assert` module provides a set of assertion functions for verifying invariants. */ declare module "assert" { + /** + * An alias of {@link ok}. + * @param value The input that is checked for being truthy. + */ + function assert(value: unknown, message?: string | Error): asserts value; /** * Tests if `value` is truthy. *