Skip to content

Commit 275ed6c

Browse files
nazarhussainensi321
authored andcommitted
test: migrate light-client tests to vitest (ChainSafe#6221)
1 parent e8e75fc commit 275ed6c

19 files changed

+58
-82
lines changed

packages/light-client/.mocharc.yaml

-6
This file was deleted.

packages/light-client/.nycrc.json

-3
This file was deleted.

packages/light-client/karma.config.cjs

-9
This file was deleted.

packages/light-client/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@
5959
"lint:fix": "yarn run lint --fix",
6060
"pretest": "yarn run check-types",
6161
"test": "yarn test:unit && yarn test:e2e",
62-
"test:browsers": "yarn karma start karma.config.cjs",
63-
"test:unit": "LODESTAR_PRESET=minimal nyc --cache-dir .nyc_output/.cache -e .ts mocha 'test/unit/**/*.test.ts'",
62+
"test:unit": "vitest --run --dir test/unit/ --coverage",
63+
"test:browsers": "yarn test:browsers:chrome && yarn test:browsers:firefox && yarn test:browsers:electron",
64+
"test:browsers:chrome": "vitest --run --browser chrome --config ./vitest.browser.config.ts --dir test/unit",
65+
"test:browsers:firefox": "vitest --run --browser firefox --config ./vitest.browser.config.ts --dir test/unit",
66+
"test:browsers:electron": "echo 'Electron tests will be introduced back in the future as soon vitest supports electron.'",
6467
"check-readme": "typescript-docs-verifier"
6568
},
6669
"dependencies": {

packages/light-client/src/spec/validateLightClientUpdate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import bls from "@chainsafe/bls/switchable";
1+
import bls from "@chainsafe/bls";
22
import type {PublicKey, Signature} from "@chainsafe/bls/types";
33
import {Root, ssz, allForks} from "@lodestar/types";
44
import {ChainForkConfig} from "@lodestar/config";

packages/light-client/src/utils/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import bls from "@chainsafe/bls/switchable";
1+
import bls from "@chainsafe/bls";
22
import type {PublicKey} from "@chainsafe/bls/types";
33
import {BitArray} from "@chainsafe/ssz";
44
import {altair, Root, ssz} from "@lodestar/types";

packages/light-client/src/validation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import bls from "@chainsafe/bls/switchable";
1+
import bls from "@chainsafe/bls";
22
import type {PublicKey, Signature} from "@chainsafe/bls/types";
33
import {altair, Root, Slot, ssz, allForks} from "@lodestar/types";
44
import {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export async function setup(): Promise<void> {}
2+
export async function teardown(): Promise<void> {}

packages/light-client/test/unit/isValidLightClientHeader.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect} from "chai";
1+
import {describe, it, expect} from "vitest";
22
import {fromHexString} from "@chainsafe/ssz";
33
import {ssz, allForks} from "@lodestar/types";
44
import {createBeaconConfig, createChainForkConfig, defaultChainConfig} from "@lodestar/config";
@@ -91,7 +91,7 @@ describe("isValidLightClientHeader", function () {
9191
testCases.forEach(([name, header]: [string, allForks.LightClientHeader]) => {
9292
it(name, function () {
9393
const isValid = isValidLightClientHeader(config, header);
94-
expect(isValid).to.be.true;
94+
expect(isValid).toBe(true);
9595
});
9696
});
9797
});

packages/light-client/test/unit/sync.node.test.ts

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {expect} from "chai";
2-
import {init} from "@chainsafe/bls/switchable";
1+
import {describe, it, expect, afterEach, vi} from "vitest";
32
import {JsonPath, toHexString} from "@chainsafe/ssz";
43
import {computeDescriptor, TreeOffsetProof} from "@chainsafe/persistent-merkle-tree";
54
import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH} from "@lodestar/params";
@@ -21,21 +20,15 @@ import {
2120
lastInMap,
2221
} from "../utils/utils.js";
2322
import {startServer, ServerOpts} from "../utils/server.js";
24-
import {isNode} from "../../src/utils/utils.js";
2523
import {computeSyncPeriodAtSlot} from "../../src/utils/clock.js";
2624
import {LightClientRestTransport} from "../../src/transport/rest.js";
2725

2826
const SOME_HASH = Buffer.alloc(32, 0xff);
2927

