Skip to content

Commit 0ab9dae

Browse files
authored
chore: migrate from mocha to vitest (#422)
* Add vitest package * Add vitest config files * Update test files to use vitest * Remove unused packages * Update CI task * Update typescript to latest minor version * Update ts config * Update ts-node * Fix lint errors * Fix build step * Fix spec test runner * Update the CI tasks * Fix timeout * Fix a chai version * Remove unused task
1 parent b85c774 commit 0ab9dae

File tree

113 files changed

+3212
-1658
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+3212
-1658
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ jobs:
2727
run: yarn check-types
2828
- name: Lint
2929
run: yarn lint
30-
- name: Tests
31-
run: yarn test
30+
- name: Unit Tests
31+
run: yarn test:unit
32+
- name: Browsers Tests
33+
run: yarn test:browsers
3234

3335
# Download spec tests with cache
3436
- name: Restore spec tests cache
@@ -44,23 +46,23 @@ jobs:
4446
# Otherwise just doing `yarn test:spec` you can't tell which specific suite failed
4547
# many of the suites have identical names for minimal and mainnet
4648
- name: Spec tests general
47-
run: yarn mocha test/spec/generic/index.test.ts
49+
run: yarn test:spec-generic
4850
working-directory: packages/ssz
4951
- name: Spec tests phase0-minimal
50-
run: LODESTAR_PRESET=minimal LODESTAR_FORK=phase0 yarn mocha test/spec/ssz_static.test.ts
52+
run: LODESTAR_FORK=phase0 yarn test:spec-static-minimal
5153
working-directory: packages/ssz
5254
- name: Spec tests phase0-mainnet
53-
run: LODESTAR_PRESET=mainnet LODESTAR_FORK=phase0 yarn mocha test/spec/ssz_static.test.ts
55+
run: LODESTAR_FORK=phase0 yarn test:spec-static-mainnet
5456
working-directory: packages/ssz
5557
- name: Spec tests altair-minimal
56-
run: LODESTAR_PRESET=minimal LODESTAR_FORK=altair yarn mocha test/spec/ssz_static.test.ts
58+
run: LODESTAR_FORK=altair yarn test:spec-static-minimal
5759
working-directory: packages/ssz
5860
- name: Spec tests altair-mainnet
59-
run: LODESTAR_PRESET=mainnet LODESTAR_FORK=altair yarn mocha test/spec/ssz_static.test.ts
61+
run: LODESTAR_FORK=altair yarn test:spec-static-mainnet
6062
working-directory: packages/ssz
6163
- name: Spec tests bellatrix-minimal
62-
run: LODESTAR_PRESET=minimal LODESTAR_FORK=bellatrix yarn mocha test/spec/ssz_static.test.ts
64+
run: LODESTAR_FORK=bellatrix yarn test:spec-static-minimal
6365
working-directory: packages/ssz
6466
- name: Spec tests bellatrix-mainnet
65-
run: LODESTAR_PRESET=mainnet LODESTAR_FORK=bellatrix yarn mocha test/spec/ssz_static.test.ts
67+
run: LODESTAR_FORK=bellatrix yarn test:spec-static-mainnet
6668
working-directory: packages/ssz

package.json

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"build": "yarn clean && lerna run build",
1212
"lint": "lerna run lint",
1313
"check-types": "lerna run check-types",
14-
"coverage": "lerna run coverage",
14+
"test:unit": "lerna run test:unit",
15+
"test:browsers": "lerna run test:browsers",
1516
"test": "lerna run test",
1617
"benchmark:files": "NODE_OPTIONS=--max_old_space_size=4096 benchmark --config .benchrc.yaml",
1718
"benchmark": "yarn benchmark:files 'packages/*/test/perf/**/*.test.ts'",
@@ -26,37 +27,34 @@
2627
"@babel/preset-typescript": "^7.14.5",
2728
"@babel/register": "^7.15.3",
2829
"@dapplion/benchmark": "^0.2.2",
29-
"@types/chai": "^4.2.15",
30-
"@types/mocha": "^8.2.2",
30+
"@types/mocha": "^9.0.1",
31+
"@types/chai": "^4.2.19",
3132
"@types/node": "^14.14.17",
3233
"@typescript-eslint/eslint-plugin": "^4.19.0",
3334
"@typescript-eslint/parser": "^4.19.0",
35+
"@vitest/browser": "^2.1.4",
36+
"@vitest/coverage-v8": "^2.1.4",
3437
"babel-loader": "^8.2.2",
35-
"chai": "^4.3.4",
3638
"eslint": "^7.30.0",
3739
"eslint-plugin-import": "^2.23.4",
3840
"eslint-plugin-no-only-tests": "^2.4.0",
3941
"eslint-plugin-node": "^11.1.0",
4042
"eslint-plugin-prettier": "^3.3.1",
41-
"karma": "^6.3.16",
42-
"karma-babel-preprocessor": "^8.0.1",
43-
"karma-chai": "^0.1.0",
44-
"karma-chrome-launcher": "^3.1.0",
45-
"karma-firefox-launcher": "^1.2.0",
46-
"karma-mocha": "^1.3.0",
47-
"karma-webpack": "^5.0.0",
4843
"lerna": "^8.1.3",
49-
"mocha": "^8.3.2",
50-
"nyc": "^15.0.0",
44+
"mocha": "^9.0.1",
5145
"prettier": "^2.2.1",
5246
"ts-loader": "^9.2.8",
53-
"ts-node": "^9.1.1",
54-
"typescript": "~4.2.3",
47+
"ts-node": "^10.9.2",
48+
"typescript": "^4.9.5",
49+
"vite-plugin-node-polyfills": "^0.22.0",
50+
"vitest": "^2.1.4",
51+
"webdriverio": "^9.2.11",
5552
"webpack": "^5.75.0",
5653
"webpack-cli": "^5.0.1",
5754
"webpack-dev-server": "^3.11.2"
5855
},
5956
"resolutions": {
60-
"@types/react": "^17.0"
57+
"@types/react": "^17.0",
58+
"chai": "4.5.0"
6159
}
6260
}

