diff --git a/packages/tests/src/constants.ts b/packages/tests/src/constants.ts index 31b31fc05..ab2c4b8ee 100644 --- a/packages/tests/src/constants.ts +++ b/packages/tests/src/constants.ts @@ -61,3 +61,5 @@ export const TEST_TIMESTAMPS = [ 1949153314000, undefined ]; + +export const MOCHA_HOOK_MAX_TIMEOUT = 50_000; diff --git a/packages/tests/src/utils/custom_mocha_hooks.ts b/packages/tests/src/utils/custom_mocha_hooks.ts new file mode 100644 index 000000000..b2c4a4372 --- /dev/null +++ b/packages/tests/src/utils/custom_mocha_hooks.ts @@ -0,0 +1,68 @@ +import { Logger } from "@waku/utils"; +import { Suite } from "mocha"; + +import { MOCHA_HOOK_MAX_TIMEOUT } from "../constants"; +const log = new Logger("test:mocha-hook"); + +function withGracefulTimeout( + asyncOperation: () => Promise, + doneCallback: (error?: unknown) => void, + timeoutDuration: number = MOCHA_HOOK_MAX_TIMEOUT +): void { + let operationCompleted = false; + + const wrapperOperation: () => Promise = async () => { + try { + await asyncOperation(); + if (!operationCompleted) { + operationCompleted = true; + log.info("Mocha hook completed successfully."); + doneCallback(); + } + } catch (error) { + if (!operationCompleted) { + operationCompleted = true; + log.error( + "Mocha hook failed. Proceeding to the test so it can retry.", + error + ); + doneCallback(); + } + } + }; + + void wrapperOperation(); + setTimeout(() => { + if (!operationCompleted) { + log.info( + "Custom timeout reached. Proceeding to the test so it can retry." + ); + operationCompleted = true; + doneCallback(); + } + }, timeoutDuration); +} + +export const beforeEachCustom = function ( + suite: Suite, + cb: () => Promise +): void { + const timeoutBefore = suite.timeout(); + suite.timeout(MOCHA_HOOK_MAX_TIMEOUT); + suite.beforeEach((done) => { + withGracefulTimeout(cb, done); + }); + suite.timeout(timeoutBefore); // restore timeout to the original value +}; + +export const afterEachCustom = function ( + suite: Suite, + cb: () => Promise +): void { + const timeoutBefore = suite.timeout(); + suite.timeout(MOCHA_HOOK_MAX_TIMEOUT); + suite.afterEach((done) => { + withGracefulTimeout(cb, done); + }); + suite.timeout(timeoutBefore); // restore timeout to the original value +}; diff --git a/packages/tests/src/utils/index.ts b/packages/tests/src/utils/index.ts index 30dee17f9..1a2740247 100644 --- a/packages/tests/src/utils/index.ts +++ b/packages/tests/src/utils/index.ts @@ -5,3 +5,4 @@ export * from "./wait_for_remote_peer_with_codec.js"; export * from "./delay.js"; export * from "./base64_utf8.js"; export * from "./waitForConnections.js"; +export * from "./custom_mocha_hooks.js"; diff --git a/packages/tests/tests/connection-mananger/connection_state.spec.ts b/packages/tests/tests/connection-mananger/connection_state.spec.ts index aa0eb55d9..0fd8394ab 100644 --- a/packages/tests/tests/connection-mananger/connection_state.spec.ts +++ b/packages/tests/tests/connection-mananger/connection_state.spec.ts @@ -4,7 +4,12 @@ import { createLightNode } from "@waku/sdk"; import { createRelayNode } from "@waku/sdk/relay"; import { expect } from "chai"; -import { delay, NOISE_KEY_1 } from "../../src/index.js"; +import { + afterEachCustom, + beforeEachCustom, + delay, + NOISE_KEY_1 +} from "../../src/index.js"; import { makeLogFileName, ServiceNode, @@ -22,8 +27,7 @@ describe("Connection state", function () { let nwaku1PeerId: Multiaddr; let nwaku2PeerId: Multiaddr; - beforeEach(async () => { - this.timeout(TEST_TIMEOUT); + beforeEachCustom(this, async () => { waku = await createLightNode({ shardInfo: { shards: [0] } }); nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1"); nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2"); @@ -33,8 +37,7 @@ describe("Connection state", function () { nwaku2PeerId = await nwaku2.getMultiaddrWithId(); }); - afterEach(async () => { - this.timeout(TEST_TIMEOUT); + afterEachCustom(this, async () => { await tearDownNodes([nwaku1, nwaku2], waku); }); diff --git a/packages/tests/tests/connection-mananger/dials.spec.ts b/packages/tests/tests/connection-mananger/dials.spec.ts index 4d0b8f8f1..a157c7a3c 100644 --- a/packages/tests/tests/connection-mananger/dials.spec.ts +++ b/packages/tests/tests/connection-mananger/dials.spec.ts @@ -6,7 +6,7 @@ import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; import sinon, { SinonSpy, SinonStub } from "sinon"; -import { delay } from "../../src/index.js"; +import { afterEachCustom, beforeEachCustom, delay } from "../../src/index.js"; import { tearDownNodes } from "../../src/index.js"; const DELAY_MS = 1_000; @@ -20,8 +20,7 @@ describe("Dials", function () { let isPeerTopicConfigured: SinonStub; let waku: LightNode; - this.beforeEach(async function () { - this.timeout(TEST_TIMEOUT); + beforeEachCustom(this, async () => { waku = await createLightNode({ shardInfo: { shards: [0] } }); isPeerTopicConfigured = sinon.stub( waku.connectionManager as any, @@ -30,8 +29,7 @@ describe("Dials", function () { isPeerTopicConfigured.resolves(true); }); - afterEach(async () => { - this.timeout(TEST_TIMEOUT); + afterEachCustom(this, async () => { await tearDownNodes([], waku); isPeerTopicConfigured.restore(); sinon.restore(); @@ -40,11 +38,11 @@ describe("Dials", function () { describe("attemptDial method", function () { let attemptDialSpy: SinonSpy; - beforeEach(function () { + beforeEachCustom(this, async () => { attemptDialSpy = sinon.spy(waku.connectionManager as any, "attemptDial"); }); - afterEach(function () { + afterEachCustom(this, async () => { attemptDialSpy.restore(); }); @@ -73,7 +71,7 @@ describe("Dials", function () { describe("dialPeer method", function () { let peerStoreHasStub: SinonStub; let dialAttemptsForPeerHasStub: SinonStub; - beforeEach(function () { + beforeEachCustom(this, async () => { getConnectionsStub = sinon.stub( (waku.connectionManager as any).libp2p, "getConnections" @@ -102,7 +100,7 @@ describe("Dials", function () { dialAttemptsForPeerHasStub.returns(false); }); - afterEach(function () { + afterEachCustom(this, async () => { dialPeerStub.restore(); getTagNamesForPeerStub.restore(); getConnectionsStub.restore(); diff --git a/packages/tests/tests/connection-mananger/events.spec.ts b/packages/tests/tests/connection-mananger/events.spec.ts index dec186cad..637c355f9 100644 --- a/packages/tests/tests/connection-mananger/events.spec.ts +++ b/packages/tests/tests/connection-mananger/events.spec.ts @@ -10,7 +10,7 @@ import { import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; -import { delay } from "../../src/index.js"; +import { afterEachCustom, beforeEachCustom, delay } from "../../src/index.js"; import { tearDownNodes } from "../../src/index.js"; const TEST_TIMEOUT = 20_000; @@ -18,13 +18,11 @@ const TEST_TIMEOUT = 20_000; describe("Events", function () { let waku: LightNode; this.timeout(TEST_TIMEOUT); - beforeEach(async function () { - this.timeout(TEST_TIMEOUT); + beforeEachCustom(this, async () => { waku = await createLightNode({ shardInfo: { shards: [0] } }); }); - afterEach(async () => { - this.timeout(TEST_TIMEOUT); + afterEachCustom(this, async () => { await tearDownNodes([], waku); }); diff --git a/packages/tests/tests/connection-mananger/methods.spec.ts b/packages/tests/tests/connection-mananger/methods.spec.ts index a3532d9f1..a43c1f2b8 100644 --- a/packages/tests/tests/connection-mananger/methods.spec.ts +++ b/packages/tests/tests/connection-mananger/methods.spec.ts @@ -10,7 +10,7 @@ import { import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; -import { delay } from "../../src/index.js"; +import { afterEachCustom, beforeEachCustom, delay } from "../../src/index.js"; import { tearDownNodes } from "../../src/index.js"; const TEST_TIMEOUT = 20_000; @@ -18,13 +18,11 @@ const TEST_TIMEOUT = 20_000; describe("Public methods", function () { let waku: LightNode; this.timeout(TEST_TIMEOUT); - beforeEach(async function () { - this.timeout(TEST_TIMEOUT); + beforeEachCustom(this, async () => { waku = await createLightNode({ shardInfo: { shards: [0] } }); }); - afterEach(async () => { - this.timeout(TEST_TIMEOUT); + afterEachCustom(this, async () => { await tearDownNodes([], waku); }); it("addEventListener with correct event", async function () { diff --git a/packages/tests/tests/create.optional.spec.ts b/packages/tests/tests/create.optional.spec.ts index d098487a5..9e3b7c45c 100644 --- a/packages/tests/tests/create.optional.spec.ts +++ b/packages/tests/tests/create.optional.spec.ts @@ -2,17 +2,21 @@ import { createLightNode, LightNode } from "@waku/sdk"; import { expect } from "chai"; import sinon, { SinonSpy } from "sinon"; -import { tearDownNodes } from "../src/index.js"; +import { + afterEachCustom, + beforeEachCustom, + tearDownNodes +} from "../src/index.js"; -describe("Create node", () => { +describe("Create node", function () { let waku: LightNode; let consoleInfoSpy: SinonSpy; - beforeEach(() => { + beforeEachCustom(this, async () => { consoleInfoSpy = sinon.spy(console as any, "info"); }); - afterEach(async () => { + afterEachCustom(this, async () => { consoleInfoSpy.restore(); sinon.restore(); await tearDownNodes([], waku); diff --git a/packages/tests/tests/enr.node.spec.ts b/packages/tests/tests/enr.node.spec.ts index cdbdbfb13..5db2c41ca 100644 --- a/packages/tests/tests/enr.node.spec.ts +++ b/packages/tests/tests/enr.node.spec.ts @@ -6,6 +6,7 @@ import { createRelayNode } from "@waku/sdk/relay"; import { expect } from "chai"; import { + afterEachCustom, makeLogFileName, NOISE_KEY_1, ServiceNode, @@ -16,8 +17,7 @@ describe("ENR Interop: ServiceNode", function () { let waku: RelayNode; let nwaku: ServiceNode; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/ephemeral.node.spec.ts b/packages/tests/tests/ephemeral.node.spec.ts index 9e2ca1c72..fe0d5b075 100644 --- a/packages/tests/tests/ephemeral.node.spec.ts +++ b/packages/tests/tests/ephemeral.node.spec.ts @@ -25,6 +25,8 @@ import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, delay, makeLogFileName, NOISE_KEY_1, @@ -41,20 +43,18 @@ const TestEncoder = createEncoder({ }); const TestDecoder = createDecoder(TestContentTopic); -describe("Waku Message Ephemeral field", () => { +describe("Waku Message Ephemeral field", function () { let waku: LightNode; let nwaku: ServiceNode; let subscription: IFilterSubscription; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); - beforeEach(async function () { - this.timeout(15_000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ filter: true, lightpush: true, diff --git a/packages/tests/tests/filter/ping.node.spec.ts b/packages/tests/tests/filter/ping.node.spec.ts index 5527d79cd..4604bd5d2 100644 --- a/packages/tests/tests/filter/ping.node.spec.ts +++ b/packages/tests/tests/filter/ping.node.spec.ts @@ -6,7 +6,11 @@ import { import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; -import { ServiceNodesFleet } from "../../src/index.js"; +import { + afterEachCustom, + beforeEachCustom, + ServiceNodesFleet +} from "../../src/index.js"; import { runMultipleNodes, @@ -25,14 +29,14 @@ const runTests = (strictCheckNodes: boolean): void => { let serviceNodes: ServiceNodesFleet; let subscription: IFilterSubscription; - this.beforeEach(async function () { - this.timeout(15000); - [serviceNodes, waku] = await runMultipleNodes(this, [DefaultPubsubTopic]); + beforeEachCustom(this, async () => { + [serviceNodes, waku] = await runMultipleNodes(this.ctx, [ + DefaultPubsubTopic + ]); subscription = await waku.filter.createSubscription(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await teardownNodesWithRedundancy(serviceNodes, waku); }); diff --git a/packages/tests/tests/filter/push.node.spec.ts b/packages/tests/tests/filter/push.node.spec.ts index 3907de3cb..bc1d049ff 100644 --- a/packages/tests/tests/filter/push.node.spec.ts +++ b/packages/tests/tests/filter/push.node.spec.ts @@ -9,6 +9,8 @@ import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, delay, ServiceNodesFleet, TEST_STRING, @@ -32,14 +34,14 @@ const runTests = (strictCheckNodes: boolean): void => { let serviceNodes: ServiceNodesFleet; let subscription: IFilterSubscription; - this.beforeEach(async function () { - this.timeout(15000); - [serviceNodes, waku] = await runMultipleNodes(this, [DefaultPubsubTopic]); + beforeEachCustom(this, async () => { + [serviceNodes, waku] = await runMultipleNodes(this.ctx, [ + DefaultPubsubTopic + ]); subscription = await waku.filter.createSubscription(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await teardownNodesWithRedundancy(serviceNodes, waku); }); diff --git a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts index 119eac1c0..7fd42b1a7 100644 --- a/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/filter/single_node/multiple_pubsub.node.spec.ts @@ -17,6 +17,8 @@ import { utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, MessageCollector, ServiceNode, @@ -58,10 +60,9 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () { }); const customDecoder2 = createDecoder(customContentTopic2, singleShardInfo2); - this.beforeEach(async function () { - this.timeout(15000); + beforeEachCustom(this, async () => { [nwaku, waku] = await runNodes( - this, + this.ctx, [customPubsubTopic1, customPubsubTopic2], shardInfo ); @@ -71,8 +72,7 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () { messageCollector = new MessageCollector(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); @@ -229,10 +229,9 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () { shard: contentTopicToShardIndex(customContentTopic2) }); - this.beforeEach(async function () { - this.timeout(15000); + beforeEachCustom(this, async () => { [nwaku, waku] = await runNodes( - this, + this.ctx, [autoshardingPubsubTopic1, autoshardingPubsubTopic2], contentTopicInfo ); @@ -242,8 +241,7 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () { messageCollector = new MessageCollector(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); @@ -396,10 +394,9 @@ describe("Waku Filter V2 (Named sharding): Multiple PubsubTopics", function () { }); const customDecoder2 = createDecoder(customContentTopic2, customPubsubTopic2); - this.beforeEach(async function () { - this.timeout(15000); + beforeEachCustom(this, async () => { [nwaku, waku] = await runNodes( - this, + this.ctx, [customPubsubTopic1, customPubsubTopic2], { clusterId: 3, @@ -410,8 +407,7 @@ describe("Waku Filter V2 (Named sharding): Multiple PubsubTopics", function () { messageCollector = new MessageCollector(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); diff --git a/packages/tests/tests/filter/single_node/ping.node.spec.ts b/packages/tests/tests/filter/single_node/ping.node.spec.ts index 8528c48dc..14f9bf0b4 100644 --- a/packages/tests/tests/filter/single_node/ping.node.spec.ts +++ b/packages/tests/tests/filter/single_node/ping.node.spec.ts @@ -7,6 +7,8 @@ import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, MessageCollector, ServiceNode, tearDownNodes @@ -28,15 +30,13 @@ describe("Waku Filter V2: Ping", function () { let subscription: IFilterSubscription; let messageCollector: MessageCollector; - this.beforeEach(async function () { - this.timeout(15000); - [nwaku, waku] = await runNodes(this, [DefaultPubsubTopic]); + beforeEachCustom(this, async () => { + [nwaku, waku] = await runNodes(this.ctx, [DefaultPubsubTopic]); subscription = await waku.filter.createSubscription(); messageCollector = new MessageCollector(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/filter/single_node/push.node.spec.ts b/packages/tests/tests/filter/single_node/push.node.spec.ts index afe76d8e9..4b900c66f 100644 --- a/packages/tests/tests/filter/single_node/push.node.spec.ts +++ b/packages/tests/tests/filter/single_node/push.node.spec.ts @@ -9,6 +9,8 @@ import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, delay, MessageCollector, ServiceNode, @@ -32,15 +34,13 @@ describe("Waku Filter V2: FilterPush", function () { let subscription: IFilterSubscription; let messageCollector: MessageCollector; - this.beforeEach(async function () { - this.timeout(15000); - [nwaku, waku] = await runNodes(this, [DefaultPubsubTopic]); + beforeEachCustom(this, async () => { + [nwaku, waku] = await runNodes(this.ctx, [DefaultPubsubTopic]); subscription = await waku.filter.createSubscription(); messageCollector = new MessageCollector(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts index 973f957a8..fa53c1960 100644 --- a/packages/tests/tests/filter/single_node/subscribe.node.spec.ts +++ b/packages/tests/tests/filter/single_node/subscribe.node.spec.ts @@ -16,6 +16,8 @@ import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, delay, generateTestData, isNwakuAtLeast, @@ -44,18 +46,14 @@ describe("Waku Filter V2: Subscribe: Single Service Node", function () { let subscription: IFilterSubscription; let messageCollector: MessageCollector; - this.beforeEach(async function () { - this.timeout(15000); - [nwaku, waku] = await runNodes(this, [DefaultPubsubTopic]); + beforeEachCustom(this, async () => { + [nwaku, waku] = await runNodes(this.ctx, [DefaultPubsubTopic]); subscription = await waku.filter.createSubscription(); messageCollector = new MessageCollector(); - - // Nwaku subscribe to the default pubsub topic await nwaku.ensureSubscriptions(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); diff --git a/packages/tests/tests/filter/single_node/unsubscribe.node.spec.ts b/packages/tests/tests/filter/single_node/unsubscribe.node.spec.ts index 2496f2ecf..c0c95c625 100644 --- a/packages/tests/tests/filter/single_node/unsubscribe.node.spec.ts +++ b/packages/tests/tests/filter/single_node/unsubscribe.node.spec.ts @@ -5,6 +5,8 @@ import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, generateTestData, MessageCollector, ServiceNode, @@ -27,9 +29,8 @@ describe("Waku Filter V2: Unsubscribe", function () { let subscription: IFilterSubscription; let messageCollector: MessageCollector; - this.beforeEach(async function () { - this.timeout(15000); - [nwaku, waku] = await runNodes(this, [DefaultPubsubTopic]); + beforeEachCustom(this, async () => { + [nwaku, waku] = await runNodes(this.ctx, [DefaultPubsubTopic]); subscription = await waku.filter.createSubscription(); messageCollector = new MessageCollector(); @@ -37,8 +38,7 @@ describe("Waku Filter V2: Unsubscribe", function () { await nwaku.ensureSubscriptions(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/filter/subscribe.node.spec.ts b/packages/tests/tests/filter/subscribe.node.spec.ts index 196bb17ee..422d11e09 100644 --- a/packages/tests/tests/filter/subscribe.node.spec.ts +++ b/packages/tests/tests/filter/subscribe.node.spec.ts @@ -15,6 +15,8 @@ import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, delay, generateTestData, isNwakuAtLeast, @@ -39,18 +41,16 @@ const runTests = (strictCheckNodes: boolean): void => { let serviceNodes: ServiceNodesFleet; let subscription: IFilterSubscription; - this.beforeEach(async function () { - this.timeout(15000); + beforeEachCustom(this, async () => { [serviceNodes, waku] = await runMultipleNodes( - this, + this.ctx, [DefaultPubsubTopic], strictCheckNodes ); subscription = await waku.filter.createSubscription(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await teardownNodesWithRedundancy(serviceNodes, waku); }); diff --git a/packages/tests/tests/filter/unsubscribe.node.spec.ts b/packages/tests/tests/filter/unsubscribe.node.spec.ts index 55b96e977..dffacf3a1 100644 --- a/packages/tests/tests/filter/unsubscribe.node.spec.ts +++ b/packages/tests/tests/filter/unsubscribe.node.spec.ts @@ -7,7 +7,12 @@ import { import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; -import { generateTestData, ServiceNodesFleet } from "../../src/index.js"; +import { + afterEachCustom, + beforeEachCustom, + generateTestData, + ServiceNodesFleet +} from "../../src/index.js"; import { messagePayload, @@ -27,14 +32,14 @@ const runTests = (strictCheckNodes: boolean): void => { let serviceNodes: ServiceNodesFleet; let subscription: IFilterSubscription; - this.beforeEach(async function () { - this.timeout(15000); - [serviceNodes, waku] = await runMultipleNodes(this, [DefaultPubsubTopic]); + beforeEachCustom(this, async () => { + [serviceNodes, waku] = await runMultipleNodes(this.ctx, [ + DefaultPubsubTopic + ]); subscription = await waku.filter.createSubscription(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await teardownNodesWithRedundancy(serviceNodes, waku); }); diff --git a/packages/tests/tests/getPeers.spec.ts b/packages/tests/tests/getPeers.spec.ts index 4b58ab6f9..fa8705d86 100644 --- a/packages/tests/tests/getPeers.spec.ts +++ b/packages/tests/tests/getPeers.spec.ts @@ -17,7 +17,13 @@ import { expect } from "chai"; import fc from "fast-check"; import Sinon from "sinon"; -import { makeLogFileName, ServiceNode, tearDownNodes } from "../src/index.js"; +import { + afterEachCustom, + beforeEachCustom, + makeLogFileName, + ServiceNode, + tearDownNodes +} from "../src/index.js"; describe("getConnectedPeersForProtocolAndShard", function () { let waku: LightNode; @@ -25,14 +31,12 @@ describe("getConnectedPeersForProtocolAndShard", function () { let serviceNode2: ServiceNode; const contentTopic = "/test/2/waku-light-push/utf8"; - this.beforeEach(async function () { - this.timeout(15000); - serviceNode1 = new ServiceNode(makeLogFileName(this) + "1"); - serviceNode2 = new ServiceNode(makeLogFileName(this) + "2"); + beforeEachCustom(this, async () => { + serviceNode1 = new ServiceNode(makeLogFileName(this.ctx) + "1"); + serviceNode2 = new ServiceNode(makeLogFileName(this.ctx) + "2"); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([serviceNode1, serviceNode2], waku); }); @@ -420,8 +424,7 @@ describe("getPeers", function () { let nonBootstrapPeers: Peer[]; let allPeers: Peer[]; - beforeEach(async function () { - this.timeout(10_000); + beforeEachCustom(this, async () => { waku = await createLightNode(); peerStore = waku.libp2p.peerStore; connectionManager = waku.libp2p.components.connectionManager; @@ -537,7 +540,7 @@ describe("getPeers", function () { }); }); - this.afterEach(function () { + afterEachCustom(this, async () => { Sinon.restore(); }); diff --git a/packages/tests/tests/light-push/index.node.spec.ts b/packages/tests/tests/light-push/index.node.spec.ts index 770a37636..2934c263b 100644 --- a/packages/tests/tests/light-push/index.node.spec.ts +++ b/packages/tests/tests/light-push/index.node.spec.ts @@ -9,6 +9,8 @@ import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, generateRandomUint8Array, ServiceNodesFleet, TEST_STRING @@ -33,10 +35,9 @@ const runTests = (strictNodeCheck: boolean): void => { let waku: LightNode; let serviceNodes: ServiceNodesFleet; - this.beforeEach(async function () { - this.timeout(15000); + beforeEachCustom(this, async () => { [serviceNodes, waku] = await runMultipleNodes( - this, + this.ctx, [DefaultPubsubTopic], strictNodeCheck, undefined, @@ -45,8 +46,7 @@ const runTests = (strictNodeCheck: boolean): void => { ); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await teardownNodesWithRedundancy(serviceNodes, waku); }); diff --git a/packages/tests/tests/light-push/single_node/index.node.spec.ts b/packages/tests/tests/light-push/single_node/index.node.spec.ts index ba317fb1d..e794aa4a5 100644 --- a/packages/tests/tests/light-push/single_node/index.node.spec.ts +++ b/packages/tests/tests/light-push/single_node/index.node.spec.ts @@ -9,6 +9,8 @@ import { utf8ToBytes } from "@waku/sdk"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, generateRandomUint8Array, MessageCollector, ServiceNode, @@ -30,16 +32,14 @@ describe("Waku Light Push: Single Node", function () { let nwaku: ServiceNode; let messageCollector: MessageCollector; - this.beforeEach(async function () { - this.timeout(15000); - [nwaku, waku] = await runNodes(this, [DefaultPubsubTopic]); + beforeEachCustom(this, async () => { + [nwaku, waku] = await runNodes(this.ctx, [DefaultPubsubTopic]); messageCollector = new MessageCollector(nwaku); await nwaku.ensureSubscriptions(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts index 8faf2ee5a..d16893f3e 100644 --- a/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/light-push/single_node/multiple_pubsub.node.spec.ts @@ -18,6 +18,8 @@ import { utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, MessageCollector, ServiceNode, @@ -52,10 +54,9 @@ describe("Waku Light Push : Multiple PubsubTopics", function () { let nimPeerId: PeerId; - this.beforeEach(async function () { - this.timeout(15000); + beforeEachCustom(this, async () => { [nwaku, waku] = await runNodes( - this, + this.ctx, [ singleShardInfoToPubsubTopic(singleShardInfo1), singleShardInfoToPubsubTopic(singleShardInfo2) @@ -66,8 +67,7 @@ describe("Waku Light Push : Multiple PubsubTopics", function () { nimPeerId = await nwaku.getPeerId(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); @@ -211,10 +211,9 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () { let nimPeerId: PeerId; - this.beforeEach(async function () { - this.timeout(15000); + beforeEachCustom(this, async () => { [nwaku, waku] = await runNodes( - this, + this.ctx, [autoshardingPubsubTopic1, autoshardingPubsubTopic2], shardInfo ); @@ -222,8 +221,7 @@ describe("Waku Light Push (Autosharding): Multiple PubsubTopics", function () { nimPeerId = await nwaku.getPeerId(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); @@ -368,9 +366,8 @@ describe("Waku Light Push (named sharding): Multiple PubsubTopics", function () let nimPeerId: PeerId; - this.beforeEach(async function () { - this.timeout(15000); - [nwaku, waku] = await runNodes(this, [ + beforeEachCustom(this, async () => { + [nwaku, waku] = await runNodes(this.ctx, [ autoshardingPubsubTopic1, autoshardingPubsubTopic2 ]); @@ -378,8 +375,7 @@ describe("Waku Light Push (named sharding): Multiple PubsubTopics", function () nimPeerId = await nwaku.getPeerId(); }); - this.afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); diff --git a/packages/tests/tests/log_file.node.spec.ts b/packages/tests/tests/log_file.node.spec.ts index c79b5f2bd..ccbc6edc8 100644 --- a/packages/tests/tests/log_file.node.spec.ts +++ b/packages/tests/tests/log_file.node.spec.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; -import { makeLogFileName } from "../src/index.js"; +import { beforeEachCustom, makeLogFileName } from "../src/index.js"; describe("This", function () { describe("Is", function () { @@ -11,8 +11,8 @@ describe("This", function () { describe("Is also", function () { let testName: string; - beforeEach(function () { - testName = makeLogFileName(this); + beforeEachCustom(this, async () => { + testName = makeLogFileName(this.ctx); }); it("A test", function () { expect(testName).to.equal("This_Is_also_A_test"); diff --git a/packages/tests/tests/metadata.spec.ts b/packages/tests/tests/metadata.spec.ts index a2b3e9abe..172accb1b 100644 --- a/packages/tests/tests/metadata.spec.ts +++ b/packages/tests/tests/metadata.spec.ts @@ -7,6 +7,8 @@ import chai, { expect } from "chai"; import chaiAsPromised from "chai-as-promised"; import { + afterEachCustom, + beforeEachCustom, delay, makeLogFileName, ServiceNode, @@ -20,13 +22,11 @@ describe("Metadata Protocol", function () { let waku: LightNode; let nwaku1: ServiceNode; - beforeEach(function () { - this.timeout(15000); - nwaku1 = new ServiceNode(makeLogFileName(this) + "1"); + beforeEachCustom(this, async () => { + nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1"); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku1], waku); }); diff --git a/packages/tests/tests/multiaddr.node.spec.ts b/packages/tests/tests/multiaddr.node.spec.ts index c263c1dcd..b996d5b88 100644 --- a/packages/tests/tests/multiaddr.node.spec.ts +++ b/packages/tests/tests/multiaddr.node.spec.ts @@ -9,6 +9,8 @@ import { expect } from "chai"; import Sinon, { SinonSpy, SinonStub } from "sinon"; import { + afterEachCustom, + beforeEachCustom, delay, makeLogFileName, ServiceNode, @@ -21,8 +23,7 @@ describe("multiaddr: dialing", function () { let dialPeerSpy: SinonSpy; let isPeerTopicConfigured: SinonStub; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); @@ -54,9 +55,8 @@ describe("multiaddr: dialing", function () { let peerId: PeerId; let multiaddr: Multiaddr; - beforeEach(async function () { - this.timeout(10_000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start(); waku = await createLightNode(); @@ -72,7 +72,7 @@ describe("multiaddr: dialing", function () { dialPeerSpy = Sinon.spy(waku.connectionManager as any, "dialPeer"); }); - afterEach(function () { + afterEachCustom(this, async () => { dialPeerSpy.restore(); }); diff --git a/packages/tests/tests/peer_exchange.node.spec.ts b/packages/tests/tests/peer_exchange.node.spec.ts index 9be7ee234..07a417dcd 100644 --- a/packages/tests/tests/peer_exchange.node.spec.ts +++ b/packages/tests/tests/peer_exchange.node.spec.ts @@ -16,6 +16,8 @@ import { import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, delay, makeLogFileName, ServiceNode, @@ -23,19 +25,18 @@ import { waitForRemotePeerWithCodec } from "../src/index.js"; -describe("Peer Exchange", () => { +describe("Peer Exchange", function () { describe("Locally Run Nodes", () => { let waku: LightNode; let nwaku1: ServiceNode; let nwaku2: ServiceNode; - beforeEach(function () { - nwaku1 = new ServiceNode(makeLogFileName(this) + "1"); - nwaku2 = new ServiceNode(makeLogFileName(this) + "2"); + beforeEachCustom(this, async () => { + nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1"); + nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2"); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku1, nwaku2], waku); }); @@ -126,9 +127,9 @@ describe("Peer Exchange", () => { let nwaku1: ServiceNode; let nwaku2: ServiceNode; - beforeEach(async function () { - nwaku1 = new ServiceNode(makeLogFileName(this) + "1"); - nwaku2 = new ServiceNode(makeLogFileName(this) + "2"); + beforeEachCustom(this, async () => { + nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1"); + nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2"); }); tests({ diff --git a/packages/tests/tests/peer_exchange.optional.spec.ts b/packages/tests/tests/peer_exchange.optional.spec.ts index f61df4a04..15282f651 100644 --- a/packages/tests/tests/peer_exchange.optional.spec.ts +++ b/packages/tests/tests/peer_exchange.optional.spec.ts @@ -8,14 +8,13 @@ import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange"; import { createLightNode, DefaultPubsubTopic } from "@waku/sdk"; import { expect } from "chai"; -import { tearDownNodes } from "../src"; +import { afterEachCustom, tearDownNodes } from "../src"; describe("Peer Exchange", () => { describe("Auto Discovery", function () { let waku: LightNode; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([], waku); }); diff --git a/packages/tests/tests/relay/index.node.spec.ts b/packages/tests/tests/relay/index.node.spec.ts index c62e0b2a3..e964d0b77 100644 --- a/packages/tests/tests/relay/index.node.spec.ts +++ b/packages/tests/tests/relay/index.node.spec.ts @@ -18,6 +18,8 @@ import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, delay, NOISE_KEY_1, NOISE_KEY_2, @@ -31,8 +33,7 @@ describe("Waku Relay", function () { let waku1: RelayNode; let waku2: RelayNode; - beforeEach(async function () { - this.timeout(10000); + beforeEachCustom(this, async () => { log.info("Starting JS Waku instances"); [waku1, waku2] = await Promise.all([ createRelayNode({ staticNoiseKey: NOISE_KEY_1 }).then((waku) => @@ -53,8 +54,7 @@ describe("Waku Relay", function () { log.info("before each hook done"); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([], [waku1, waku2]); }); diff --git a/packages/tests/tests/relay/interop.node.spec.ts b/packages/tests/tests/relay/interop.node.spec.ts index 9efca2340..b3cd5a40f 100644 --- a/packages/tests/tests/relay/interop.node.spec.ts +++ b/packages/tests/tests/relay/interop.node.spec.ts @@ -6,7 +6,9 @@ import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, base64ToUtf8, + beforeEachCustom, delay, makeLogFileName, NOISE_KEY_1, @@ -23,14 +25,13 @@ describe("Waku Relay, Interop", function () { let waku: RelayNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(30000); + beforeEachCustom(this, async () => { waku = await createRelayNode({ staticNoiseKey: NOISE_KEY_1 }); await waku.start(); - nwaku = new ServiceNode(this.test?.ctx?.currentTest?.title + ""); + nwaku = new ServiceNode(this.ctx.test?.ctx?.currentTest?.title + ""); await nwaku.start({ relay: true }); await waku.dial(await nwaku.getMultiaddrWithId()); @@ -40,8 +41,7 @@ describe("Waku Relay, Interop", function () { await nwaku.ensureSubscriptions(); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); @@ -108,7 +108,7 @@ describe("Waku Relay, Interop", function () { let waku2: RelayNode; let nwaku: ServiceNode; - afterEach(async function () { + afterEachCustom(this, async () => { await tearDownNodes(nwaku, [waku1, waku2]); }); diff --git a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts index c6e9a4426..c2e086e22 100644 --- a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts @@ -21,6 +21,7 @@ import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, MessageCollector, NOISE_KEY_1, NOISE_KEY_2, @@ -67,8 +68,7 @@ describe("Waku Relay, multiple pubsub topics", function () { const customDecoder2 = createDecoder(customContentTopic2, singleShardInfo2); const shardInfoBothShards: ShardInfo = { clusterId: 3, shards: [1, 2] }; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([], [waku1, waku2, waku3]); }); @@ -360,8 +360,7 @@ describe("Waku Relay (Autosharding), multiple pubsub topics", function () { contentTopics: [customContentTopic1, customContentTopic2] }; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([], [waku1, waku2, waku3]); }); @@ -671,8 +670,7 @@ describe("Waku Relay (named sharding), multiple pubsub topics", function () { }); const customDecoder2 = createDecoder(customContentTopic2, customPubsubTopic2); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([], [waku1, waku2, waku3]); }); diff --git a/packages/tests/tests/relay/publish.node.spec.ts b/packages/tests/tests/relay/publish.node.spec.ts index cc6ab5f5b..892553655 100644 --- a/packages/tests/tests/relay/publish.node.spec.ts +++ b/packages/tests/tests/relay/publish.node.spec.ts @@ -5,6 +5,8 @@ import { utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, delay, generateRandomUint8Array, MessageCollector, @@ -29,8 +31,7 @@ describe("Waku Relay, Publish", function () { let waku2: RelayNode; let messageCollector: MessageCollector; - beforeEach(async function () { - this.timeout(10000); + beforeEachCustom(this, async () => { log.info("Starting JS Waku instances"); [waku1, waku2] = await Promise.all([ createRelayNode({ @@ -52,8 +53,7 @@ describe("Waku Relay, Publish", function () { await waku2.relay.subscribe([TestDecoder], messageCollector.callback); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([], [waku1, waku2]); }); diff --git a/packages/tests/tests/relay/subscribe.node.spec.ts b/packages/tests/tests/relay/subscribe.node.spec.ts index db7b6d376..804d3a39b 100644 --- a/packages/tests/tests/relay/subscribe.node.spec.ts +++ b/packages/tests/tests/relay/subscribe.node.spec.ts @@ -5,6 +5,8 @@ import { utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, generateTestData, MessageCollector, NOISE_KEY_1, @@ -28,8 +30,7 @@ describe("Waku Relay, Subscribe", function () { let waku2: RelayNode; let messageCollector: MessageCollector; - beforeEach(async function () { - this.timeout(10000); + beforeEachCustom(this, async () => { log.info("Starting JS Waku instances"); [waku1, waku2] = await Promise.all([ createRelayNode({ @@ -46,11 +47,10 @@ describe("Waku Relay, Subscribe", function () { }); await waku1.dial(waku2.libp2p.peerId); log.info("before each hook done"); - messageCollector = new MessageCollector(this.nwaku); + messageCollector = new MessageCollector(this.ctx.nwaku); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([], [waku1, waku2]); }); diff --git a/packages/tests/tests/sharding/peer_management.spec.ts b/packages/tests/tests/sharding/peer_management.spec.ts index 6752cc490..4cf75792b 100644 --- a/packages/tests/tests/sharding/peer_management.spec.ts +++ b/packages/tests/tests/sharding/peer_management.spec.ts @@ -17,6 +17,8 @@ import chaiAsPromised from "chai-as-promised"; import Sinon, { SinonSpy } from "sinon"; import { + afterEachCustom, + beforeEachCustom, delay, makeLogFileName, ServiceNode, @@ -35,15 +37,13 @@ describe("Static Sharding: Peer Management", function () { let dialPeerSpy: SinonSpy; const clusterId = 18; - beforeEach(async function () { - this.timeout(15000); - nwaku1 = new ServiceNode(makeLogFileName(this) + "1"); - nwaku2 = new ServiceNode(makeLogFileName(this) + "2"); - nwaku3 = new ServiceNode(makeLogFileName(this) + "3"); + beforeEachCustom(this, async () => { + nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1"); + nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2"); + nwaku3 = new ServiceNode(makeLogFileName(this.ctx) + "3"); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku1, nwaku2, nwaku3], waku); dialPeerSpy && dialPeerSpy.restore(); }); @@ -216,15 +216,13 @@ describe("Autosharding: Peer Management", function () { let dialPeerSpy: SinonSpy; - beforeEach(async function () { - this.timeout(15000); - nwaku1 = new ServiceNode(makeLogFileName(this) + "1_auto"); - nwaku2 = new ServiceNode(makeLogFileName(this) + "2_auto"); - nwaku3 = new ServiceNode(makeLogFileName(this) + "3_auto"); + beforeEachCustom(this, async () => { + nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1_auto"); + nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2_auto"); + nwaku3 = new ServiceNode(makeLogFileName(this.ctx) + "3_auto"); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku1, nwaku2, nwaku3], waku); dialPeerSpy && dialPeerSpy.restore(); }); diff --git a/packages/tests/tests/sharding/running_nodes.spec.ts b/packages/tests/tests/sharding/running_nodes.spec.ts index 01558717d..f122f33d5 100644 --- a/packages/tests/tests/sharding/running_nodes.spec.ts +++ b/packages/tests/tests/sharding/running_nodes.spec.ts @@ -17,6 +17,8 @@ import { import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, ServiceNode, tearDownNodes @@ -37,18 +39,16 @@ const singleShardInfo2: SingleShardInfo = { clusterId: 0, shard: 3 }; const ContentTopic = "/waku/2/content/test.js"; const ContentTopic2 = "/myapp/1/latest/proto"; -describe("Static Sharding: Running Nodes", () => { +describe("Static Sharding: Running Nodes", function () { let waku: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15_000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, lightpush: true, relay: true }); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); @@ -112,18 +112,16 @@ describe("Static Sharding: Running Nodes", () => { }); }); -describe("Autosharding: Running Nodes", () => { +describe("Autosharding: Running Nodes", function () { let waku: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15_000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, lightpush: true, relay: true }); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/store/cursor.node.spec.ts b/packages/tests/tests/store/cursor.node.spec.ts index 1b3bffd6f..af82dcfd3 100644 --- a/packages/tests/tests/store/cursor.node.spec.ts +++ b/packages/tests/tests/store/cursor.node.spec.ts @@ -5,6 +5,8 @@ import { bytesToUtf8 } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, ServiceNode, tearDownNodes @@ -25,15 +27,13 @@ describe("Waku Store, cursor", function () { let waku2: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, [waku, waku2]); }); diff --git a/packages/tests/tests/store/error_handling.node.spec.ts b/packages/tests/tests/store/error_handling.node.spec.ts index 8f09ff0a6..b63f9482d 100644 --- a/packages/tests/tests/store/error_handling.node.spec.ts +++ b/packages/tests/tests/store/error_handling.node.spec.ts @@ -3,6 +3,8 @@ import { IMessage, type LightNode } from "@waku/interfaces"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, ServiceNode, tearDownNodes @@ -21,16 +23,14 @@ describe("Waku Store, error handling", function () { let waku: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); waku = await startAndConnectLightNode(nwaku); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/store/index.node.spec.ts b/packages/tests/tests/store/index.node.spec.ts index efec0e904..1c4229109 100644 --- a/packages/tests/tests/store/index.node.spec.ts +++ b/packages/tests/tests/store/index.node.spec.ts @@ -19,6 +19,8 @@ import { expect } from "chai"; import { equals } from "uint8arrays/equals"; import { + afterEachCustom, + beforeEachCustom, delay, makeLogFileName, MessageCollector, @@ -48,15 +50,13 @@ describe("Waku Store, general", function () { let waku2: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, [waku, waku2]); }); diff --git a/packages/tests/tests/store/multiple_pubsub.spec.ts b/packages/tests/tests/store/multiple_pubsub.spec.ts index 80c70acfb..cfac638e9 100644 --- a/packages/tests/tests/store/multiple_pubsub.spec.ts +++ b/packages/tests/tests/store/multiple_pubsub.spec.ts @@ -9,6 +9,8 @@ import { import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, NOISE_KEY_1, ServiceNode, @@ -39,9 +41,8 @@ describe("Waku Store, custom pubsub topic", function () { let nwaku: ServiceNode; let nwaku2: ServiceNode; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, pubsubTopic: [customShardedPubsubTopic1, customShardedPubsubTopic2], @@ -54,8 +55,7 @@ describe("Waku Store, custom pubsub topic", function () { ]); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); @@ -215,9 +215,8 @@ describe.skip("Waku Store (Autosharding), custom pubsub topic", function () { contentTopics: [customContentTopic1, customContentTopic2] }; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, pubsubTopic: [autoshardingPubsubTopic1, autoshardingPubsubTopic2], @@ -230,8 +229,7 @@ describe.skip("Waku Store (Autosharding), custom pubsub topic", function () { ]); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); @@ -350,15 +348,13 @@ describe("Waku Store (named sharding), custom pubsub topic", function () { customShardedPubsubTopic2 ); - beforeEach(async function () { - this.timeout(15000); - + beforeEachCustom(this, async () => { const shardInfo = singleShardInfosToShardInfo([ customShardInfo1, customShardInfo2 ]); - nwaku = new ServiceNode(makeLogFileName(this)); + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, relay: true, @@ -377,8 +373,7 @@ describe("Waku Store (named sharding), custom pubsub topic", function () { ); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([nwaku, nwaku2], waku); }); diff --git a/packages/tests/tests/store/order.node.spec.ts b/packages/tests/tests/store/order.node.spec.ts index 2c4282bfd..bb83142a3 100644 --- a/packages/tests/tests/store/order.node.spec.ts +++ b/packages/tests/tests/store/order.node.spec.ts @@ -4,6 +4,8 @@ import { DefaultPubsubTopic } from "@waku/interfaces"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, ServiceNode, tearDownNodes @@ -23,15 +25,13 @@ describe("Waku Store, order", function () { let waku: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/store/page_size.node.spec.ts b/packages/tests/tests/store/page_size.node.spec.ts index 85273f97f..f19e38ea5 100644 --- a/packages/tests/tests/store/page_size.node.spec.ts +++ b/packages/tests/tests/store/page_size.node.spec.ts @@ -3,6 +3,8 @@ import type { LightNode } from "@waku/interfaces"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, ServiceNode, tearDownNodes @@ -20,15 +22,13 @@ describe("Waku Store, page size", function () { let waku: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/store/sorting.node.spec.ts b/packages/tests/tests/store/sorting.node.spec.ts index c60392e14..aa1e63c60 100644 --- a/packages/tests/tests/store/sorting.node.spec.ts +++ b/packages/tests/tests/store/sorting.node.spec.ts @@ -3,6 +3,8 @@ import type { IMessage, LightNode } from "@waku/interfaces"; import { DefaultPubsubTopic } from "@waku/interfaces"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, ServiceNode, tearDownNodes @@ -21,15 +23,13 @@ describe("Waku Store, sorting", function () { let waku: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/store/time_filter.node.spec.ts b/packages/tests/tests/store/time_filter.node.spec.ts index 2da4fd9cd..38a6ced61 100644 --- a/packages/tests/tests/store/time_filter.node.spec.ts +++ b/packages/tests/tests/store/time_filter.node.spec.ts @@ -2,6 +2,8 @@ import type { IMessage, LightNode } from "@waku/interfaces"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, ServiceNode, tearDownNodes @@ -19,15 +21,13 @@ describe("Waku Store, time filter", function () { let waku: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ store: true, lightpush: true, relay: true }); await nwaku.ensureSubscriptions(); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/utils.spec.ts b/packages/tests/tests/utils.spec.ts index fcb1caccc..eb2e6d4a3 100644 --- a/packages/tests/tests/utils.spec.ts +++ b/packages/tests/tests/utils.spec.ts @@ -11,6 +11,8 @@ import chai, { expect } from "chai"; import chaiAsPromised from "chai-as-promised"; import { + afterEachCustom, + beforeEachCustom, delay, makeLogFileName, NOISE_KEY_1, @@ -24,13 +26,12 @@ const TestContentTopic = "/test/1/waku-filter"; const TestEncoder = createEncoder({ contentTopic: TestContentTopic }); const TestDecoder = createDecoder(TestContentTopic); -describe("Util: toAsyncIterator: Filter", () => { +describe("Util: toAsyncIterator: Filter", function () { let waku: LightNode; let nwaku: ServiceNode; - beforeEach(async function () { - this.timeout(15000); - nwaku = new ServiceNode(makeLogFileName(this)); + beforeEachCustom(this, async () => { + nwaku = new ServiceNode(makeLogFileName(this.ctx)); await nwaku.start({ filter: true, lightpush: true, @@ -45,8 +46,7 @@ describe("Util: toAsyncIterator: Filter", () => { await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); }); - afterEach(async function () { - this.timeout(10000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 51551ba17..b6b590b36 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -6,6 +6,7 @@ import { createRelayNode } from "@waku/sdk/relay"; import { expect } from "chai"; import { + afterEachCustom, delay, makeLogFileName, NOISE_KEY_1, @@ -18,8 +19,7 @@ describe("Wait for remote peer", function () { let waku2: LightNode; let nwaku: ServiceNode; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, [waku1, waku2]); }); diff --git a/packages/tests/tests/waku.node.optional.spec.ts b/packages/tests/tests/waku.node.optional.spec.ts index 98cca925e..d9a000934 100644 --- a/packages/tests/tests/waku.node.optional.spec.ts +++ b/packages/tests/tests/waku.node.optional.spec.ts @@ -4,14 +4,18 @@ import { LightNode } from "@waku/interfaces"; import { createLightNode } from "@waku/sdk"; import { expect } from "chai"; -import { makeLogFileName, ServiceNode, tearDownNodes } from "../src/index.js"; +import { + afterEachCustom, + makeLogFileName, + ServiceNode, + tearDownNodes +} from "../src/index.js"; describe("Use static and several ENR trees for bootstrap", function () { let waku: LightNode; let nwaku: ServiceNode; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index 8ae4a0876..b19a06bca 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -21,6 +21,8 @@ import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import { + afterEachCustom, + beforeEachCustom, makeLogFileName, NOISE_KEY_1, NOISE_KEY_2, @@ -37,8 +39,7 @@ describe("Waku Dial [node only]", function () { let waku: LightNode; let nwaku: ServiceNode; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); @@ -102,8 +103,7 @@ describe("Waku Dial [node only]", function () { let waku: LightNode; let nwaku: ServiceNode; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes(nwaku, waku); }); @@ -158,17 +158,16 @@ describe("Waku Dial [node only]", function () { }); }); -describe("Decryption Keys", () => { - afterEach(function () { - if (this.currentTest?.state === "failed") { - console.log(`Test failed, log file name is ${makeLogFileName(this)}`); +describe("Decryption Keys", function () { + afterEachCustom(this, async () => { + if (this.ctx.currentTest?.state === "failed") { + console.log(`Test failed, log file name is ${makeLogFileName(this.ctx)}`); } }); let waku1: RelayNode; let waku2: RelayNode; - beforeEach(async function () { - this.timeout(5000); + beforeEachCustom(this, async () => { [waku1, waku2] = await Promise.all([ createRelayNode({ staticNoiseKey: NOISE_KEY_1 }).then((waku) => waku.start().then(() => waku) @@ -190,8 +189,7 @@ describe("Decryption Keys", () => { ]); }); - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([], [waku1, waku2]); }); @@ -228,12 +226,11 @@ describe("Decryption Keys", () => { }); }); -describe("User Agent", () => { +describe("User Agent", function () { let waku1: Waku; let waku2: Waku; - afterEach(async function () { - this.timeout(15000); + afterEachCustom(this, async () => { await tearDownNodes([], [waku1, waku2]); });