3028
describe("sync", () => {
29+
vi.setConfig({testTimeout: 30_000});
3130
const afterEachCbs: (() => Promise<unknown> | unknown)[] = [];
3231

33-
before("init bls", async () => {
34-
// This process has to be done manually because of an issue in Karma runner
35-
// https://github.com/karma-runner/karma/issues/3804
36-
await init(isNode ? "blst-native" : "herumi");
37-
});
38-
3932
afterEach(async () => {
4033
await Promise.all(afterEachCbs);
4134
afterEachCbs.length = 0;
@@ -168,16 +161,13 @@ describe("sync", () => {
168161
});
169162

170163
// Ensure that the lightclient head is correct
171-
expect(lightclient.getHead().beacon.slot).to.equal(targetSlot, "lightclient.head is not the targetSlot head");
164+
expect(lightclient.getHead().beacon.slot).toBe(targetSlot);
172165

173166
// Fetch proof of "latestExecutionPayloadHeader.stateRoot"
174167
const {proof, header} = await getHeadStateProof(lightclient, api, [["latestExecutionPayloadHeader", "stateRoot"]]);
175168

176169
const recoveredState = ssz.bellatrix.BeaconState.createFromProof(proof, header.beacon.stateRoot);
177-
expect(toHexString(recoveredState.latestExecutionPayloadHeader.stateRoot)).to.equal(
178-
toHexString(executionStateRoot),
179-
"Recovered executionStateRoot from getHeadStateProof() not correct"
180-
);
170+
expect(toHexString(recoveredState.latestExecutionPayloadHeader.stateRoot)).toBe(toHexString(executionStateRoot));
181171
});
182172
});
183173

packages/light-client/test/unit/syncInMemory.test.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {expect} from "chai";
2-
import bls, {init} from "@chainsafe/bls/switchable";
1+
import {describe, it, expect, beforeAll, vi} from "vitest";
2+
import bls from "@chainsafe/bls";
33
import {createBeaconConfig} from "@lodestar/config";
44
import {chainConfig} from "@lodestar/config/default";
55
import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH} from "@lodestar/params";
@@ -9,7 +9,6 @@ import {BeaconChainLcMock} from "../mocks/BeaconChainLcMock.js";
99
import {processLightClientUpdate} from "../utils/naive/update.js";
1010
import {IBeaconChainLc, prepareUpdateNaive} from "../utils/prepareUpdateNaive.js";
1111
import {getInteropSyncCommittee, getSyncAggregateSigningRoot, SyncCommitteeKeys} from "../utils/utils.js";
12-
import {isNode} from "../../src/utils/utils.js";
1312

