-
-
Notifications
You must be signed in to change notification settings - Fork 290
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
test: move mocha to vitest for beacon-node change #6028
Merged
Merged
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
0591eec
Add jest dependencies
nazarhussain e471638
Convert beacon node unit tests to jest
nazarhussain fc2f715
Convert all beacon unit tests to vitest
nazarhussain 861351b
Update dependencies
nazarhussain a777dbb
Move all e2e tests to vitest
nazarhussain 6f338b7
Fix http but which was causing abort to not work
nazarhussain c041f77
Update the e2e script for the beacon-node
nazarhussain 87ccec2
Fix the e2e tests
nazarhussain ef350a0
Update yarn dependencies
nazarhussain a12f52b
Remove .only filter
nazarhussain 1a1e67a
Fix lint and type errors
nazarhussain 48af4c2
Made close callbacks async
nazarhussain 42725aa
Fix the test path
nazarhussain d9a4d18
Fix order of resource cleanup
nazarhussain 43ef4f0
Fix the peer manager for agent version
nazarhussain ee665df
Fix failing unit test
nazarhussain 7141892
Update e2e workflow
nazarhussain 347a633
Add code coverage support for vitest
nazarhussain 1f207d5
Match the code coverage configuration to previous nyc config
nazarhussain 11ca6ef
Fix the formatting for easy code review
nazarhussain 00b495a
Add custom error messages to extremely confusing assertions
nazarhussain d08b6e8
Add custom matcher support in the vitest
nazarhussain dc4b282
Merge branch 'unstable' into nh/vitest
nazarhussain c17be2d
Update code with feedback
nazarhussain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following change fixes the following problem. I still suggest that we pass the top level
AbortSignal
to thePeerManager
to be used on multiple places to terminate any running timeout.