packages/as-sha256/karma.config.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

packages/as-sha256/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
"build:lib": "tsc -p tsconfig.build.json",
3939
"build:web": "webpack --mode production --entry ./index.js --output ./dist/as-sha256.min.js",
4040
"test": "yarn run test:unit",
41-
"test:unit": "yarn run test:unit:node && yarn run test:unit:browser",
42-
"test:unit:node": "mocha -r ts-node/register test/unit/*.test.ts",
43-
"test:unit:browser": "karma start --single-run --browsers ChromeHeadless,FirefoxHeadless karma.config.js",
41+
"test:unit": "vitest run --dir test/unit/",
42+
"test:browsers": "yarn test:browsers:chrome && yarn test:browsers:firefox",
43+
"test:browsers:chrome": "vitest run --browser chrome --config ./vitest.browser.config.ts --dir test/unit",
44+
"test:browsers:firefox": "vitest run --browser firefox --config ./vitest.browser.config.ts --dir test/unit",
4445
"benchmark": "node -r ts-node/register ./node_modules/.bin/benchmark 'test/perf/*.test.ts'",
4546
"benchmark:local": "yarn benchmark --local",
4647
"test:ci": "yarn test:as-ci"

packages/as-sha256/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {allocUnsafe} from "./alloc";
22
import {newInstance} from "./wasm";
3-
import {HashObject, byteArrayIntoHashObject, byteArrayToHashObject, hashObjectToByteArray} from "./hashObject";
3+
import type {HashObject} from "./hashObject";
4+
import {byteArrayIntoHashObject, byteArrayToHashObject, hashObjectToByteArray} from "./hashObject";
45
import SHA256 from "./sha256";
56
export {HashObject, byteArrayToHashObject, hashObjectToByteArray, byteArrayIntoHashObject, SHA256};
67

packages/as-sha256/test/unit/index.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1+
import {describe, it, expect} from "vitest";
12
import {Buffer} from "buffer";
23
import * as sha256 from "../../src";
3-
import {expect} from "chai";
44

