Skip to content

Commit

Permalink
Migrate utils tests to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Dec 21, 2023
1 parent 2530fae commit 8d06345
Show file tree
Hide file tree
Showing 22 changed files with 153 additions and 168 deletions.
3 changes: 0 additions & 3 deletions packages/utils/.mocharc.yml

This file was deleted.

9 changes: 0 additions & 9 deletions packages/utils/karma.config.cjs

This file was deleted.

8 changes: 6 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/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> {}
6 changes: 0 additions & 6 deletions packages/utils/test/setup.ts

This file was deleted.

15 changes: 7 additions & 8 deletions packages/utils/test/unit/assert.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});

Expand Down Expand Up @@ -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();
}
});
}
Expand Down
7 changes: 3 additions & 4 deletions packages/utils/test/unit/base64.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
13 changes: 6 additions & 7 deletions packages/utils/test/unit/bytes.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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);
});
}
});
Expand All @@ -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);
});
}
});
Expand All @@ -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);
});
}
});
Expand All @@ -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);
});
}
});
Expand All @@ -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);
});
}
});
4 changes: 2 additions & 2 deletions packages/utils/test/unit/err.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 {Err, isErr, mapOkResults, mapOkResultsAsync, Result} from "../../src/err.js";
import {expectDeepEquals, expectEquals} from "../utils/chai.js";

Expand Down Expand Up @@ -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", () => {
Expand Down
39 changes: 19 additions & 20 deletions packages/utils/test/unit/math.test.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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);
});
});

Expand All @@ -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());
});
});
});
23 changes: 11 additions & 12 deletions packages/utils/test/unit/objects.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

Expand Down Expand Up @@ -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);
});
});
}
Expand Down
35 changes: 35 additions & 0 deletions packages/utils/test/unit/promise.node.test.ts
Original file line number Diff line number Diff line change
@@ -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<string>((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<string>((_, 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);
}
});
});
Loading

0 comments on commit 8d06345

Please sign in to comment.