-
-
Notifications
You must be signed in to change notification settings - Fork 287
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: migrate api unit tests to vitest #6177
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export async function setup(): Promise<void> {} | ||
export async function teardown(): Promise<void> {} |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,10 +3,10 @@ import {ssz} from "@lodestar/types"; | |||||||||
import {Api} from "../../../../src/beacon/routes/validator.js"; | ||||||||||
import {GenericServerTestCases} from "../../../utils/genericServerTest.js"; | ||||||||||
|
||||||||||
const ZERO_HASH = Buffer.alloc(32, 0); | ||||||||||
const ZERO_HASH_HEX = "0x" + ZERO_HASH.toString("hex"); | ||||||||||
const randaoReveal = Buffer.alloc(96, 1); | ||||||||||
const selectionProof = Buffer.alloc(96, 1); | ||||||||||
const ZERO_HASH = new Uint8Array(32); | ||||||||||
const ZERO_HASH_HEX = "0x" + Buffer.from(ZERO_HASH).toString("hex"); | ||||||||||
const randaoReveal = new Uint8Array(96).fill(1); | ||||||||||
const selectionProof = new Uint8Array(96).fill(1); | ||||||||||
const graffiti = "a".repeat(32); | ||||||||||
const feeRecipient = "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; | ||||||||||
|
||||||||||
|
@@ -17,7 +17,7 @@ export const testData: GenericServerTestCases<Api> = { | |||||||||
executionOptimistic: true, | ||||||||||
data: [ | ||||||||||
{ | ||||||||||
pubkey: Buffer.alloc(48, 1), | ||||||||||
pubkey: new Uint8Array(48).fill(1), | ||||||||||
validatorIndex: 2, | ||||||||||
committeeIndex: 3, | ||||||||||
committeeLength: 4, | ||||||||||
|
@@ -33,23 +33,35 @@ export const testData: GenericServerTestCases<Api> = { | |||||||||
args: [1000], | ||||||||||
res: { | ||||||||||
executionOptimistic: true, | ||||||||||
data: [{slot: 1, validatorIndex: 2, pubkey: Buffer.alloc(48, 3)}], | ||||||||||
data: [{slot: 1, validatorIndex: 2, pubkey: new Uint8Array(48).fill(3)}], | ||||||||||
dependentRoot: ZERO_HASH_HEX, | ||||||||||
}, | ||||||||||
}, | ||||||||||
getSyncCommitteeDuties: { | ||||||||||
args: [1000, [1, 2, 3]], | ||||||||||
res: { | ||||||||||
executionOptimistic: true, | ||||||||||
data: [{pubkey: Buffer.alloc(48, 1), validatorIndex: 2, validatorSyncCommitteeIndices: [3]}], | ||||||||||
data: [{pubkey: Uint8Array.from(Buffer.alloc(48, 1)), validatorIndex: 2, validatorSyncCommitteeIndices: [3]}], | ||||||||||
}, | ||||||||||
}, | ||||||||||
produceBlock: { | ||||||||||
args: [32000, randaoReveal, graffiti], | ||||||||||
args: [ | ||||||||||
32000, | ||||||||||
randaoReveal, | ||||||||||
graffiti, | ||||||||||
undefined, | ||||||||||
{feeRecipient: undefined, builderSelection: undefined, strictFeeRecipientCheck: undefined}, | ||||||||||
], | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's because we are reusing reqSerializer for lodestar/packages/api/src/beacon/routes/validator.ts Lines 608 to 611 in 1a63b07
To make it logically correct we should have different serializers for each method. Would be nice to know why it's actually written in this way in first place. @g11tech Do you remember any rationale for it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because produceBlockV3 serializer is superset of all of them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can create separate serializer fns for each of these but then delegate the call to but we anyway will remove these fns and not support them so we can leave it like that as well |
||||||||||
res: {data: ssz.phase0.BeaconBlock.defaultValue()}, | ||||||||||
}, | ||||||||||
produceBlockV2: { | ||||||||||
args: [32000, randaoReveal, graffiti], | ||||||||||
args: [ | ||||||||||
32000, | ||||||||||
randaoReveal, | ||||||||||
graffiti, | ||||||||||
undefined, | ||||||||||
{feeRecipient: undefined, builderSelection: undefined, strictFeeRecipientCheck: undefined}, | ||||||||||
], | ||||||||||
res: { | ||||||||||
data: ssz.altair.BeaconBlock.defaultValue(), | ||||||||||
version: ForkName.altair, | ||||||||||
|
@@ -74,7 +86,13 @@ export const testData: GenericServerTestCases<Api> = { | |||||||||
}, | ||||||||||
}, | ||||||||||
produceBlindedBlock: { | ||||||||||
args: [32000, randaoReveal, graffiti], | ||||||||||
args: [ | ||||||||||
32000, | ||||||||||
randaoReveal, | ||||||||||
graffiti, | ||||||||||
undefined, | ||||||||||
{feeRecipient: undefined, builderSelection: undefined, strictFeeRecipientCheck: undefined}, | ||||||||||
], | ||||||||||
res: { | ||||||||||
data: ssz.bellatrix.BlindedBeaconBlock.defaultValue(), | ||||||||||
version: ForkName.bellatrix, | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||
import crypto from "node:crypto"; | ||||||
import http from "node:http"; | ||||||
import {expect} from "chai"; | ||||||
import {describe, it, expect, afterEach} from "vitest"; | ||||||
import {FetchError, FetchErrorType, fetch} from "../../../src/utils/client/fetch.js"; | ||||||
|
||||||
describe("FetchError", function () { | ||||||
|
@@ -116,12 +116,16 @@ describe("FetchError", function () { | |||||
); | ||||||
} | ||||||
|
||||||
await expect(fetch(url, {signal: signalHandler?.()})).to.be.rejected.then((error: FetchError) => { | ||||||
expect(error.type).to.be.equal(testCase.errorType); | ||||||
expect(error.code).to.be.equal(testCase.errorCode); | ||||||
await expect(fetch(url, {signal: signalHandler?.()})).rejects.toSatisfy((err) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't this be changed to?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that's the strict type issue we can't assign type inside the function descriptor.
|
||||||
expect(err).toBeInstanceOf(FetchError); | ||||||
expect((err as FetchError).code).toBe(testCase.errorCode); | ||||||
expect((err as FetchError).type).toBe(testCase.errorType); | ||||||
|
||||||
if (testCase.expectCause) { | ||||||
expect(error.cause).to.be.instanceof(Error); | ||||||
expect((err as FetchError).cause).toBeInstanceOf(Error); | ||||||
} | ||||||
|
||||||
return true; | ||||||
}); | ||||||
}); | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import {expect} from "chai"; | ||
import {describe, expect, it} from "vitest"; | ||
import {EventType} from "../../../src/beacon/routes/events.js"; | ||
import {stringifyQuery} from "../../../src/utils/client/format.js"; | ||
|
||
describe("client / utils / format", () => { | ||
it("Should repeat topic query", () => { | ||
expect(stringifyQuery({topics: [EventType.finalizedCheckpoint]})).to.equal("topics=finalized_checkpoint"); | ||
expect(stringifyQuery({topics: [EventType.finalizedCheckpoint]})).toBe("topics=finalized_checkpoint"); | ||
}); | ||
}); |
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.
One observation for reviewers, it seems that
chai
was matching theBuffer
andUint8Array
equality. Which is logically wrong, if we are expecting aBuffer
instance of a value then assertion should pass otherwise fail.For this reason have to update a lot of fixture data in this PR, converting
Buffer
to actual returningUint8Array
instances.