From 4c6c33e6a8ab5ed53d3b95839c345067eb9bb5ed Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Thu, 28 Dec 2023 18:33:07 +0100 Subject: [PATCH] test: migrate types unit tests to vitest (#6224) * Migrate type tests to vitest * Fix the npm scripts --- packages/types/karma.config.cjs | 9 --------- packages/types/package.json | 11 +++++++---- packages/types/test/constants/blobs.test.ts | 2 +- packages/types/test/constants/lightclient.test.ts | 4 ++-- packages/types/test/globalSetup.ts | 2 ++ packages/types/test/unit/ssz.test.ts | 10 +++++----- packages/types/tsconfig.e2e.json | 7 ------- packages/types/vitest.browser.config.ts | 14 ++++++++++++++ packages/types/vitest.config.ts | 11 +++++++++++ packages/types/webpack.test.config.cjs | 5 ----- 10 files changed, 42 insertions(+), 33 deletions(-) delete mode 100644 packages/types/karma.config.cjs create mode 100644 packages/types/test/globalSetup.ts delete mode 100644 packages/types/tsconfig.e2e.json create mode 100644 packages/types/vitest.browser.config.ts create mode 100644 packages/types/vitest.config.ts delete mode 100644 packages/types/webpack.test.config.cjs diff --git a/packages/types/karma.config.cjs b/packages/types/karma.config.cjs deleted file mode 100644 index a3ebb967e2ce..000000000000 --- a/packages/types/karma.config.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const karmaConfig = require("../../karma.base.config.js"); -const webpackConfig = require("./webpack.test.config.cjs"); - -module.exports = function karmaConfigurator(config) { - config.set({ - ...karmaConfig, - webpack: webpackConfig, - }); -}; diff --git a/packages/types/package.json b/packages/types/package.json index 42dffc436875..1a6f3ebf1570 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -61,10 +61,13 @@ "check-types": "tsc", "lint": "eslint --color --ext .ts src/ test/", "lint:fix": "yarn run lint --fix", - "test:unit": "yarn test:constants:minimal && yarn test:constants:mainnet && mocha 'test/unit/**/*.test.ts'", - "test:constants:minimal": "LODESTAR_PRESET=minimal mocha 'test/constants/**/*.test.ts'", - "test:constants:mainnet": "LODESTAR_PRESET=mainnet mocha 'test/constants/**/*.test.ts'", - "test:browsers": "yarn karma start karma.config.cjs", + "test:constants:minimal": "LODESTAR_PRESET=minimal vitest --run --dir test/constants/ --coverage", + "test:constants:mainnet": "LODESTAR_PRESET=mainnet vitest --run --dir test/constants/ --coverage", + "test:unit": "yarn test:constants:minimal && yarn test:constants:mainnet && vitest --run --dir test/unit/ --coverage", + "test:browsers": "yarn test:browsers:chrome && yarn test:browsers:firefox && yarn test:browsers:electron", + "test:browsers:chrome": "vitest --run --browser chrome --config ./vitest.browser.config.ts --dir test/unit", + "test:browsers:firefox": "vitest --run --browser firefox --config ./vitest.browser.config.ts --dir test/unit", + "test:browsers:electron": "echo 'Electron tests will be introduced back in the future as soon vitest supports electron.'", "check-readme": "typescript-docs-verifier" }, "types": "lib/index.d.ts", diff --git a/packages/types/test/constants/blobs.test.ts b/packages/types/test/constants/blobs.test.ts index 42dce38e8636..93193463e76b 100644 --- a/packages/types/test/constants/blobs.test.ts +++ b/packages/types/test/constants/blobs.test.ts @@ -1,4 +1,4 @@ -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import * as constants from "@lodestar/params"; import {ssz} from "../../src/index.js"; diff --git a/packages/types/test/constants/lightclient.test.ts b/packages/types/test/constants/lightclient.test.ts index 3d8652ccc44c..567cc7c3bd17 100644 --- a/packages/types/test/constants/lightclient.test.ts +++ b/packages/types/test/constants/lightclient.test.ts @@ -1,4 +1,4 @@ -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import * as constants from "@lodestar/params"; import {ssz} from "../../src/index.js"; @@ -26,7 +26,7 @@ describe(`${constants.ACTIVE_PRESET}/ Lightclient pre-computed constants`, () => for (const [key, expectedValue] of Object.entries(correctConstants)) { it(key, () => { - expect((constants as unknown as Record)[key]).to.equal(expectedValue); + expect((constants as unknown as Record)[key]).toBe(expectedValue); }); } }); diff --git a/packages/types/test/globalSetup.ts b/packages/types/test/globalSetup.ts new file mode 100644 index 000000000000..0ab57c057472 --- /dev/null +++ b/packages/types/test/globalSetup.ts @@ -0,0 +1,2 @@ +export async function setup(): Promise {} +export async function teardown(): Promise {} diff --git a/packages/types/test/unit/ssz.test.ts b/packages/types/test/unit/ssz.test.ts index 80ddcb12b893..b5c972a8f471 100644 --- a/packages/types/test/unit/ssz.test.ts +++ b/packages/types/test/unit/ssz.test.ts @@ -1,4 +1,4 @@ -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {ssz} from "../../src/index.js"; describe("size", function () { @@ -6,8 +6,8 @@ describe("size", function () { const minSize = ssz.phase0.BeaconState.minSize; const maxSize = ssz.phase0.BeaconState.maxSize; // https://gist.github.com/protolambda/db75c7faa1e94f2464787a480e5d613e - expect(minSize).to.be.equal(2687377); - expect(maxSize).to.be.equal(141837543039377); + expect(minSize).toBe(2687377); + expect(maxSize).toBe(141837543039377); }); }); @@ -24,7 +24,7 @@ describe("container serialization/deserialization field casing(s)", function () const result = ssz.phase0.AttesterSlashing.fromJson(json); const back = ssz.phase0.AttesterSlashing.toJson(result); - expect(back).to.be.deep.equal(json); + expect(back).toEqual(json); }); it("ProposerSlashing", function () { @@ -39,6 +39,6 @@ describe("container serialization/deserialization field casing(s)", function () const result = ssz.phase0.ProposerSlashing.fromJson(json); const back = ssz.phase0.ProposerSlashing.toJson(result); - expect(back).to.be.deep.equal(json); + expect(back).toEqual(json); }); }); diff --git a/packages/types/tsconfig.e2e.json b/packages/types/tsconfig.e2e.json deleted file mode 100644 index cedf626f4124..000000000000 --- a/packages/types/tsconfig.e2e.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "../../tsconfig.e2e.json", - "include": [ - "src", - "test" - ], -} \ No newline at end of file diff --git a/packages/types/vitest.browser.config.ts b/packages/types/vitest.browser.config.ts new file mode 100644 index 000000000000..3c4b48885a33 --- /dev/null +++ b/packages/types/vitest.browser.config.ts @@ -0,0 +1,14 @@ +import {defineConfig, mergeConfig} from "vitest/config"; +import vitestConfig from "../../vitest.base.browser.config"; + +export default mergeConfig( + vitestConfig, + defineConfig({ + test: { + globalSetup: ["./test/globalSetup.ts"], + }, + optimizeDeps: { + exclude: ["@chainsafe/blst"], + }, + }) +); diff --git a/packages/types/vitest.config.ts b/packages/types/vitest.config.ts new file mode 100644 index 000000000000..1df0de848936 --- /dev/null +++ b/packages/types/vitest.config.ts @@ -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"], + }, + }) +); diff --git a/packages/types/webpack.test.config.cjs b/packages/types/webpack.test.config.cjs deleted file mode 100644 index 711c6ac891a7..000000000000 --- a/packages/types/webpack.test.config.cjs +++ /dev/null @@ -1,5 +0,0 @@ -const webpackConfig = require("../../webpack.test.config.js"); - -module.exports = { - ...webpackConfig, -};