From 82d7bc224fef824e51fc92fbffff1a541f45ca93 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Thu, 28 Dec 2023 13:04:33 +0100 Subject: [PATCH] test: migrate light-client tests to vitest (#6221) --- packages/light-client/.mocharc.yaml | 6 ------ packages/light-client/.nycrc.json | 3 --- packages/light-client/karma.config.cjs | 9 -------- packages/light-client/package.json | 7 +++++-- .../src/spec/validateLightClientUpdate.ts | 2 +- packages/light-client/src/utils/utils.ts | 2 +- packages/light-client/src/validation.ts | 2 +- packages/light-client/test/globalSetup.ts | 2 ++ .../unit/isValidLightClientHeader.test.ts | 4 ++-- .../light-client/test/unit/sync.node.test.ts | 18 ++++------------ .../test/unit/syncInMemory.test.ts | 21 +++++++------------ packages/light-client/test/unit/utils.test.ts | 4 ++-- .../test/unit/utils/chunkify.test.ts | 4 ++-- .../light-client/test/unit/validation.test.ts | 17 +++++---------- packages/light-client/test/utils/utils.ts | 2 +- packages/light-client/tsconfig.e2e.json | 7 ------- .../light-client/vitest.browser.config.ts | 14 +++++++++++++ packages/light-client/vitest.config.ts | 11 ++++++++++ packages/light-client/webpack.test.config.cjs | 5 ----- 19 files changed, 58 insertions(+), 82 deletions(-) delete mode 100644 packages/light-client/.mocharc.yaml delete mode 100644 packages/light-client/.nycrc.json delete mode 100644 packages/light-client/karma.config.cjs create mode 100644 packages/light-client/test/globalSetup.ts delete mode 100644 packages/light-client/tsconfig.e2e.json create mode 100644 packages/light-client/vitest.browser.config.ts create mode 100644 packages/light-client/vitest.config.ts delete mode 100644 packages/light-client/webpack.test.config.cjs diff --git a/packages/light-client/.mocharc.yaml b/packages/light-client/.mocharc.yaml deleted file mode 100644 index f28ebdf663a0..000000000000 --- a/packages/light-client/.mocharc.yaml +++ /dev/null @@ -1,6 +0,0 @@ -colors: true -timeout: 5000 -exit: true -extension: ["ts"] -node-option: - - "loader=ts-node/esm" diff --git a/packages/light-client/.nycrc.json b/packages/light-client/.nycrc.json deleted file mode 100644 index 69aa626339a0..000000000000 --- a/packages/light-client/.nycrc.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "../../.nycrc.json" -} diff --git a/packages/light-client/karma.config.cjs b/packages/light-client/karma.config.cjs deleted file mode 100644 index a3ebb967e2ce..000000000000 --- a/packages/light-client/karma.config.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const karmaConfig = require("../../karma.base.config.js"); -const webpackConfig = require("./webpack.test.config.cjs"); - -module.exports = function karmaConfigurator(config) { - config.set({ - ...karmaConfig, - webpack: webpackConfig, - }); -}; diff --git a/packages/light-client/package.json b/packages/light-client/package.json index 468e82e0ed0f..defe46d10013 100644 --- a/packages/light-client/package.json +++ b/packages/light-client/package.json @@ -59,8 +59,11 @@ "lint:fix": "yarn run lint --fix", "pretest": "yarn run check-types", "test": "yarn test:unit && yarn test:e2e", - "test:browsers": "yarn karma start karma.config.cjs", - "test:unit": "LODESTAR_PRESET=minimal nyc --cache-dir .nyc_output/.cache -e .ts mocha 'test/unit/**/*.test.ts'", + "test:unit": "vitest --run --dir test/unit/ --coverage", + "test:browsers": "yarn test:browsers:chrome && yarn test:browsers:firefox && yarn test:browsers:electron", + "test:browsers:chrome": "vitest --run --browser chrome --config ./vitest.browser.config.ts --dir test/unit", + "test:browsers:firefox": "vitest --run --browser firefox --config ./vitest.browser.config.ts --dir test/unit", + "test:browsers:electron": "echo 'Electron tests will be introduced back in the future as soon vitest supports electron.'", "check-readme": "typescript-docs-verifier" }, "dependencies": { diff --git a/packages/light-client/src/spec/validateLightClientUpdate.ts b/packages/light-client/src/spec/validateLightClientUpdate.ts index 256be6a99c2c..2629986e85f2 100644 --- a/packages/light-client/src/spec/validateLightClientUpdate.ts +++ b/packages/light-client/src/spec/validateLightClientUpdate.ts @@ -1,4 +1,4 @@ -import bls from "@chainsafe/bls/switchable"; +import bls from "@chainsafe/bls"; import type {PublicKey, Signature} from "@chainsafe/bls/types"; import {Root, ssz, allForks} from "@lodestar/types"; import {ChainForkConfig} from "@lodestar/config"; diff --git a/packages/light-client/src/utils/utils.ts b/packages/light-client/src/utils/utils.ts index c6be99bac8ac..9960921eee90 100644 --- a/packages/light-client/src/utils/utils.ts +++ b/packages/light-client/src/utils/utils.ts @@ -1,4 +1,4 @@ -import bls from "@chainsafe/bls/switchable"; +import bls from "@chainsafe/bls"; import type {PublicKey} from "@chainsafe/bls/types"; import {BitArray} from "@chainsafe/ssz"; import {altair, Root, ssz} from "@lodestar/types"; diff --git a/packages/light-client/src/validation.ts b/packages/light-client/src/validation.ts index a0d6f83d8d02..85c5c35a2cea 100644 --- a/packages/light-client/src/validation.ts +++ b/packages/light-client/src/validation.ts @@ -1,4 +1,4 @@ -import bls from "@chainsafe/bls/switchable"; +import bls from "@chainsafe/bls"; import type {PublicKey, Signature} from "@chainsafe/bls/types"; import {altair, Root, Slot, ssz, allForks} from "@lodestar/types"; import { diff --git a/packages/light-client/test/globalSetup.ts b/packages/light-client/test/globalSetup.ts new file mode 100644 index 000000000000..0ab57c057472 --- /dev/null +++ b/packages/light-client/test/globalSetup.ts @@ -0,0 +1,2 @@ +export async function setup(): Promise {} +export async function teardown(): Promise {} diff --git a/packages/light-client/test/unit/isValidLightClientHeader.test.ts b/packages/light-client/test/unit/isValidLightClientHeader.test.ts index a28ac65ff618..40efa1293231 100644 --- a/packages/light-client/test/unit/isValidLightClientHeader.test.ts +++ b/packages/light-client/test/unit/isValidLightClientHeader.test.ts @@ -1,4 +1,4 @@ -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {fromHexString} from "@chainsafe/ssz"; import {ssz, allForks} from "@lodestar/types"; import {createBeaconConfig, createChainForkConfig, defaultChainConfig} from "@lodestar/config"; @@ -91,7 +91,7 @@ describe("isValidLightClientHeader", function () { testCases.forEach(([name, header]: [string, allForks.LightClientHeader]) => { it(name, function () { const isValid = isValidLightClientHeader(config, header); - expect(isValid).to.be.true; + expect(isValid).toBe(true); }); }); }); diff --git a/packages/light-client/test/unit/sync.node.test.ts b/packages/light-client/test/unit/sync.node.test.ts index 27c924e37462..168bfeceb5f9 100644 --- a/packages/light-client/test/unit/sync.node.test.ts +++ b/packages/light-client/test/unit/sync.node.test.ts @@ -1,5 +1,4 @@ -import {expect} from "chai"; -import {init} from "@chainsafe/bls/switchable"; +import {describe, it, expect, afterEach, vi} from "vitest"; import {JsonPath, toHexString} from "@chainsafe/ssz"; import {computeDescriptor, TreeOffsetProof} from "@chainsafe/persistent-merkle-tree"; import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH} from "@lodestar/params"; @@ -21,21 +20,15 @@ import { lastInMap, } from "../utils/utils.js"; import {startServer, ServerOpts} from "../utils/server.js"; -import {isNode} from "../../src/utils/utils.js"; import {computeSyncPeriodAtSlot} from "../../src/utils/clock.js"; import {LightClientRestTransport} from "../../src/transport/rest.js"; const SOME_HASH = Buffer.alloc(32, 0xff); describe("sync", () => { + vi.setConfig({testTimeout: 30_000}); const afterEachCbs: (() => Promise | unknown)[] = []; - before("init bls", async () => { - // This process has to be done manually because of an issue in Karma runner - // https://github.com/karma-runner/karma/issues/3804 - await init(isNode ? "blst-native" : "herumi"); - }); - afterEach(async () => { await Promise.all(afterEachCbs); afterEachCbs.length = 0; @@ -168,16 +161,13 @@ describe("sync", () => { }); // Ensure that the lightclient head is correct - expect(lightclient.getHead().beacon.slot).to.equal(targetSlot, "lightclient.head is not the targetSlot head"); + expect(lightclient.getHead().beacon.slot).toBe(targetSlot); // Fetch proof of "latestExecutionPayloadHeader.stateRoot" const {proof, header} = await getHeadStateProof(lightclient, api, [["latestExecutionPayloadHeader", "stateRoot"]]); const recoveredState = ssz.bellatrix.BeaconState.createFromProof(proof, header.beacon.stateRoot); - expect(toHexString(recoveredState.latestExecutionPayloadHeader.stateRoot)).to.equal( - toHexString(executionStateRoot), - "Recovered executionStateRoot from getHeadStateProof() not correct" - ); + expect(toHexString(recoveredState.latestExecutionPayloadHeader.stateRoot)).toBe(toHexString(executionStateRoot)); }); }); diff --git a/packages/light-client/test/unit/syncInMemory.test.ts b/packages/light-client/test/unit/syncInMemory.test.ts index df9f5dcd57da..770827e86655 100644 --- a/packages/light-client/test/unit/syncInMemory.test.ts +++ b/packages/light-client/test/unit/syncInMemory.test.ts @@ -1,5 +1,5 @@ -import {expect} from "chai"; -import bls, {init} from "@chainsafe/bls/switchable"; +import {describe, it, expect, beforeAll, vi} from "vitest"; +import bls from "@chainsafe/bls"; import {createBeaconConfig} from "@lodestar/config"; import {chainConfig} from "@lodestar/config/default"; import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH} from "@lodestar/params"; @@ -9,7 +9,6 @@ import {BeaconChainLcMock} from "../mocks/BeaconChainLcMock.js"; import {processLightClientUpdate} from "../utils/naive/update.js"; import {IBeaconChainLc, prepareUpdateNaive} from "../utils/prepareUpdateNaive.js"; import {getInteropSyncCommittee, getSyncAggregateSigningRoot, SyncCommitteeKeys} from "../utils/utils.js"; -import {isNode} from "../../src/utils/utils.js"; function getSyncCommittee( syncCommitteesKeys: Map, @@ -25,7 +24,7 @@ function getSyncCommittee( describe("syncInMemory", function () { // In browser test this process is taking more time than default 2000ms - this.timeout(10000); + vi.setConfig({testTimeout: 10000}); // Fixed params const genValiRoot = Buffer.alloc(32, 9); @@ -35,20 +34,14 @@ describe("syncInMemory", function () { let updateData: {chain: IBeaconChainLc; blockWithSyncAggregate: altair.BeaconBlock}; let update: altair.LightClientUpdate; - before("init bls", async () => { - // This process has to be done manually because of an issue in Karma runner - // https://github.com/karma-runner/karma/issues/3804 - await init(isNode ? "blst-native" : "herumi"); - }); - - before("BLS sanity check", () => { + beforeAll(() => { const sk = bls.SecretKey.fromBytes(Buffer.alloc(32, 1)); - expect(sk.toPublicKey().toHex()).to.equal( + expect(sk.toPublicKey().toHex()).toBe( "0xaa1a1c26055a329817a5759d877a2795f9499b97d6056edde0eea39512f24e8bc874b4471f0501127abb1ea0d9f68ac1" ); }); - before("Generate data for prepareUpdate", () => { + beforeAll(() => { // Create a state that has as nextSyncCommittee the committee 2 const finalizedBlockSlot = SLOTS_PER_EPOCH * EPOCHS_PER_SYNC_COMMITTEE_PERIOD + 1; const headerBlockSlot = finalizedBlockSlot + 1; @@ -107,6 +100,6 @@ describe("syncInMemory", function () { }, }; - expect(() => processLightClientUpdate(config, store, update, currentSlot)).to.not.throw(); + expect(() => processLightClientUpdate(config, store, update, currentSlot)).not.toThrow(); }); }); diff --git a/packages/light-client/test/unit/utils.test.ts b/packages/light-client/test/unit/utils.test.ts index 90a97e05db28..91bfab113431 100644 --- a/packages/light-client/test/unit/utils.test.ts +++ b/packages/light-client/test/unit/utils.test.ts @@ -1,4 +1,4 @@ -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {isValidMerkleBranch} from "../../src/utils/verifyMerkleBranch.js"; import {computeMerkleBranch} from "../utils/utils.js"; @@ -9,6 +9,6 @@ describe("utils", () => { const index = 22; const {root, proof} = computeMerkleBranch(leaf, depth, index); - expect(isValidMerkleBranch(leaf, proof, depth, index, root)).to.equal(true); + expect(isValidMerkleBranch(leaf, proof, depth, index, root)).toBe(true); }); }); diff --git a/packages/light-client/test/unit/utils/chunkify.test.ts b/packages/light-client/test/unit/utils/chunkify.test.ts index 78fc567513da..297637fd70b0 100644 --- a/packages/light-client/test/unit/utils/chunkify.test.ts +++ b/packages/light-client/test/unit/utils/chunkify.test.ts @@ -1,4 +1,4 @@ -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {chunkifyInclusiveRange} from "../../../src/utils/chunkify.js"; describe("utils / chunkifyInclusiveRange", () => { @@ -20,7 +20,7 @@ describe("utils / chunkifyInclusiveRange", () => { for (const {id, from, to, max, result} of testCases) { it(id, () => { - expect(chunkifyInclusiveRange(from, to, max)).to.deep.equal(result); + expect(chunkifyInclusiveRange(from, to, max)).toEqual(result); }); } }); diff --git a/packages/light-client/test/unit/validation.test.ts b/packages/light-client/test/unit/validation.test.ts index 9bda4c86a91f..61442fb4bf8c 100644 --- a/packages/light-client/test/unit/validation.test.ts +++ b/packages/light-client/test/unit/validation.test.ts @@ -1,5 +1,5 @@ -import {expect} from "chai"; -import bls, {init} from "@chainsafe/bls/switchable"; +import {describe, it, expect, beforeAll, vi} from "vitest"; +import bls from "@chainsafe/bls"; import {Tree} from "@chainsafe/persistent-merkle-tree"; import {altair, ssz} from "@lodestar/types"; import {chainConfig} from "@lodestar/config/default"; @@ -14,12 +14,11 @@ import { import {assertValidLightClientUpdate} from "../../src/validation.js"; import {LightClientSnapshotFast, SyncCommitteeFast} from "../../src/types.js"; import {defaultBeaconBlockHeader, getSyncAggregateSigningRoot, signAndAggregate} from "../utils/utils.js"; -import {isNode} from "../../src/utils/utils.js"; describe("validation", function () { // In browser test this process is taking more time than default 2000ms // specially on the CI - this.timeout(15000); + vi.setConfig({testTimeout: 15000}); const genValiRoot = Buffer.alloc(32, 9); const config = createBeaconConfig(chainConfig, genValiRoot); @@ -27,13 +26,7 @@ describe("validation", function () { let update: altair.LightClientUpdate; let snapshot: LightClientSnapshotFast; - before("prepare bls", async () => { - // This process has to be done manually because of an issue in Karma runner - // https://github.com/karma-runner/karma/issues/3804 - await init(isNode ? "blst-native" : "herumi"); - }); - - before("prepare data", function () { + beforeAll(function () { // Update slot must > snapshot slot // attestedHeaderSlot must == updateHeaderSlot + 1 const snapshotHeaderSlot = 1; @@ -106,6 +99,6 @@ describe("validation", function () { }); it("should validate valid update", () => { - expect(() => assertValidLightClientUpdate(config, snapshot.nextSyncCommittee, update)).to.not.throw(); + expect(() => assertValidLightClientUpdate(config, snapshot.nextSyncCommittee, update)).not.toThrow(); }); }); diff --git a/packages/light-client/test/utils/utils.ts b/packages/light-client/test/utils/utils.ts index df9bd4170dcc..455d6ef9997b 100644 --- a/packages/light-client/test/utils/utils.ts +++ b/packages/light-client/test/utils/utils.ts @@ -1,4 +1,4 @@ -import bls from "@chainsafe/bls/switchable"; +import bls from "@chainsafe/bls"; import {PointFormat, PublicKey, SecretKey} from "@chainsafe/bls/types"; import {hasher, Tree} from "@chainsafe/persistent-merkle-tree"; import {BitArray, fromHexString} from "@chainsafe/ssz"; diff --git a/packages/light-client/tsconfig.e2e.json b/packages/light-client/tsconfig.e2e.json deleted file mode 100644 index cedf626f4124..000000000000 --- a/packages/light-client/tsconfig.e2e.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "../../tsconfig.e2e.json", - "include": [ - "src", - "test" - ], -} \ No newline at end of file diff --git a/packages/light-client/vitest.browser.config.ts b/packages/light-client/vitest.browser.config.ts new file mode 100644 index 000000000000..3c4b48885a33 --- /dev/null +++ b/packages/light-client/vitest.browser.config.ts @@ -0,0 +1,14 @@ +import {defineConfig, mergeConfig} from "vitest/config"; +import vitestConfig from "../../vitest.base.browser.config"; + +export default mergeConfig( + vitestConfig, + defineConfig({ + test: { + globalSetup: ["./test/globalSetup.ts"], + }, + optimizeDeps: { + exclude: ["@chainsafe/blst"], + }, + }) +); diff --git a/packages/light-client/vitest.config.ts b/packages/light-client/vitest.config.ts new file mode 100644 index 000000000000..1df0de848936 --- /dev/null +++ b/packages/light-client/vitest.config.ts @@ -0,0 +1,11 @@ +import {defineConfig, mergeConfig} from "vitest/config"; +import vitestConfig from "../../vitest.base.config"; + +export default mergeConfig( + vitestConfig, + defineConfig({ + test: { + globalSetup: ["./test/globalSetup.ts"], + }, + }) +); diff --git a/packages/light-client/webpack.test.config.cjs b/packages/light-client/webpack.test.config.cjs deleted file mode 100644 index 711c6ac891a7..000000000000 --- a/packages/light-client/webpack.test.config.cjs +++ /dev/null @@ -1,5 +0,0 @@ -const webpackConfig = require("../../webpack.test.config.js"); - -module.exports = { - ...webpackConfig, -};