Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to native node test runner #339

Open
wants to merge 5 commits into
base: pablo/validator-performance-cron
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"private": true,
"engines": {
"node": "20.x"
"node": ">=20.11.0"
},
"workspaces": [
"packages/*"
Expand All @@ -14,7 +14,6 @@
"license": "MIT",
"devDependencies": {
"@types/chai": "^4.3.19",
"@types/mocha": "^10.0.7",
"@types/node": "^22.5.3",
"@typescript-eslint/eslint-plugin": "^8.4.0",
"@typescript-eslint/parser": "^8.4.0",
Expand All @@ -25,10 +24,9 @@
"eslint-plugin-import": "^2.30.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^5.2.1",
"mocha": "^10.7.3",
"prettier": "^3.3.3",
"rimraf": "^4.1.1",
"tsx": "^4.19.0",
"tsx": "^4.19.1",
"typescript": "^5.5.4"
},
"scripts": {
Expand All @@ -42,5 +40,5 @@
"clean:libraries": "rimraf packages/**/node_modules && rimraf node_modules",
"clean:build": "rimraf packages/**/dist && rimraf packages/**/build"
},
"packageManager": "yarn@4.4.1"
"packageManager": "yarn@4.5.0"
}
1 change: 0 additions & 1 deletion packages/brain/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ node_modules
.eslintignore
.eslintrc
.gitignore
.mocharc.yaml
.prettierrc
tests
8 changes: 0 additions & 8 deletions packages/brain/.mocharc.yaml

This file was deleted.

