Commit 07c4d54 for mammothjs
commit 07c4d54553f7913d2afe7ded6a36e1392fa1e801
Author: Michael Williamson <mike@zwobble.org>
Date: Sat May 31 09:30:52 2025 +0100
Add tests around promises.try()
diff --git a/test/promises.tests.js b/test/promises.tests.js
new file mode 100644
index 0000000..3dd24be
--- /dev/null
+++ b/test/promises.tests.js
@@ -0,0 +1,48 @@
+var assert = require("assert");
+
+var promises = require("../lib/promises");
+var test = require("./test")(module);
+
+test("try", {
+ "when function succeeds with non-promise then promise is resolved": function() {
+ return promises.try(function() {
+ return "success";
+ }).then(function(result) {
+ assert.strictEqual(result, "success");
+ });
+ },
+
+ "when function succeeds with promise then promise is resolved": function() {
+ return promises.try(function() {
+ return promises.resolve("success");
+ }).then(function(result) {
+ assert.strictEqual(result, "success");
+ });
+ },
+
+ "when function throws error then promise is rejected": function() {
+ return promises.try(function() {
+ throw new Error("failure");
+ }).then(
+ function() {
+ assert.fail("Expected rejection");
+ },
+ function(error) {
+ assert.strictEqual(error.message, "failure");
+ }
+ );
+ },
+
+ "when function fails with promise then promise is rejected": function() {
+ return promises.try(function() {
+ return promises.reject(new Error("failure"));
+ }).then(
+ function() {
+ assert.fail("Expected rejection");
+ },
+ function(error) {
+ assert.strictEqual(error.message, "failure");
+ }
+ );
+ }
+});