Skip to content

Commit

Permalink
test: migrate config unit tests to vitest (#6213)
Browse files Browse the repository at this point in the history
Migrate config unit tests to vitest
  • Loading branch information
nazarhussain authored Dec 21, 2023
1 parent f705b1e commit 2530fae
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"lint": "eslint --color --ext .ts src/",
"lint:fix": "yarn run lint --fix",
"pretest": "yarn run check-types",
"test:unit": "nyc --cache-dir .nyc_output/.cache -e .ts mocha 'test/unit/**/*.test.ts'",
"test:unit": "yarn vitest --run --dir test/unit/ --coverage",
"check-readme": "typescript-docs-verifier"
},
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions packages/config/test/globalSetup.ts
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> {}
15 changes: 6 additions & 9 deletions packages/config/test/unit/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect} from "chai";
import {describe, it, expect} from "vitest";
import {toHexString} from "@chainsafe/ssz";
import {ForkName} from "@lodestar/params";
import {config, chainConfig} from "../../src/default.js";
Expand All @@ -12,30 +12,27 @@ describe("forks", () => {
const fork2 = forks[i + 1];

// Use less equal to be okay with both forks being at Infinity
expect(fork1.epoch).to.be.at.most(
fork2.epoch,
`Forks are not sorted ${fork1.name} ${fork1.epoch} -> ${fork2.name} ${fork2.epoch}`
);
expect(fork1.epoch).toBeLessThanOrEqual(fork2.epoch);
}
});

it("Get phase0 fork for slot 0", () => {
const fork = config.getForkName(0);
expect(fork).to.equal(ForkName.phase0);
expect(fork).toBe(ForkName.phase0);
});

it("correct prev data", () => {
for (let i = 1; i < config.forksAscendingEpochOrder.length; i++) {
const fork = config.forksAscendingEpochOrder[i];
const prevFork = config.forksAscendingEpochOrder[i - 1];
expect(toHexString(fork.prevVersion)).to.equal(toHexString(prevFork.version), `Wrong prevVersion ${fork.name}`);
expect(fork.prevForkName).to.equal(prevFork.name, `Wrong prevName ${fork.name}`);
expect(toHexString(fork.prevVersion)).toBe(toHexString(prevFork.version));
expect(fork.prevForkName).toBe(prevFork.name);
}
});

it("correctly handle pre-genesis", () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const postMergeTestnet = createForkConfig({...chainConfig, ALTAIR_FORK_EPOCH: 0, BELLATRIX_FORK_EPOCH: 0});
expect(postMergeTestnet.getForkName(-1)).to.equal(ForkName.bellatrix);
expect(postMergeTestnet.getForkName(-1)).toBe(ForkName.bellatrix);
});
});
4 changes: 2 additions & 2 deletions packages/config/test/unit/json.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect} from "chai";
import {describe, it, expect} from "vitest";
import {chainConfigFromJson, chainConfigToJson} from "../../src/index.js";
import {chainConfig} from "../../src/default.js";

Expand All @@ -7,6 +7,6 @@ describe("chainConfig JSON", () => {
const json = chainConfigToJson(chainConfig);
const chainConfigRes = chainConfigFromJson(json);

expect(chainConfigRes).to.deep.equal(chainConfig);
expect(chainConfigRes).toEqual(chainConfig);
});
});
11 changes: 11 additions & 0 deletions packages/config/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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"],
},
})
);

0 comments on commit 2530fae

Please sign in to comment.