build: add jest, tests

This commit is contained in:
2024-03-12 21:23:19 +02:00
parent fdb28fa612
commit ea3cb13c85
19 changed files with 2173 additions and 13 deletions

1
.eslintignore Normal file
View File

@@ -0,0 +1 @@
tests

View File

@@ -1,3 +1,4 @@
/** @type {import("@typescript-eslint/utils/dist/ts-eslint/Linter").Linter.Config} c */
module.exports = {
root: true,
parser: "@typescript-eslint/parser",

3
.gitignore vendored
View File

@@ -4,3 +4,6 @@ dist
# Packages
node_modules
# Tests
coverage

View File

@@ -45,7 +45,7 @@ console.log(capitalizeFirstChar("lorem ipsum"))
#### Defined in
[functions/capitalizeFirstChar.ts:32](https://git.resultium.net/public/utils/src/commit/ec41aeb/src/functions/capitalizeFirstChar.ts#L32)
[functions/capitalizeFirstChar.ts:32](https://git.resultium.net/public/utils/src/commit/fdb28fa/src/functions/capitalizeFirstChar.ts#L32)
___
@@ -78,7 +78,7 @@ console.log(classNames("bg-blue-300", isVisible ? "block" : "hidden"))
#### Defined in
[functions/classNames.ts:34](https://git.resultium.net/public/utils/src/commit/ec41aeb/src/functions/classNames.ts#L34)
[functions/classNames.ts:34](https://git.resultium.net/public/utils/src/commit/fdb28fa/src/functions/classNames.ts#L34)
___
@@ -112,7 +112,7 @@ console.log(conditionalJoin(["Hello,", isAdmin ? "Administrator" : "User", "!"],
#### Defined in
[functions/conditionalJoin.ts:35](https://git.resultium.net/public/utils/src/commit/ec41aeb/src/functions/conditionalJoin.ts#L35)
[functions/conditionalJoin.ts:35](https://git.resultium.net/public/utils/src/commit/fdb28fa/src/functions/conditionalJoin.ts#L35)
___
@@ -160,7 +160,7 @@ console.log(
#### Defined in
[functions/isDeepWeakEqual.ts:47](https://git.resultium.net/public/utils/src/commit/ec41aeb/src/functions/isDeepWeakEqual.ts#L47)
[functions/isDeepWeakEqual.ts:47](https://git.resultium.net/public/utils/src/commit/fdb28fa/src/functions/isDeepWeakEqual.ts#L47)
___
@@ -197,7 +197,7 @@ console.log(randomInRange(1, 100))
#### Defined in
[functions/randomInRange.ts:37](https://git.resultium.net/public/utils/src/commit/ec41aeb/src/functions/randomInRange.ts#L37)
[functions/randomInRange.ts:37](https://git.resultium.net/public/utils/src/commit/fdb28fa/src/functions/randomInRange.ts#L37)
___
@@ -243,7 +243,7 @@ console.log(removeFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" }, {
#### Defined in
[functions/removeFromArrayByKeyValue.ts:40](https://git.resultium.net/public/utils/src/commit/ec41aeb/src/functions/removeFromArrayByKeyValue.ts#L40)
[functions/removeFromArrayByKeyValue.ts:40](https://git.resultium.net/public/utils/src/commit/fdb28fa/src/functions/removeFromArrayByKeyValue.ts#L40)
___
@@ -282,7 +282,7 @@ console.log(removeObjectProperty({ a: "1", b: "2", c: "3" }, "c"))
#### Defined in
[functions/removeObjectProperty.ts:33](https://git.resultium.net/public/utils/src/commit/ec41aeb/src/functions/removeObjectProperty.ts#L33)
[functions/removeObjectProperty.ts:33](https://git.resultium.net/public/utils/src/commit/fdb28fa/src/functions/removeObjectProperty.ts#L33)
___
@@ -330,7 +330,7 @@ console.log(replaceFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" },
#### Defined in
[functions/replaceFromArrayByKeyValue.ts:42](https://git.resultium.net/public/utils/src/commit/ec41aeb/src/functions/replaceFromArrayByKeyValue.ts#L42)
[functions/replaceFromArrayByKeyValue.ts:42](https://git.resultium.net/public/utils/src/commit/fdb28fa/src/functions/replaceFromArrayByKeyValue.ts#L42)
___
@@ -368,4 +368,4 @@ console.log(splitIntoChunks(["a", "b", "c", "d", "e", "f"], 3));
#### Defined in
[functions/splitIntoChuks.ts:33](https://git.resultium.net/public/utils/src/commit/ec41aeb/src/functions/splitIntoChuks.ts#L33)
[functions/splitIntoChuks.ts:33](https://git.resultium.net/public/utils/src/commit/fdb28fa/src/functions/splitIntoChuks.ts#L33)

6
jest.config.cjs Normal file
View File

@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};

View File

@@ -12,8 +12,10 @@
"build": "pnpm run build:package && pnpm run build:docs",
"build:package": "rm -rf dist && tsc --module ESNext --outdir dist/esm && tsc --module commonjs --outdir dist/cjs",
"build:docs": "typedoc --includeVersion --sourceLinkTemplate 'https://git.resultium.net/public/utils/src/commit/{gitRevision}/{path}#L{line}' --plugin typedoc-plugin-markdown src",
"prepublish": "pnpm run build",
"lint": "eslint src"
"prepublish": "pnpm run test && pnpm run build",
"lint": "eslint src",
"test": "jest",
"test:coverage": "jest --coverage"
},
"repository": {
"type": "git",
@@ -29,6 +31,7 @@
},
"license": "GPL-3.0-or-later",
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/node": "^20.11.25",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
@@ -36,7 +39,9 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-security": "^2.1.1",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"typedoc": "^0.25.12",
"typedoc-plugin-markdown": "^3.17.1",
"typescript": "^5.4.2"

2008
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -25,7 +25,7 @@
* let isAdmin = true
*
* // prints "Hello, Administrator!"
* console.log(conditionalJoin(["Hello,", isAdmin ? "Administrator" : "User", "!"], " "))
* console.log(conditionalJoin(["Hello,", isAdmin ? "Administrator!" : "User!"], " "))
* ```
*
* @param array - strings to join

View File

@@ -25,4 +25,4 @@ export * from "./functions/randomInRange";
export * from "./functions/removeFromArrayByKeyValue";
export * from "./functions/removeObjectProperty";
export * from "./functions/replaceFromArrayByKeyValue";
export * from "./functions/splitIntoChuks";
export * from "./functions/splitIntoChunks";

View File

@@ -0,0 +1,6 @@
import { capitalizeFirstChar } from "../src";
import { expect, test } from "@jest/globals";
test("capitalizes first character of the string", () => {
expect(capitalizeFirstChar("lorem ipsum")).toBe("Lorem ipsum");
});

10
tests/classNames.test.ts Normal file
View File

@@ -0,0 +1,10 @@
import { expect, test } from "@jest/globals";
import { classNames } from "../src";
test("joins html class strings", () => {
let isVisible = false;
expect(classNames("bg-blue-300", isVisible ? "block" : "hidden")).toBe(
"bg-blue-300 hidden",
);
});

View File

@@ -0,0 +1,10 @@
import { expect, test } from "@jest/globals";
import { conditionalJoin } from "../src";
test("conditionally joins strings", () => {
let isAdmin = true;
expect(
conditionalJoin(["Hello,", isAdmin ? "Administrator!" : "User!"], " "),
).toBe("Hello, Administrator!");
});

View File

@@ -0,0 +1,15 @@
import { isDeepWeakEqual } from "../src";
import { expect, test } from "@jest/globals";
test("weakly compares objects", () => {
expect(isDeepWeakEqual({ a: 1 }, ["lorem"])).toBe(false);
expect(isDeepWeakEqual({ a: 1 }, { a: 1 })).toBe(true);
expect(
isDeepWeakEqual(
{ date: "2024-03-12T18:19:50.548Z" },
{ date: new Date(1710267590548) },
),
).toBe(true);
});

View File

@@ -0,0 +1,7 @@
import { expect, test } from "@jest/globals";
import { randomInRange } from "../src";
test("generates a random number in range", () => {
const randomNumber = randomInRange(1, 100);
expect(randomNumber >= 1 && randomNumber <= 100).toBeTruthy();
});

View File

@@ -0,0 +1,29 @@
import { expect, test } from "@jest/globals";
import { removeFromArrayByKeyValue } from "../src";
test("removes an object from an array by key-value object", () => {
expect(
removeFromArrayByKeyValue(
[
{ a: "1", b: "0" },
{ a: "2", b: "0" },
{ a: "3", b: "0" },
],
{ a: "3" },
),
).toMatchObject([
{ a: "1", b: "0" },
{ a: "2", b: "0" },
]);
expect(
removeFromArrayByKeyValue(
[
{ a: "1", b: "0" },
{ a: "2", b: "0" },
{ a: "3", b: "0" },
],
{ b: "0" },
),
).toMatchObject([]);
});

View File

@@ -0,0 +1,9 @@
import { expect, test } from "@jest/globals";
import { removeObjectProperty } from "../src";
test("remove object property by provided key", () => {
expect(removeObjectProperty({ a: "1", b: "2", c: "3" }, "c")).toMatchObject({
a: "1",
b: "2",
});
});

View File

@@ -0,0 +1,36 @@
import { expect, test } from "@jest/globals";
import { replaceFromArrayByKeyValue } from "../src";
test("replace from an array by key-value", () => {
expect(
replaceFromArrayByKeyValue(
[
{ a: "1", b: "0" },
{ a: "2", b: "0" },
{ a: "3", b: "0" },
],
{ a: "3" },
{ b: "lorem" },
),
).toMatchObject([
{ a: "1", b: "0" },
{ a: "2", b: "0" },
{ a: "3", b: "lorem" },
]);
expect(
replaceFromArrayByKeyValue(
[
{ a: "1", b: "0" },
{ a: "2", b: "0" },
{ a: "3", b: "0" },
],
{ b: "0" },
{ a: "lorem" },
),
).toMatchObject([
{ a: "lorem", b: "0" },
{ a: "2", b: "0" },
{ a: "3", b: "0" },
]);
});

View File

@@ -0,0 +1,14 @@
import { expect, test } from "@jest/globals";
import { splitIntoChunks } from "../src";
test("splits an array into chunks", () => {
// JSON.stringify is being used because there is no way to compare nested arrays in jest
expect(
JSON.stringify(splitIntoChunks(["a", "b", "c", "d", "e", "f"], 3)),
).toBe(
JSON.stringify([
["a", "b", "c"],
["d", "e", "f"],
]),
);
});