From f2757067f663877fe21240d6bf5799a4580602a4 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Tue, 15 Apr 2025 15:37:23 +0200 Subject: [PATCH] Export contract deployment custom error assert into a function --- test/Marketplace.test.js | 20 ++++++-------------- test/Periods.test.js | 13 +++++-------- test/util.js | 13 +++++++++++++ 3 files changed, 24 insertions(+), 22 deletions(-) create mode 100644 test/util.js diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index 00b0ece..6dd11a8 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -41,6 +41,7 @@ const { } = require("./evm") const { getBytes } = require("ethers") const MarketplaceModule = require("../ignition/modules/marketplace") +const { assertDeploymentRejectedWithCustomError } = require("./util") const ACCOUNT_STARTING_BALANCE = 1_000_000_000_000_000n @@ -67,13 +68,7 @@ describe("Marketplace constructor", function () { }, }) - const error = await expect(promise).to.be.rejected - expect(error) - .to.have.property("message") - .that.contains( - expectedError, - `Expected error ${expectedError}, but got ${error.message}` - ) + assertDeploymentRejectedWithCustomError(expectedError, promise) }) } @@ -101,13 +96,10 @@ describe("Marketplace constructor", function () { }, }) - const error = await expect(promise).to.be.rejected - expect(error) - .to.have.property("message") - .that.contains( - expectedError, - `Expected error ${expectedError}, but got ${error.message}` - ) + assertDeploymentRejectedWithCustomError( + "Marketplace_MaximumSlashingTooHigh", + promise, + ) }) }) diff --git a/test/Periods.test.js b/test/Periods.test.js index 49ac84d..1295140 100644 --- a/test/Periods.test.js +++ b/test/Periods.test.js @@ -1,5 +1,6 @@ const { expect } = require("chai") const PeriodsModule = require("../ignition/modules/periods") +const { assertDeploymentRejectedWithCustomError } = require("./util") describe("Periods", function () { it("should revert when secondsPerPeriod is 0", async function () { @@ -10,15 +11,11 @@ describe("Periods", function () { }, }, }) - const expectedError = "Periods_InvalidSecondsPerPeriod" - const error = await expect(promise).to.be.rejected - expect(error) - .to.have.property("message") - .that.contains( - expectedError, - `Expected error ${expectedError}, but got ${error.message}`, - ) + assertDeploymentRejectedWithCustomError( + "Periods_InvalidSecondsPerPeriod", + promise, + ) }) it("should not revert when secondsPerPeriod more than 0", async function () { diff --git a/test/util.js b/test/util.js new file mode 100644 index 0000000..46bc91d --- /dev/null +++ b/test/util.js @@ -0,0 +1,13 @@ +export async function assertDeploymentRejectedWithCustomError( + customError, + deploymentPromise, +) { + const error = await expect(deploymentPromise).to.be.rejected + + expect(error) + .to.have.property("message") + .that.contains( + customError, + `Expected error ${expectedError}, but got ${error.message}`, + ) +}