-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: move mocha to vitest for beacon-node change (#6028)
* Add jest dependencies * Convert beacon node unit tests to jest * Convert all beacon unit tests to vitest * Update dependencies * Move all e2e tests to vitest * Fix http but which was causing abort to not work * Update the e2e script for the beacon-node * Fix the e2e tests * Update yarn dependencies * Remove .only filter * Fix lint and type errors * Made close callbacks async * Fix the test path * Fix order of resource cleanup * Fix the peer manager for agent version * Fix failing unit test * Update e2e workflow * Add code coverage support for vitest * Match the code coverage configuration to previous nyc config * Fix the formatting for easy code review * Add custom error messages to extremely confusing assertions * Add custom matcher support in the vitest * Update code with feedback
- Loading branch information
1 parent
ab2dfdd
commit c06f4e5
Showing
169 changed files
with
2,966 additions
and
2,353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import {config} from "@lodestar/config/default"; | ||
import {ChainForkConfig} from "@lodestar/config"; | ||
import {getBeaconBlockApi} from "../../src/api/impl/beacon/blocks/index.js"; | ||
import {getMockedBeaconChain, MockedBeaconChain} from "./mockedBeaconChain.js"; | ||
import {MockedBeaconSync, getMockedBeaconSync} from "./beaconSyncMock.js"; | ||
import {MockedBeaconDb, getMockedBeaconDb} from "./mockedBeaconDb.js"; | ||
import {MockedNetwork, getMockedNetwork} from "./mockedNetwork.js"; | ||
|
||
export type ApiImplTestModules = { | ||
forkChoiceStub: MockedBeaconChain["forkChoice"]; | ||
chainStub: MockedBeaconChain; | ||
syncStub: MockedBeaconSync; | ||
dbStub: MockedBeaconDb; | ||
networkStub: MockedNetwork; | ||
blockApi: ReturnType<typeof getBeaconBlockApi>; | ||
config: ChainForkConfig; | ||
}; | ||
|
||
export function setupApiImplTestServer(): ApiImplTestModules { | ||
const chainStub = getMockedBeaconChain(); | ||
const forkChoiceStub = chainStub.forkChoice; | ||
const syncStub = getMockedBeaconSync(); | ||
const dbStub = getMockedBeaconDb(); | ||
const networkStub = getMockedNetwork(); | ||
|
||
const blockApi = getBeaconBlockApi({ | ||
chain: chainStub, | ||
config, | ||
db: dbStub, | ||
network: networkStub, | ||
metrics: null, | ||
}); | ||
|
||
return { | ||
forkChoiceStub, | ||
chainStub, | ||
syncStub, | ||
dbStub, | ||
networkStub, | ||
blockApi, | ||
config, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import {MockedObject, vi} from "vitest"; | ||
import {BeaconSync} from "../../src/sync/index.js"; | ||
|
||
export type MockedBeaconSync = MockedObject<BeaconSync>; | ||
|
||
vi.mock("../../src/sync/index.js", async (requireActual) => { | ||
const mod = await requireActual<typeof import("../../src/sync/index.js")>(); | ||
|
||
const BeaconSync = vi.fn().mockImplementation(() => { | ||
const sync = {}; | ||
Object.defineProperty(sync, "state", {value: undefined, configurable: true}); | ||
|
||
return sync; | ||
}); | ||
|
||
return { | ||
...mod, | ||
BeaconSync, | ||
}; | ||
}); | ||
|
||
export function getMockedBeaconSync(): MockedBeaconSync { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
return vi.mocked(new BeaconSync({})) as MockedBeaconSync; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {vi, MockedObject} from "vitest"; | ||
import {Logger} from "@lodestar/logger"; | ||
|
||
export type MockedLogger = MockedObject<Logger>; | ||
|
||
export function getMockedLogger(): MockedLogger { | ||
return { | ||
debug: vi.fn(), | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
verbose: vi.fn(), | ||
}; | ||
} |
114 changes: 114 additions & 0 deletions
114
packages/beacon-node/test/__mocks__/mockedBeaconChain.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import {vi, MockedObject, Mock} from "vitest"; | ||
import {ForkChoice} from "@lodestar/fork-choice"; | ||
import {config as defaultConfig} from "@lodestar/config/default"; | ||
import {ChainForkConfig} from "@lodestar/config"; | ||
import {BeaconChain} from "../../src/chain/index.js"; | ||
import {ExecutionEngineHttp} from "../../src/execution/engine/http.js"; | ||
import {Eth1ForBlockProduction} from "../../src/eth1/index.js"; | ||
import {OpPool} from "../../src/chain/opPools/opPool.js"; | ||
import {AggregatedAttestationPool} from "../../src/chain/opPools/aggregatedAttestationPool.js"; | ||
import {BeaconProposerCache} from "../../src/chain/beaconProposerCache.js"; | ||
import {QueuedStateRegenerator} from "../../src/chain/regen/index.js"; | ||
import {LightClientServer} from "../../src/chain/lightClient/index.js"; | ||
import {Clock} from "../../src/util/clock.js"; | ||
import {getMockedLogger} from "./loggerMock.js"; | ||
|
||
export type MockedBeaconChain = MockedObject<BeaconChain> & { | ||
getHeadState: Mock<[]>; | ||
forkChoice: MockedObject<ForkChoice>; | ||
executionEngine: MockedObject<ExecutionEngineHttp>; | ||
eth1: MockedObject<Eth1ForBlockProduction>; | ||
opPool: MockedObject<OpPool>; | ||
aggregatedAttestationPool: MockedObject<AggregatedAttestationPool>; | ||
beaconProposerCache: MockedObject<BeaconProposerCache>; | ||
regen: MockedObject<QueuedStateRegenerator>; | ||
bls: { | ||
verifySignatureSets: Mock<[boolean]>; | ||
verifySignatureSetsSameMessage: Mock<[boolean]>; | ||
close: Mock; | ||
canAcceptWork: Mock<[boolean]>; | ||
}; | ||
lightClientServer: MockedObject<LightClientServer>; | ||
}; | ||
vi.mock("@lodestar/fork-choice"); | ||
vi.mock("../../src/execution/engine/http.js"); | ||
vi.mock("../../src/eth1/index.js"); | ||
vi.mock("../../src/chain/opPools/opPool.js"); | ||
vi.mock("../../src/chain/opPools/aggregatedAttestationPool.js"); | ||
vi.mock("../../src/chain/beaconProposerCache.js"); | ||
vi.mock("../../src/chain/regen/index.js"); | ||
vi.mock("../../src/chain/lightClient/index.js"); | ||
vi.mock("../../src/chain/index.js", async (requireActual) => { | ||
const mod = await requireActual<typeof import("../../src/chain/index.js")>(); | ||
|
||
const BeaconChain = vi.fn().mockImplementation(({clock, genesisTime, config}: MockedBeaconChainOptions) => { | ||
return { | ||
config, | ||
opts: {}, | ||
genesisTime, | ||
clock: | ||
clock === "real" | ||
? new Clock({config, genesisTime: 0, signal: new AbortController().signal}) | ||
: { | ||
currentSlot: undefined, | ||
currentSlotWithGossipDisparity: undefined, | ||
isCurrentSlotGivenGossipDisparity: vi.fn(), | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
forkChoice: new ForkChoice(), | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
executionEngine: new ExecutionEngineHttp(), | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
eth1: new Eth1ForBlockProduction(), | ||
opPool: new OpPool(), | ||
aggregatedAttestationPool: new AggregatedAttestationPool(), | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
beaconProposerCache: new BeaconProposerCache(), | ||
produceBlock: vi.fn(), | ||
getCanonicalBlockAtSlot: vi.fn(), | ||
recomputeForkChoiceHead: vi.fn(), | ||
getHeadStateAtCurrentEpoch: vi.fn(), | ||
getHeadState: vi.fn(), | ||
updateBuilderStatus: vi.fn(), | ||
processBlock: vi.fn(), | ||
close: vi.fn(), | ||
logger: getMockedLogger(), | ||
regen: new QueuedStateRegenerator({} as any), | ||
lightClientServer: new LightClientServer({} as any, {} as any), | ||
bls: { | ||
verifySignatureSets: vi.fn().mockResolvedValue(true), | ||
verifySignatureSetsSameMessage: vi.fn().mockResolvedValue([true]), | ||
close: vi.fn().mockResolvedValue(true), | ||
canAcceptWork: vi.fn().mockReturnValue(true), | ||
}, | ||
emitter: new mod.ChainEventEmitter(), | ||
}; | ||
}); | ||
|
||
return { | ||
...mod, | ||
BeaconChain, | ||
}; | ||
}); | ||
|
||
type MockedBeaconChainOptions = { | ||
clock: "real" | "fake"; | ||
genesisTime: number; | ||
config: ChainForkConfig; | ||
}; | ||
|
||
export function getMockedBeaconChain(opts?: Partial<MockedBeaconChainOptions>): MockedBeaconChain { | ||
const {clock, genesisTime, config} = opts ?? {}; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
return new BeaconChain({ | ||
clock: clock ?? "fake", | ||
genesisTime: genesisTime ?? 0, | ||
config: config ?? defaultConfig, | ||
}) as MockedBeaconChain; | ||
} |
Oops, something went wrong.