4 changes: 1 addition & 3 deletions packages/brain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"types": "dist/types.d.ts",
"scripts": {
"test": "yarn run test:unit",
"test:unit": "mocha --exit --recursive 'test/**/*.unit.test.ts'",
"test:unit": "node --test --import tsx test/**/*.unit.test.ts",
"dev": "yarn run link:ui && tsx --watch src/index.ts",
"link:ui": "ln -s ../ui/build/ ./uiBuild",
"build": "yarn run clean:dist && yarn run clean:ln && tsc -p tsconfig.json",
Expand All @@ -32,9 +32,7 @@
"@types/express": "^4.17.15",
"@types/lodash-es": "^4.17.12",
"@types/lowdb": "^1.0.15",
"@types/mocha": "^10.0.7",
"@types/sinon": "^10.0.13",
"mocha": "^10.7.3",
"rimraf": "^4.1.1",
"sinon": "^15.0.1",
"typescript": "^5.5.4"
Expand Down
34 changes: 20 additions & 14 deletions packages/brain/test/unit/modules/apiClients/cron.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { before } from "mocha";
import { describe, it } from "node:test";
import { ValidatorApi, Web3SignerApi } from "../../../../src/modules/apiClients/index.js";
import { execSync } from "node:child_process";
import { BrainDataBase } from "../../../../src/modules/db/index.js";
Expand Down Expand Up @@ -69,7 +69,7 @@ describe.skip("Cron: Prater", () => {

const host = "web3signer.web3signer-prater.dappnode";

before(() => {
function before(): void {
const consensusIp = execSync(
`docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${consensusClient.containerName}`
)
Expand Down Expand Up @@ -99,11 +99,10 @@ describe.skip("Cron: Prater", () => {

if (fs.existsSync(testDbName)) fs.unlinkSync(testDbName);
brainDb = new BrainDataBase(testDbName);
});

beforeEach(async function () {
this.timeout(40000);
}
before();

async function beforeEach(): Promise<void> {
console.log("Cleaning DB, validator and signer");

//Clean DB
Expand All @@ -114,9 +113,10 @@ describe.skip("Cron: Prater", () => {

//Clean signer
await signerApi.deleteRemoteKeys({ pubkeys });
});
}

it("Should post fee recipient in DB to validator", async () => {
await beforeEach();
await addSampleValidatorsToAllSources(1);

const pubkeyToTest = pubkeys[0];
Expand All @@ -138,9 +138,10 @@ describe.skip("Cron: Prater", () => {

expect(validatorFeeRecipient.data.ethaddress).to.be.equal(feeRecipient);
expect(validatorFeeRecipient.data.ethaddress).to.be.equal(feeRecipient);
}).timeout(15000);
});

it("Should remove 1 keystore from signer to match pubkeys in DB", async () => {
await beforeEach();
addSampleValidatorsToDB(1);
await addSampleKeystoresToSigner(2);

Expand All @@ -153,9 +154,10 @@ describe.skip("Cron: Prater", () => {
expect(dbPubkeys.length).to.be.equal(1);

expect(signerPubkeys.data[0].validating_pubkey).to.be.equal(dbPubkeys[0]);
}).timeout(15000);
});

it("Should remove 1 keystore from DB to match keystores in signer", async () => {
await beforeEach();
addSampleValidatorsToDB(2);
await addSampleKeystoresToSigner(1);

Expand All @@ -168,9 +170,10 @@ describe.skip("Cron: Prater", () => {
expect(dbPubkeys.length).to.be.equal(1);

expect(signerPubkeys.data[0].validating_pubkey).to.be.equal(dbPubkeys[0]);
}).timeout(15000);
});

it("Should remove all the pubkeys in DB and keystores in signer to match each other", async () => {
await beforeEach();
addSampleValidatorsToDB(2);
await addSampleKeystoresToSigner(2);

Expand All @@ -184,9 +187,10 @@ describe.skip("Cron: Prater", () => {

expect(signerPubkeys.data.length).to.be.equal(0);
expect(dbPubkeys.length).to.be.equal(0);
}).timeout(15000);
});

it("Should keep all the keystores in the signer and the pubkeys in the DB", async () => {
await beforeEach();
addSampleValidatorsToDB(2);
await addSampleKeystoresToSigner(2);

Expand All @@ -201,9 +205,10 @@ describe.skip("Cron: Prater", () => {
//Expect the same pubkeys in both sources (could not be in the same order)
expect(signerPubkeys.data[0].validating_pubkey).to.be.oneOf(dbPubkeys);
expect(signerPubkeys.data[1].validating_pubkey).to.be.oneOf(dbPubkeys);
}).timeout(15000);
});

it("Should delete all pubkeys from validator with empty DB", async () => {
await beforeEach();
await addSamplePubkeysToValidator(1);

console.log("Added pubkeys to validator");
Expand All @@ -217,9 +222,10 @@ describe.skip("Cron: Prater", () => {
console.log("Got validator pubkeys");

expect(validatorPubkeys.data.length).to.be.equal(0);
}).timeout(50000);
});

it("Should add the pubkeys in the DB to the validator", async () => {
await beforeEach();
addSampleValidatorsToDB(2);
await addSampleKeystoresToSigner(2);

Expand All @@ -234,7 +240,7 @@ describe.skip("Cron: Prater", () => {
//Expect the same pubkeys in both sources (could not be in the same order)
expect(validatorPubkeys.data[0].pubkey).to.be.oneOf(pubkeysToTest);
expect(validatorPubkeys.data[1].pubkey).to.be.oneOf(pubkeysToTest);
}).timeout(15000);
});

// AUXILIARY FUNCTIONS //

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from "chai";
import { Network } from "@stakingbrain/common";
import { BlockExplorerApi } from "../../../../src/modules/apiClients";
import { describe, it } from "node:test";
import { ApiParams } from "../../../../src/modules/apiClients/types.js";

describe.skip("Test for fetching validator indexes in every available network", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PostgresClient } from "../../../../src/modules/apiClients/index.js";
import { describe, it } from "node:test";

// This test must be executed with a real database connection

describe.skip("Postgres client", function () {
this.timeout(10 * 1000);
// change the dbUrl on demmand
const dbUrl = "postgres://postgres:[email protected]:5432/web3signer";
const postgresClient = new PostgresClient(dbUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { before } from "mocha";
import { describe, it } from "node:test";
import { ValidatorApi } from "../../../../src/modules/apiClients/index.js";
import { execSync } from "node:child_process";
import { Network } from "@stakingbrain/common";
Expand Down Expand Up @@ -47,7 +47,7 @@ describe.skip("Validator API: Prater", () => {
describe(`Consensus client: ${consensusClient.name}`, () => {
let validatorApi: ValidatorApi;

before(() => {
function before(): void {
const consensusIp = execSync(
`docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${consensusClient.containerName}`
)
Expand All @@ -60,7 +60,8 @@ describe.skip("Validator API: Prater", () => {
},
stakerSpecs.network
);
});
}
before();

it("Should post validators", async () => {
const response = await validatorApi.postRemoteKeys({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it } from "node:test";
import { expect } from "chai";
import { before } from "mocha";
import path from "path";
import fs from "fs";
import { execSync } from "node:child_process";
Expand All @@ -20,7 +20,7 @@ describe.skip("Signer API: Prater", () => {
const host = "web3signer.web3signer-prater.dappnode";
let signerApi: Web3SignerApi;

before(() => {
function before(): void {
// Get consensus client IP
const signerIp = execSync(
`docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${signerContainerName}`
Expand All @@ -34,7 +34,9 @@ describe.skip("Signer API: Prater", () => {
},
Network.Prater
);
});
}

before();

it("Should post validators", async () => {
const keystoresPaths = fs.readdirSync(keystoresPath).filter((file) => file.endsWith(".json"));
Expand All @@ -47,7 +49,7 @@ describe.skip("Signer API: Prater", () => {
});

expect(response.data).to.be.an("array");
}).timeout(10000);
});

it("Should get validators", async () => {
const response = await signerApi.listRemoteKeys();
Expand Down
18 changes: 12 additions & 6 deletions packages/brain/test/unit/modules/db/index.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from "chai";
import { describe, it } from "node:test";
import sinon from "sinon";
import { BrainDataBase } from "../../../../src/modules/db/index.js";
import fs from "fs";
Expand All @@ -12,9 +13,9 @@ describe("DataBase", () => {
const signerDnp = "DAppNodePackage-web3signer.web3signer-prater.dnp.dappnode.eth";
const consensusClientDnp = "DAppNodePackage-validator.prysm-prater.dnp.dappnode.eth";

beforeEach(() => {
function setup(): void {
if (fs.existsSync(testDbName)) fs.unlinkSync(testDbName);
});
}

/**
* Test public initializeDb()
Expand All @@ -24,6 +25,7 @@ describe("DataBase", () => {
* Should do migration if the database file is not found
*/
it.skip("Should do migration if database file not found", async () => {
setup();
const expectedDb = {
"0x821a80380122281580ba8a56cd21956933d43c62fdc8f5b4ec31b2c620e8534e80b6b816c9a2cc8d25568dc4ebcfd47a": {
tag: "solo",
Expand Down Expand Up @@ -98,12 +100,13 @@ describe("DataBase", () => {
console.log(db.data);
// check database
expect(db.data).to.deep.equal(expectedDb);
}).timeout(10000);
});

/**
* Create a new empty database if migration fails
*/
it("Should create a new empty database if migration fails", async () => {
setup();
const db = new BrainDataBase(testDbName);
async function databaseMigration(): Promise<void> {
throw new Error("Database migration failed");
Expand All @@ -117,12 +120,13 @@ describe("DataBase", () => {
expect(fs.existsSync(testDbName)).to.be.true;
db.read();
expect(db.data).to.be.empty;
}).timeout(10000);
});

/**
* Do nothing if the database file exists and is valid
*/
it("Should do nothing if the database file exists and is valid", () => {
setup();
const db = new BrainDataBase(testDbName);
fs.writeFileSync(testDbName, JSON.stringify({}));
const signerApi = sinon.createStubInstance(Web3SignerApi);
Expand Down Expand Up @@ -168,7 +172,9 @@ describe("DataBase", () => {
it("Should add the pubkeys to the database", () => {});
});

after(() => {
function after(): void {
if (fs.existsSync(testDbName)) fs.unlinkSync(testDbName);
});
}

after();
});
1 change: 0 additions & 1 deletion packages/common/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ node_modules
.eslintignore
.eslintrc
.gitignore
.mocharc.yaml
.prettierrc
tests
8 changes: 0 additions & 8 deletions packages/common/.mocharc.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dev": "tsc -w",
"build": "tsc -p tsconfig.json",
"test": "yarn run test:unit",
"test:unit": "mocha -r dotenv/config --exit --recursive 'test/**/*.unit.test.ts'"
"test:unit": "node --test --import tsx test/**/*.unit.test.ts"
},
"devDependencies": {
"@types/sinon": "^10.0.13",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isValidNonWithdrawableBlsAddress, isValidWithdrawableBlsAddress } from "../../../src/utils/index.js";
import { describe, it } from "node:test";
import { expect } from "chai";

describe("isValidWithdrawableBlsAddress", () => {
Expand Down
8 changes: 0 additions & 8 deletions packages/ui/.mocharc.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"start": "vite",
"build": "tsc -b && vite build",
"serve": "vite preview",
"test": "mocha -r dotenv/config --exit --recursive 'test/**/*.test.ts'",
"test": "node --test --import tsx test/**/*.test.ts",
"dev": "vite build --watch --mode=development"
},
"eslintConfig": {
Expand Down
Loading