diff --git a/packages/utils/.mocharc.yml b/packages/utils/.mocharc.yml deleted file mode 100644 index 8b4eb53ed37a..000000000000 --- a/packages/utils/.mocharc.yml +++ /dev/null @@ -1,3 +0,0 @@ -colors: true -node-option: - - "loader=ts-node/esm" diff --git a/packages/utils/karma.config.cjs b/packages/utils/karma.config.cjs deleted file mode 100644 index a3ebb967e2ce..000000000000 --- a/packages/utils/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/utils/package.json b/packages/utils/package.json index 8490ba2fa89d..8fd1e2a360d5 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -31,8 +31,12 @@ "lint": "eslint --color --ext .ts src/ test/", "lint:fix": "yarn run lint --fix", "pretest": "yarn run check-types", - "test:unit": "mocha 'test/**/*.test.ts'", - "test:browsers": "yarn karma start karma.config.cjs", + "test:unit": "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.'", + "test:e2e": "LODESTAR_PRESET=minimal vitest --run --poolOptions.threads.singleThread true --dir test/e2e", "check-readme": "typescript-docs-verifier" }, "types": "lib/index.d.ts", diff --git a/packages/utils/test/globalSetup.ts b/packages/utils/test/globalSetup.ts new file mode 100644 index 000000000000..0ab57c057472 --- /dev/null +++ b/packages/utils/test/globalSetup.ts @@ -0,0 +1,2 @@ +export async function setup(): Promise {} +export async function teardown(): Promise {} diff --git a/packages/utils/test/setup.ts b/packages/utils/test/setup.ts deleted file mode 100644 index b83e6cb78511..000000000000 --- a/packages/utils/test/setup.ts +++ /dev/null @@ -1,6 +0,0 @@ -import chai from "chai"; -import chaiAsPromised from "chai-as-promised"; -import sinonChai from "sinon-chai"; - -chai.use(chaiAsPromised); -chai.use(sinonChai); diff --git a/packages/utils/test/unit/assert.test.ts b/packages/utils/test/unit/assert.test.ts index e20595b69cfc..0555bcbd01a0 100644 --- a/packages/utils/test/unit/assert.test.ts +++ b/packages/utils/test/unit/assert.test.ts @@ -1,23 +1,22 @@ -import "../setup.js"; -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {assert} from "../../src/index.js"; describe("assert", () => { describe("true", () => { it("Should not throw with true", () => { - expect(() => assert.true(true)).to.not.throw(); + expect(() => assert.true(true)).not.toThrow(); }); it("Should throw with false", () => { - expect(() => assert.true(false, "something must be valid")).to.throw("something must be valid"); + expect(() => assert.true(false, "something must be valid")).toThrow("something must be valid"); }); }); describe("equal with custom message", () => { it("Should not throw with equal values", () => { - expect(() => assert.equal(1, 1)).to.not.throw(); + expect(() => assert.equal(1, 1)).not.toThrow(); }); it("Should throw with different values", () => { - expect(() => assert.equal(1, 2, "something must be equal")).to.throw("something must be equal: 1 === 2"); + expect(() => assert.equal(1, 2, "something must be equal")).toThrow("something must be equal: 1 === 2"); }); }); @@ -51,9 +50,9 @@ describe("assert", () => { for (const {op, args, ok} of cases) { it(`assert ${args[0]} ${op} ${args[1]} = ${ok}`, () => { if (ok) { - expect(() => assert[op](...args)).to.not.throw(); + expect(() => assert[op](...args)).not.toThrow(); } else { - expect(() => assert[op](...args)).to.throw(); + expect(() => assert[op](...args)).toThrow(); } }); } diff --git a/packages/utils/test/unit/base64.test.ts b/packages/utils/test/unit/base64.test.ts index 38ccd77bafe8..7c68e84f4c3e 100644 --- a/packages/utils/test/unit/base64.test.ts +++ b/packages/utils/test/unit/base64.test.ts @@ -1,15 +1,14 @@ -import "../setup.js"; -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {toBase64, fromBase64} from "../../src/index.js"; describe("toBase64", () => { it("should encode UTF-8 string as base64 string", () => { - expect(toBase64("user:password")).to.be.equal("dXNlcjpwYXNzd29yZA=="); + expect(toBase64("user:password")).toBe("dXNlcjpwYXNzd29yZA=="); }); }); describe("fromBase64", () => { it("should decode UTF-8 string from base64 string", () => { - expect(fromBase64("dXNlcjpwYXNzd29yZA==")).to.be.equal("user:password"); + expect(fromBase64("dXNlcjpwYXNzd29yZA==")).toBe("user:password"); }); }); diff --git a/packages/utils/test/unit/bytes.test.ts b/packages/utils/test/unit/bytes.test.ts index f47e4c7ac3ed..8410e667187a 100644 --- a/packages/utils/test/unit/bytes.test.ts +++ b/packages/utils/test/unit/bytes.test.ts @@ -1,5 +1,4 @@ -import "../setup.js"; -import {assert, expect} from "chai"; +import {describe, it, expect} from "vitest"; import {intToBytes, bytesToInt, toHex, fromHex, toHexString} from "../../src/index.js"; describe("intToBytes", () => { @@ -27,7 +26,7 @@ describe("intToBytes", () => { const type = typeof input; const length = input[1]; it(`should correctly serialize ${type} to bytes length ${length}`, () => { - assert(intToBytes(input[0], input[1]).equals(output)); + expect(intToBytes(input[0], input[1])).toEqual(output); }); } }); @@ -43,7 +42,7 @@ describe("bytesToInt", () => { ]; for (const {input, output} of testCases) { it(`should produce ${output}`, () => { - expect(bytesToInt(input)).to.be.equal(output); + expect(bytesToInt(input)).toBe(output); }); } }); @@ -57,7 +56,7 @@ describe("toHex", () => { ]; for (const {input, output} of testCases) { it(`should convert Uint8Array to hex string ${output}`, () => { - expect(toHex(input)).to.be.equal(output); + expect(toHex(input)).toBe(output); }); } }); @@ -77,7 +76,7 @@ describe("fromHex", () => { for (const {input, output} of testCases) { it(`should convert hex string ${input} to Uint8Array`, () => { - expect(fromHex(input)).to.deep.equal(output); + expect(fromHex(input)).toEqual(output); }); } }); @@ -94,7 +93,7 @@ describe("toHexString", () => { for (const {input, output} of testCases) { it(`should convert Uint8Array to hex string ${output}`, () => { - expect(toHexString(input)).to.be.equal(output); + expect(toHexString(input)).toBe(output); }); } }); diff --git a/packages/utils/test/unit/err.test.ts b/packages/utils/test/unit/err.test.ts index 81bfd505ffc0..a4b30ee65d73 100644 --- a/packages/utils/test/unit/err.test.ts +++ b/packages/utils/test/unit/err.test.ts @@ -1,4 +1,4 @@ -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {Err, isErr, mapOkResults, mapOkResultsAsync, Result} from "../../src/err.js"; import {expectDeepEquals, expectEquals} from "../utils/chai.js"; @@ -46,7 +46,7 @@ describe("Result Err", () => { }); it("throw for different length", () => { - expect(() => mapOkResults([], () => [0])).to.throw(); + expect(() => mapOkResults([], () => [0])).toThrow(); }); it("num to string mixed results", () => { diff --git a/packages/utils/test/unit/math.test.ts b/packages/utils/test/unit/math.test.ts index 526f98ac1f1a..6827fea2bbb0 100644 --- a/packages/utils/test/unit/math.test.ts +++ b/packages/utils/test/unit/math.test.ts @@ -1,5 +1,4 @@ -import "../setup.js"; -import {assert} from "chai"; +import {describe, it, expect} from "vitest"; import {bigIntMin, bigIntMax, intDiv, intSqrt, bigIntSqrt} from "../../src/index.js"; describe("util/maths", function () { @@ -8,13 +7,13 @@ describe("util/maths", function () { const a = BigInt(1); const b = BigInt(2); const result = bigIntMin(a, b); - assert.equal(result, a, "Should have returned a!"); + expect(result).toBe(a); }); it("if b is lt should return b", () => { const a = BigInt(3); const b = BigInt(2); const result = bigIntMin(a, b); - assert.equal(result, b, "Should have returned b!"); + expect(result).toBe(b); }); }); @@ -23,78 +22,78 @@ describe("util/maths", function () { const a = BigInt(2); const b = BigInt(1); const result = bigIntMax(a, b); - assert.equal(result, a, "Should have returned a!"); + expect(result).toBe(a); }); it("if b is gt should return b", () => { const a = BigInt(2); const b = BigInt(3); const result = bigIntMax(a, b); - assert.equal(result, b, "Should have returned b!"); + expect(result).toBe(b); }); }); describe("intDiv", () => { it("should divide whole number", () => { const result = intDiv(6, 3); - assert.equal(result, 2, "Should have returned 2!"); + expect(result).toBe(2); }); it("should round less division", () => { const result = intDiv(9, 8); - assert.equal(result, 1, "Should have returned 1!"); + expect(result).toBe(1); }); }); describe("intSqrt", () => { it("0 should return 0", () => { const result = intSqrt(0); - assert.equal(result, 0, "Should have returned 0!"); + expect(result).toBe(0); }); it("1 should return 1", () => { const result = intSqrt(1); - assert.equal(result, 1, "Should have returned 1!"); + expect(result).toBe(1); }); it("3 should return 1", () => { const result = intSqrt(3); - assert.equal(result, 1, "Should have returned 1!"); + expect(result).toBe(1); }); it("4 should return 2", () => { const result = intSqrt(4); - assert.equal(result, 2, "Should have returned 2!"); + expect(result).toBe(2); }); it("16 should return 4", () => { const result = intSqrt(16); - assert.equal(result, 4, "Should have returned 4!"); + expect(result).toBe(4); }); it("31 should return 5", () => { const result = intSqrt(31); - assert.equal(result, 5, "Should have returned 5!"); + expect(result).toBe(5); }); }); describe("bigIntSqrt", () => { it("0 should return 0", () => { const result = bigIntSqrt(BigInt(0)); - assert.equal(result.toString(), BigInt(0).toString(), "Should have returned 0!"); + expect(result.toString()).toBe(BigInt(0).toString()); }); it("1 should return 1", () => { const result = bigIntSqrt(BigInt(1)); - assert.equal(result.toString(), BigInt(1).toString(), "Should have returned 1!"); + expect(result.toString()).toBe(BigInt(1).toString()); }); it("3 should return 1", () => { const result = bigIntSqrt(BigInt(3)); - assert.equal(result.toString(), BigInt(1).toString(), "Should have returned 1!"); + expect(result.toString()).toBe(BigInt(1).toString()); }); it("4 should return 2", () => { const result = bigIntSqrt(BigInt(4)); - assert.equal(result.toString(), BigInt(2).toString(), "Should have returned 2!"); + expect(result.toString()).toBe(BigInt(2).toString()); }); it("16 should return 4", () => { const result = bigIntSqrt(BigInt(16)); - assert.equal(result.toString(), BigInt(4).toString(), "Should have returned 4!"); + expect(result.toString()).toBe(BigInt(4).toString()); }); it("31 should return 5", () => { const result = bigIntSqrt(BigInt(31)); - assert.equal(result.toString(), BigInt(5).toString(), "Should have returned 5!"); + expect(result.toString()).toBe(BigInt(5).toString()); }); }); }); diff --git a/packages/utils/test/unit/objects.test.ts b/packages/utils/test/unit/objects.test.ts index ebad6c3f447c..4699a8c6f405 100644 --- a/packages/utils/test/unit/objects.test.ts +++ b/packages/utils/test/unit/objects.test.ts @@ -1,20 +1,19 @@ -import "../setup.js"; -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {isPlainObject, objectToExpectedCase} from "../../src/index.js"; describe("Objects helper", () => { it("should be plain object", () => { - expect(isPlainObject(Object.create({}))).to.equal(true); - expect(isPlainObject(Object.create(Object.create(Object.prototype)))).to.equal(true); - expect(isPlainObject({foo: "bar"})).to.equal(true); - expect(isPlainObject({})).to.equal(true); + expect(isPlainObject(Object.create({}))).toBe(true); + expect(isPlainObject(Object.create(Object.create(Object.prototype)))).toBe(true); + expect(isPlainObject({foo: "bar"})).toBe(true); + expect(isPlainObject({})).toBe(true); }); it("should not be plain object", () => { - expect(isPlainObject(1)).to.equal(false); - expect(isPlainObject(["foo", "bar"])).to.equal(false); - expect(isPlainObject([])).to.equal(false); - expect(isPlainObject(null)).to.equal(false); + expect(isPlainObject(1)).toBe(false); + expect(isPlainObject(["foo", "bar"])).toBe(false); + expect(isPlainObject([])).toBe(false); + expect(isPlainObject(null)).toBe(false); }); }); @@ -54,11 +53,11 @@ describe("objectToExpectedCase", () => { for (const {id, snake, camel} of testCases) { describe(id, () => { it("snake > camel", () => { - expect(objectToExpectedCase(snake, "camel")).to.deep.equal(camel); + expect(objectToExpectedCase(snake, "camel")).toEqual(camel); }); it("camel > snake", () => { - expect(objectToExpectedCase(camel, "snake")).to.deep.equal(snake); + expect(objectToExpectedCase(camel, "snake")).toEqual(snake); }); }); } diff --git a/packages/utils/test/unit/promise.node.test.ts b/packages/utils/test/unit/promise.node.test.ts new file mode 100644 index 000000000000..c9f6a3c2f98d --- /dev/null +++ b/packages/utils/test/unit/promise.node.test.ts @@ -0,0 +1,35 @@ +import {describe, it, expect, vi, beforeEach, afterEach} from "vitest"; +import {callFnWhenAwait} from "../../src/promise.js"; + +// TODO: Need to debug why vi.useFakeTimers() is not working for the browsers +describe("callFnWhenAwait util", function () { + beforeEach(() => { + vi.useFakeTimers(); + }); + + afterEach(() => { + vi.clearAllTimers(); + }); + + it("should call function while awaing for promise", async () => { + const p = new Promise((resolve) => setTimeout(() => resolve("done"), 5 * 1000)); + const stub = vi.fn(); + const result = await Promise.all([callFnWhenAwait(p, stub, 2 * 1000), vi.advanceTimersByTimeAsync(5000)]); + expect(result[0]).toBe("done"); + expect(stub).toHaveBeenCalledTimes(2); + await vi.advanceTimersByTimeAsync(5000); + expect(stub).toHaveBeenCalledTimes(2); + }); + + it("should throw error", async () => { + const stub = vi.fn(); + const p = new Promise((_, reject) => setTimeout(() => reject(new Error("done")), 5 * 1000)); + try { + await Promise.all([callFnWhenAwait(p, stub, 2 * 1000), vi.advanceTimersByTimeAsync(5000)]); + expect.fail("should throw error here"); + } catch (e) { + expect((e as Error).message).toBe("done"); + expect(stub).toHaveBeenCalledTimes(2); + } + }); +}); diff --git a/packages/utils/test/unit/promise.test.ts b/packages/utils/test/unit/promise.test.ts deleted file mode 100644 index dec5dc370a2b..000000000000 --- a/packages/utils/test/unit/promise.test.ts +++ /dev/null @@ -1,37 +0,0 @@ -import "../setup.js"; -import {expect} from "chai"; -import sinon from "sinon"; -import {callFnWhenAwait} from "../../src/promise.js"; - -describe("callFnWhenAwait util", function () { - const sandbox = sinon.createSandbox(); - beforeEach(() => { - sandbox.useFakeTimers(); - }); - - afterEach(() => { - sandbox.restore(); - }); - - it("should call function while awaing for promise", async () => { - const p = new Promise((resolve) => setTimeout(() => resolve("done"), 5 * 1000)); - const stub = sandbox.stub(); - const result = await Promise.all([callFnWhenAwait(p, stub, 2 * 1000), sandbox.clock.tickAsync(5000)]); - expect(result[0]).to.be.equal("done"); - expect(stub).to.be.calledTwice; - await sandbox.clock.tickAsync(5000); - expect(stub).to.be.calledTwice; - }); - - it("should throw error", async () => { - const stub = sandbox.stub(); - const p = new Promise((_, reject) => setTimeout(() => reject(new Error("done")), 5 * 1000)); - try { - await Promise.all([callFnWhenAwait(p, stub, 2 * 1000), sandbox.clock.tickAsync(5000)]); - expect.fail("should throw error here"); - } catch (e) { - expect((e as Error).message).to.be.equal("done"); - expect(stub).to.be.calledTwice; - } - }); -}); diff --git a/packages/utils/test/unit/promiserace.test.ts b/packages/utils/test/unit/promiserace.test.ts index 25952f828920..5d0567553522 100644 --- a/packages/utils/test/unit/promiserace.test.ts +++ b/packages/utils/test/unit/promiserace.test.ts @@ -1,4 +1,4 @@ -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {racePromisesWithCutoff, RaceEvent} from "../../src/promise.js"; describe("racePromisesWithCutoff", () => { @@ -98,7 +98,7 @@ describe("racePromisesWithCutoff", () => { testEvents.push(event) ); const testResultsCmp = testResults.map((res: string | Error) => (res instanceof Error ? res.message : res)); - expect({results: testResultsCmp, events: testEvents}).to.be.deep.equal({results, events}); + expect({results: testResultsCmp, events: testEvents}).toEqual({results, events}); }); } }); diff --git a/packages/utils/test/unit/retry.test.ts b/packages/utils/test/unit/retry.test.ts index b5211c7e106b..12afb7597015 100644 --- a/packages/utils/test/unit/retry.test.ts +++ b/packages/utils/test/unit/retry.test.ts @@ -1,5 +1,4 @@ -import "../setup.js"; -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {retry, RetryOptions} from "../../src/retry.js"; describe("retry", () => { @@ -39,9 +38,9 @@ describe("retry", () => { for (const {id, fn, opts, result} of testCases) { it(id, async () => { if (result instanceof Error) { - await expect(retry(fn, opts)).to.be.rejectedWith(result); + await expect(retry(fn, opts)).rejects.toThrow(result); } else { - expect(await retry(fn, opts)).to.deep.equal(result); + expect(await retry(fn, opts)).toEqual(result); } }); } diff --git a/packages/utils/test/unit/sleep.test.ts b/packages/utils/test/unit/sleep.test.ts index 44f7d309412a..a887560836eb 100644 --- a/packages/utils/test/unit/sleep.test.ts +++ b/packages/utils/test/unit/sleep.test.ts @@ -1,5 +1,4 @@ -import "../setup.js"; -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {sleep} from "../../src/sleep.js"; import {ErrorAborted} from "../../src/errors.js"; @@ -13,20 +12,19 @@ describe("sleep", function () { const controller = new AbortController(); setTimeout(() => controller.abort(), 10); - // Sleep for longer than the current test timeout. - // If the abort signal doesn't work mocha will throw a timeout error - const sleepTime = 2 * this.timeout(); + const sleepTime = 5000; - await expect(sleep(sleepTime, controller.signal)).to.rejectedWith(ErrorAborted); + await expect(sleep(sleepTime, controller.signal)).rejects.toThrow(ErrorAborted); }); it("Should abort timeout with already aborted signal", async function () { const controller = new AbortController(); controller.abort(); - expect(controller.signal.aborted, "Signal should already be aborted").to.equal(true); + // "Signal should already be aborted" + expect(controller.signal.aborted).toBe(true); - await expect(sleep(0, controller.signal)).to.rejectedWith(ErrorAborted); + await expect(sleep(0, controller.signal)).rejects.toThrow(ErrorAborted); }); it("sleep 0 must tick the event loop", async () => { @@ -51,16 +49,13 @@ describe("sleep", function () { await new Promise((r) => setTimeout(r, 0)); } - expect(steps).to.deep.equal( - [ - // Sync execution - Step.beforeSleep, - // Next tick, first registered callback - Step.setTimeout0, - // Next tick, second registered callback - Step.afterSleep, - ], - "Wrong steps" - ); + expect(steps).toEqual([ + // Sync execution + Step.beforeSleep, + // Next tick, first registered callback + Step.setTimeout0, + // Next tick, second registered callback + Step.afterSleep, + ]); }); }); diff --git a/packages/utils/test/unit/timeout.test.ts b/packages/utils/test/unit/timeout.test.ts index a2b430e86855..b8844355effb 100644 --- a/packages/utils/test/unit/timeout.test.ts +++ b/packages/utils/test/unit/timeout.test.ts @@ -1,14 +1,11 @@ -import "../setup.js"; -import {expect} from "chai"; +import {describe, it, expect, afterEach} from "vitest"; import {withTimeout} from "../../src/timeout.js"; import {ErrorAborted, TimeoutError} from "../../src/errors.js"; describe("withTimeout", function () { const data = "DATA"; const shortTimeoutMs = 10; - // Sleep for longer than the current test timeout. - // If the abort signal doesn't work mocha will throw a timeout error - const longTimeoutMs = 2 * this.timeout(); + const longTimeoutMs = 5000; const pendingTimeouts: NodeJS.Timeout[] = []; @@ -32,33 +29,33 @@ describe("withTimeout", function () { it("Should resolve timeout", async function () { const res = await withTimeout(() => pause(shortTimeoutMs, data), longTimeoutMs); - expect(res).to.equal(data); + expect(res).toBe(data); }); it("Should resolve timeout with not triggered signal", async function () { const controller = new AbortController(); const res = await withTimeout(() => pause(shortTimeoutMs, data), longTimeoutMs, controller.signal); - expect(res).to.equal(data); + expect(res).toBe(data); }); it("Should abort timeout with triggered signal", async function () { const controller = new AbortController(); setTimeout(() => controller.abort(), shortTimeoutMs); - await expect(withTimeout(() => pause(longTimeoutMs, data), longTimeoutMs, controller.signal)).to.rejectedWith( + await expect(withTimeout(() => pause(longTimeoutMs, data), longTimeoutMs, controller.signal)).rejects.toThrow( ErrorAborted ); }); it("Should timeout with no signal", async function () { - await expect(withTimeout(() => pause(longTimeoutMs, data), shortTimeoutMs)).to.rejectedWith(TimeoutError); + await expect(withTimeout(() => pause(longTimeoutMs, data), shortTimeoutMs)).rejects.toThrow(TimeoutError); }); it("Should timeout with not triggered signal", async function () { const controller = new AbortController(); - await expect(withTimeout(() => pause(longTimeoutMs, data), shortTimeoutMs, controller.signal)).to.rejectedWith( + await expect(withTimeout(() => pause(longTimeoutMs, data), shortTimeoutMs, controller.signal)).rejects.toThrow( TimeoutError ); }); @@ -67,9 +64,10 @@ describe("withTimeout", function () { const controller = new AbortController(); controller.abort(); - expect(controller.signal.aborted, "Signal should already be aborted").to.equal(true); + // "Signal should already be aborted" + expect(controller.signal.aborted).toBe(true); - await expect(withTimeout(() => pause(shortTimeoutMs, data), shortTimeoutMs, controller.signal)).to.rejectedWith( + await expect(withTimeout(() => pause(shortTimeoutMs, data), shortTimeoutMs, controller.signal)).rejects.toThrow( ErrorAborted ); }); diff --git a/packages/utils/test/unit/waitFor.test.ts b/packages/utils/test/unit/waitFor.test.ts index d659be3d4bcb..293e5aba936a 100644 --- a/packages/utils/test/unit/waitFor.test.ts +++ b/packages/utils/test/unit/waitFor.test.ts @@ -1,5 +1,4 @@ -import "../setup.js"; -import {expect} from "chai"; +import {describe, it, expect} from "vitest"; import {waitFor, createElapsedTimeTracker} from "../../src/waitFor.js"; import {ErrorAborted, TimeoutError} from "../../src/errors.js"; import {sleep} from "../../src/sleep.js"; @@ -9,7 +8,7 @@ describe("waitFor", () => { const timeout = 20; it("Should resolve if condition is already true", async () => { - await expect(waitFor(() => true, {interval, timeout})).to.be.fulfilled; + await expect(waitFor(() => true, {interval, timeout})).resolves.toBeUndefined(); }); it("Should resolve if condition becomes true within timeout", async () => { @@ -21,19 +20,19 @@ describe("waitFor", () => { }); it("Should reject with TimeoutError if condition does not become true within timeout", async () => { - await expect(waitFor(() => false, {interval, timeout})).to.be.rejectedWith(TimeoutError); + await expect(waitFor(() => false, {interval, timeout})).rejects.toThrow(TimeoutError); }); it("Should reject with ErrorAborted if aborted before condition becomes true", async () => { const controller = new AbortController(); setTimeout(() => controller.abort(), interval); - await expect(waitFor(() => false, {interval, timeout, signal: controller.signal})).to.be.rejectedWith(ErrorAborted); + await expect(waitFor(() => false, {interval, timeout, signal: controller.signal})).rejects.toThrow(ErrorAborted); }); it("Should reject with ErrorAborted if signal is already aborted", async () => { const controller = new AbortController(); controller.abort(); - await expect(waitFor(() => true, {interval, timeout, signal: controller.signal})).to.be.rejectedWith(ErrorAborted); + await expect(waitFor(() => true, {interval, timeout, signal: controller.signal})).rejects.toThrow(ErrorAborted); }); }); @@ -41,7 +40,7 @@ describe("waitForElapsedTime", () => { it("should true for the first time", () => { const callIfTimePassed = createElapsedTimeTracker({minElapsedTime: 1000}); - expect(callIfTimePassed()).to.be.true; + expect(callIfTimePassed()).toBe(true); }); it("should return true after the minElapsedTime has passed", async () => { @@ -50,7 +49,7 @@ describe("waitForElapsedTime", () => { await sleep(150); - expect(callIfTimePassed()).to.be.true; + expect(callIfTimePassed()).toBe(true); }); it("should return false before the minElapsedTime has passed", async () => { @@ -59,6 +58,6 @@ describe("waitForElapsedTime", () => { await sleep(10); - expect(callIfTimePassed()).to.be.false; + expect(callIfTimePassed()).toBe(false); }); }); diff --git a/packages/utils/tsconfig.e2e.json b/packages/utils/tsconfig.e2e.json deleted file mode 100644 index cedf626f4124..000000000000 --- a/packages/utils/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/utils/vitest.browser.config.ts b/packages/utils/vitest.browser.config.ts new file mode 100644 index 000000000000..3c4b48885a33 --- /dev/null +++ b/packages/utils/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/utils/vitest.config.ts b/packages/utils/vitest.config.ts new file mode 100644 index 000000000000..1df0de848936 --- /dev/null +++ b/packages/utils/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/utils/webpack.test.config.cjs b/packages/utils/webpack.test.config.cjs deleted file mode 100644 index 711c6ac891a7..000000000000 --- a/packages/utils/webpack.test.config.cjs +++ /dev/null @@ -1,5 +0,0 @@ -const webpackConfig = require("../../webpack.test.config.js"); - -module.exports = { - ...webpackConfig, -};