1413
function getSyncCommittee(
1514
syncCommitteesKeys: Map<SyncPeriod, SyncCommitteeKeys>,
@@ -25,7 +24,7 @@ function getSyncCommittee(
2524

2625
describe("syncInMemory", function () {
2726
// In browser test this process is taking more time than default 2000ms
28-
this.timeout(10000);
27+
vi.setConfig({testTimeout: 10000});
2928

3029
// Fixed params
3130
const genValiRoot = Buffer.alloc(32, 9);
@@ -35,20 +34,14 @@ describe("syncInMemory", function () {
3534
let updateData: {chain: IBeaconChainLc; blockWithSyncAggregate: altair.BeaconBlock};
3635
let update: altair.LightClientUpdate;
3736

38-
before("init bls", async () => {
39-
// This process has to be done manually because of an issue in Karma runner
40-
// https://github.com/karma-runner/karma/issues/3804
41-
await init(isNode ? "blst-native" : "herumi");
42-
});
43-
44-
before("BLS sanity check", () => {
37+
beforeAll(() => {
4538
const sk = bls.SecretKey.fromBytes(Buffer.alloc(32, 1));
46-
expect(sk.toPublicKey().toHex()).to.equal(
39+
expect(sk.toPublicKey().toHex()).toBe(
4740
"0xaa1a1c26055a329817a5759d877a2795f9499b97d6056edde0eea39512f24e8bc874b4471f0501127abb1ea0d9f68ac1"
4841
);
4942
});
5043

51-
before("Generate data for prepareUpdate", () => {
44+
beforeAll(() => {
5245
// Create a state that has as nextSyncCommittee the committee 2
5346
const finalizedBlockSlot = SLOTS_PER_EPOCH * EPOCHS_PER_SYNC_COMMITTEE_PERIOD + 1;
5447
const headerBlockSlot = finalizedBlockSlot + 1;
@@ -107,6 +100,6 @@ describe("syncInMemory", function () {
107100
},
108101
};
109102

110-
expect(() => processLightClientUpdate(config, store, update, currentSlot)).to.not.throw();
103+
expect(() => processLightClientUpdate(config, store, update, currentSlot)).not.toThrow();
111104
});
112105
});

packages/light-client/test/unit/utils.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect} from "chai";
1+
import {describe, it, expect} from "vitest";
22
import {isValidMerkleBranch} from "../../src/utils/verifyMerkleBranch.js";
33
import {computeMerkleBranch} from "../utils/utils.js";
44

@@ -9,6 +9,6 @@ describe("utils", () => {
99
const index = 22;
1010
const {root, proof} = computeMerkleBranch(leaf, depth, index);
1111

12-
expect(isValidMerkleBranch(leaf, proof, depth, index, root)).to.equal(true);
12+
expect(isValidMerkleBranch(leaf, proof, depth, index, root)).toBe(true);
1313
});
1414
});

packages/light-client/test/unit/utils/chunkify.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect} from "chai";
1+
import {describe, it, expect} from "vitest";
22
import {chunkifyInclusiveRange} from "../../../src/utils/chunkify.js";
33

44
describe("utils / chunkifyInclusiveRange", () => {
@@ -20,7 +20,7 @@ describe("utils / chunkifyInclusiveRange", () => {
2020

2121
for (const {id, from, to, max, result} of testCases) {
2222
it(id, () => {
23-
expect(chunkifyInclusiveRange(from, to, max)).to.deep.equal(result);
23+
expect(chunkifyInclusiveRange(from, to, max)).toEqual(result);
2424
});
2525
}
2626
});

packages/light-client/test/unit/validation.test.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {expect} from "chai";
2-
import bls, {init} from "@chainsafe/bls/switchable";
1+
import {describe, it, expect, beforeAll, vi} from "vitest";
2+
import bls from "@chainsafe/bls";
33
import {Tree} from "@chainsafe/persistent-merkle-tree";
44
import {altair, ssz} from "@lodestar/types";
55
import {chainConfig} from "@lodestar/config/default";
@@ -14,26 +14,19 @@ import {
1414
import {assertValidLightClientUpdate} from "../../src/validation.js";
1515
import {LightClientSnapshotFast, SyncCommitteeFast} from "../../src/types.js";
1616
import {defaultBeaconBlockHeader, getSyncAggregateSigningRoot, signAndAggregate} from "../utils/utils.js";
17-
import {isNode} from "../../src/utils/utils.js";
1817

1918
describe("validation", function () {
2019
// In browser test this process is taking more time than default 2000ms
2120
// specially on the CI
22-
this.timeout(15000);
21+
vi.setConfig({testTimeout: 15000});
2322

2423
const genValiRoot = Buffer.alloc(32, 9);
2524
const config = createBeaconConfig(chainConfig, genValiRoot);
2625

2726
let update: altair.LightClientUpdate;
2827
let snapshot: LightClientSnapshotFast;
2928

30-
before("prepare bls", async () => {
31-
// This process has to be done manually because of an issue in Karma runner
32-
// https://github.com/karma-runner/karma/issues/3804
33-
await init(isNode ? "blst-native" : "herumi");
34-
});
35-
36-
before("prepare data", function () {
29+
beforeAll(function () {
3730
// Update slot must > snapshot slot
3831
// attestedHeaderSlot must == updateHeaderSlot + 1
3932
const snapshotHeaderSlot = 1;
@@ -106,6 +99,6 @@ describe("validation", function () {
10699
});
107100

108101
it("should validate valid update", () => {
109-
expect(() => assertValidLightClientUpdate(config, snapshot.nextSyncCommittee, update)).to.not.throw();
102+
expect(() => assertValidLightClientUpdate(config, snapshot.nextSyncCommittee, update)).not.toThrow();
110103
});
111104
});

packages/light-client/test/utils/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import bls from "@chainsafe/bls/switchable";
1+
import bls from "@chainsafe/bls";
22
import {PointFormat, PublicKey, SecretKey} from "@chainsafe/bls/types";
33
import {hasher, Tree} from "@chainsafe/persistent-merkle-tree";
44
import {BitArray, fromHexString} from "@chainsafe/ssz";

packages/light-client/tsconfig.e2e.json

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {defineConfig, mergeConfig} from "vitest/config";
2+
import vitestConfig from "../../vitest.base.browser.config";
3+
4+
export default mergeConfig(
5+
vitestConfig,
6+
defineConfig({
7+
test: {
8+
globalSetup: ["./test/globalSetup.ts"],
9+
},
10+
optimizeDeps: {
11+
exclude: ["@chainsafe/blst"],
12+
},
13+
})
14+
);
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {defineConfig, mergeConfig} from "vitest/config";
2+
import vitestConfig from "../../vitest.base.config";
3+
4+
export default mergeConfig(
5+
vitestConfig,
6+
defineConfig({
7+
test: {
8+
globalSetup: ["./test/globalSetup.ts"],
9+
},
10+
})
11+
);

packages/light-client/webpack.test.config.cjs

-5
This file was deleted.

0 commit comments

Comments
 (0)