55
describe("sha256", function () {
66
describe("digest function", function () {
77
it("abc", function () {
88
const input = Buffer.from("abc", "utf8");
9-
expect(Buffer.from(sha256.digest(input)).toString("hex")).to.be.equal(
9+
expect(Buffer.from(sha256.digest(input)).toString("hex")).toEqual(
1010
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
1111
);
1212
});
1313

1414
it("empty string", function () {
1515
const input = Buffer.from("", "utf8");
16-
expect(Buffer.from(sha256.digest(input)).toString("hex")).to.be.equal(
16+
expect(Buffer.from(sha256.digest(input)).toString("hex")).toEqual(
1717
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
1818
);
1919
});
2020

2121
it("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", function () {
2222
const input = Buffer.from("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "utf8");
23-
expect(Buffer.from(sha256.digest(input)).toString("hex")).to.be.equal(
23+
expect(Buffer.from(sha256.digest(input)).toString("hex")).toEqual(
2424
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
2525
);
2626
});
@@ -30,7 +30,7 @@ describe("sha256", function () {
3030
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
3131
"utf8"
3232
);
33-
expect(Buffer.from(sha256.digest(input)).toString("hex")).to.be.equal(
33+
expect(Buffer.from(sha256.digest(input)).toString("hex")).toEqual(
3434
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
3535
);
3636
});
@@ -45,7 +45,7 @@ describe("sha256", function () {
4545
131, 72, 178, 215, 235, 20, 207, 110,
4646
]);
4747
expect(output).to.be.deep.equal(expectedOutput, "incorrect digest64 result");
48-
expect(Buffer.from(output).toString("hex")).to.be.equal(
48+
expect(Buffer.from(output).toString("hex")).toEqual(
4949
"be39380ff1d0261e6f37dafe4278b662ef611f1cb2f7c0a18348b2d7eb14cf6e"
5050
);
5151
// digestTwoHashObjects should be the same to digest64
@@ -57,7 +57,7 @@ describe("sha256", function () {
5757
const output2 = new Uint8Array(32);
5858
sha256.hashObjectToByteArray(obj, output2, 0);
5959
for (let i = 0; i < 32; i++) {
60-
expect(output2[i]).to.be.equal(output[i], "failed at index" + i);
60+
expect(output2[i]).toEqual(output[i], "failed at index" + i);
6161
}
6262
expect(output2).to.be.deep.equal(expectedOutput, "incorrect digestTwoHashObjects result");
6363

@@ -69,16 +69,16 @@ describe("sha256", function () {
6969
const input = Buffer.from("harkamalharkamalharkamalharkamalharkamalharkamalharkamalharkamal", "utf8");
7070
const output = Buffer.from(sha256.digest(input)).toString("hex");
7171
const output64 = Buffer.from(sha256.digest64(input)).toString("hex");
72-
expect(output).to.be.equal(output64);
72+
expect(output).toEqual(output64);
7373
});
7474

7575
it("1024 digest test", function () {
7676
const inputStr = "12345678".repeat(128);
7777
const input = Buffer.from(inputStr, "utf8");
78-
expect(input.length).to.be.equal(1024);
78+
expect(input.length).toEqual(1024);
7979

8080
const output = Buffer.from(sha256.digest(input)).toString("hex");
81-
expect(output).to.be.equal("54c7cb8a82d68145fd5f5da4103f5a66f422dbea23d9fc9f40f59b1dcf5403a9");
81+
expect(output).toEqual("54c7cb8a82d68145fd5f5da4103f5a66f422dbea23d9fc9f40f59b1dcf5403a9");
8282
});
8383
});
8484
});

packages/as-sha256/test/unit/simd.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect} from "chai";
1+
import {describe, it, expect} from "vitest";
22
import crypto from "crypto";
33
import {byteArrayToHashObject, hashObjectToByteArray} from "../../src/hashObject";
44
import * as sha256 from "../../src";
@@ -10,11 +10,11 @@ describe("Test SIMD implementation of as-sha256", () => {
1010
const input = Buffer.from(input1 + input2, "utf8");
1111
const outputs = sha256.batchHash4UintArray64s([input, input, input, input]);
1212
const expectedOutput = new Uint8Array([
13-
190, 57, 56, 15, 241, 208, 38, 30, 111, 55, 218, 254, 66, 120, 182, 98, 239, 97, 31, 28, 178, 247, 192, 161,
14-
131, 72, 178, 215, 235, 20, 207, 110,
13+
190, 57, 56, 15, 241, 208, 38, 30, 111, 55, 218, 254, 66, 120, 182, 98, 239, 97, 31, 28, 178, 247, 192, 161, 131,
14+
72, 178, 215, 235, 20, 207, 110,
1515
]);
1616
for (let i = 0; i < 4; i++) {
17-
expect(outputs[i]).to.be.deep.equal(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
17+
expect(outputs[i]).toEqualWithMessage(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
1818
}
1919
});
2020

@@ -23,10 +23,10 @@ describe("Test SIMD implementation of as-sha256", () => {
2323
const input = crypto.randomBytes(64);
2424
const outputs = sha256.batchHash4UintArray64s([input, input, input, input]);
2525
const expectedOutput = sha256.digest64(input);
26-
expect(outputs[0]).to.be.deep.equal(expectedOutput);
27-
expect(outputs[1]).to.be.deep.equal(expectedOutput);
28-
expect(outputs[2]).to.be.deep.equal(expectedOutput);
29-
expect(outputs[3]).to.be.deep.equal(expectedOutput);
26+
expect(outputs[0]).toEqual(expectedOutput);
27+
expect(outputs[1]).toEqual(expectedOutput);
28+
expect(outputs[2]).toEqual(expectedOutput);
29+
expect(outputs[3]).toEqual(expectedOutput);
3030
}
3131
});
3232

@@ -35,13 +35,13 @@ describe("Test SIMD implementation of as-sha256", () => {
3535
const inputHashObject = byteArrayToHashObject(Buffer.from(input1, "utf8"), 0);
3636
const outputs = sha256.batchHash4HashObjectInputs(Array.from({length: 8}, () => inputHashObject));
3737
const expectedOutput = new Uint8Array([
38-
190, 57, 56, 15, 241, 208, 38, 30, 111, 55, 218, 254, 66, 120, 182, 98, 239, 97, 31, 28, 178, 247, 192, 161,
39-
131, 72, 178, 215, 235, 20, 207, 110,
38+
190, 57, 56, 15, 241, 208, 38, 30, 111, 55, 218, 254, 66, 120, 182, 98, 239, 97, 31, 28, 178, 247, 192, 161, 131,
39+
72, 178, 215, 235, 20, 207, 110,
4040
]);
4141
for (let i = 0; i < 4; i++) {
4242
const output = new Uint8Array(32);
4343
hashObjectToByteArray(outputs[i], output, 0);
44-
expect(output).to.be.deep.equal(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
44+
expect(output).toEqualWithMessage(expectedOutput, "incorrect batchHash4UintArray64s result " + i);
4545
}
4646
});
4747

@@ -59,7 +59,7 @@ describe("Test SIMD implementation of as-sha256", () => {
5959

6060
const expectedOutputs = Array.from({length: numHash}, (_, i) => sha256.digest64(inputs[i]));
6161
for (let i = 0; i < numHash; i++) {
62-
expect(output.subarray(i * 32, (i + 1) * 32)).to.be.deep.equal(expectedOutputs[i]);
62+
expect(output.subarray(i * 32, (i + 1) * 32)).toEqual(expectedOutputs[i]);
6363
}
6464
});
6565
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {defineConfig, mergeConfig} from "vitest/config";
2+
import vitestConfig from "../../vitest.base.browser.config";
3+
4+
export default mergeConfig(vitestConfig, defineConfig({}));

packages/as-sha256/vitest.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {defineConfig, mergeConfig} from "vitest/config";
2+
import baseConfig from "../../vitest.base.unit.config";
3+
4+
export default mergeConfig(baseConfig, defineConfig({}));

packages/persistent-merkle-tree/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"benchmark:files": "node --max-old-space-size=4096 --expose-gc -r ts-node/register ../../node_modules/.bin/benchmark",
2525
"benchmark": "yarn benchmark:files 'test/perf/*.test.ts'",
2626
"benchmark:local": "yarn benchmark --local",
27-
"test": "mocha -r ts-node/register 'test/unit/**/*.test.ts'"
27+
"test": "vitest run --dir test/unit"
2828
},
2929
"pre-push": [
3030
"lint"

0 commit comments

Comments
 (0)