use node:test instead of jest

This commit is contained in:
Mylloon 2024-12-10 10:04:15 +01:00
parent 884d62ceed
commit 3a81b398bb
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
9 changed files with 674 additions and 3323 deletions

View file

@ -293,11 +293,14 @@ Il est souhaité d'écrire des tests quand cela est possible.
```ts ```ts
import { fnReturnsTrue } from "../src/utils/file"; import { fnReturnsTrue } from "../src/utils/file";
import { describe, it } from "node:test";
import assert from "node:assert/strict";
describe("test name", () => { describe("test name", () => {
{ {
const name = "to be tested"; const name = "to be tested";
test(name, () => { it(name, () => {
expect(fnReturnsTrue() /* function to test */).toBe(true /* expected result */); assert.strictEqual(fnReturnsTrue() /* function to test */, true /* expected result */);
}); });
} }
}); });

3727
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -10,7 +10,7 @@
"lint": "npx eslint src", "lint": "npx eslint src",
"format-check": "npx prettier --check src", "format-check": "npx prettier --check src",
"format-write": "npx prettier --write src", "format-write": "npx prettier --write src",
"test": "npx jest" "test": "node --import tsx --test --experimental-strip-types"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -31,16 +31,11 @@
"uuid": "^11.0.3" "uuid": "^11.0.3"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "~29.5.14",
"@typescript-eslint/eslint-plugin": "~8.17.0", "@typescript-eslint/eslint-plugin": "~8.17.0",
"@typescript-eslint/parser": "~8.17.0", "@typescript-eslint/parser": "~8.17.0",
"dotenv": "~16.4.7", "dotenv": "~16.4.7",
"jest": "~29.7.0",
"prettier-eslint": "~16.3.0", "prettier-eslint": "~16.3.0",
"ts-jest": "~29.2.5", "ts-node-dev": "~2.0.0",
"ts-node-dev": "~2.0.0" "tsx": "^4.19.2"
},
"jest": {
"preset": "ts-jest"
} }
} }

View file

@ -1,34 +1,37 @@
import "../../modules/string"; import "../../modules/string";
import { describe, it } from "node:test";
import assert from "node:assert/strict";
describe("Capitalize", () => { describe("Capitalize", () => {
{ {
const name = "test"; const name = "test";
test(name, () => { it(name, () => {
expect(name.capitalize()).toBe("Test"); assert.strictEqual(name.capitalize(), "Test");
}); });
} }
{ {
const name = "MACHIN"; const name = "MACHIN";
test(name, () => { it(name, () => {
expect(name.capitalize()).toBe("MACHIN"); assert.strictEqual(name.capitalize(), "MACHIN");
}); });
} }
{ {
const name = "tRUC"; const name = "tRUC";
test(name, () => { it(name, () => {
expect(name.capitalize()).toBe("TRUC"); assert.strictEqual(name.capitalize(), "TRUC");
}); });
} }
{ {
const name = "Super"; const name = "Super";
test(name, () => { it(name, () => {
expect(name.capitalize()).toBe("Super"); assert.strictEqual(name.capitalize(), "Super");
}); });
} }
{ {
const name = ""; const name = "";
test(name, () => { it(name, () => {
expect(name.capitalize()).toBe(""); assert.strictEqual(name.capitalize(), "");
}); });
} }
}); });

View file

@ -1,28 +1,31 @@
import { OptionReminder, splitTime } from "../../../utils/commands/reminder"; import { OptionReminder, splitTime } from "../../../utils/commands/reminder";
import { describe, it } from "node:test";
import assert from "node:assert/strict";
describe("Time splitter", () => { describe("Time splitter", () => {
{ {
const name = ""; const name = "";
test(name, () => { it(name, () => {
expect(splitTime(name)).toStrictEqual({ option: OptionReminder.Nothing, time: "" }); assert.deepStrictEqual(splitTime(name), { option: OptionReminder.Nothing, time: "" });
}); });
} }
{ {
const name = "2m@p"; const name = "2m@p";
test(name, () => { it(name, () => {
expect(splitTime(name)).toStrictEqual({ option: OptionReminder.DirectMessage, time: "2m" }); assert.deepStrictEqual(splitTime(name), { option: OptionReminder.DirectMessage, time: "2m" });
}); });
} }
{ {
const name = "41@"; const name = "41@";
test(name, () => { it(name, () => {
expect(splitTime(name)).toStrictEqual({ option: OptionReminder.Mention, time: "41" }); assert.deepStrictEqual(splitTime(name), { option: OptionReminder.Mention, time: "41" });
}); });
} }
{ {
const name = "0P"; const name = "0P";
test(name, () => { it(name, () => {
expect(splitTime(name)).toStrictEqual({ option: OptionReminder.DirectMessage, time: "0" }); assert.deepStrictEqual(splitTime(name), { option: OptionReminder.DirectMessage, time: "0" });
}); });
} }
}); });

View file

@ -1,6 +1,9 @@
import { Attachment, Collection, EmbedBuilder } from "discord.js"; import { Attachment, Collection, EmbedBuilder } from "discord.js";
import { handleAttachments } from "../../../utils/events/citation"; import { handleAttachments } from "../../../utils/events/citation";
import { describe, it } from "node:test";
import assert from "node:assert/strict";
/** /**
* Generate a new random string * Generate a new random string
* @returns random string * @returns random string
@ -17,7 +20,7 @@ describe("Attachements Handler", () => {
const max_field = Array.from({ length: max }, () => "[f](url)").join(", "); const max_field = Array.from({ length: max }, () => "[f](url)").join(", ");
{ {
const name = "One image"; const name = "One image";
test(name, () => { it(name, () => {
const embedExpected = new EmbedBuilder(); const embedExpected = new EmbedBuilder();
embedExpected.setImage("http://url"); embedExpected.setImage("http://url");
@ -28,12 +31,12 @@ describe("Attachements Handler", () => {
new Collection([[newKey(), { name: "image.png", url: "http://url" } as Attachment]]), new Collection([[newKey(), { name: "image.png", url: "http://url" } as Attachment]]),
); );
expect(embedTest).toStrictEqual(embedExpected); assert.deepStrictEqual(embedTest, embedExpected);
}); });
} }
{ {
const name = "Two images"; const name = "Two images";
test(name, () => { it(name, () => {
const embedExpected = new EmbedBuilder(); const embedExpected = new EmbedBuilder();
embedExpected.addFields({ embedExpected.addFields({
name: "yes_s", name: "yes_s",
@ -50,12 +53,12 @@ describe("Attachements Handler", () => {
]), ]),
); );
expect(embedTest).toStrictEqual(embedExpected); assert.deepStrictEqual(embedTest, embedExpected);
}); });
} }
{ {
const name = "One link"; const name = "One link";
test(name, () => { it(name, () => {
const embedExpected = new EmbedBuilder(); const embedExpected = new EmbedBuilder();
embedExpected.addFields({ name: "no_s", value: "[f](url)" }); embedExpected.addFields({ name: "no_s", value: "[f](url)" });
@ -66,12 +69,12 @@ describe("Attachements Handler", () => {
new Collection([[newKey(), { name: "f", url: "url" } as Attachment]]), new Collection([[newKey(), { name: "f", url: "url" } as Attachment]]),
); );
expect(embedTest).toStrictEqual(embedExpected); assert.deepStrictEqual(embedTest, embedExpected);
}); });
} }
{ {
const name = "Two files"; const name = "Two files";
test(name, () => { it(name, () => {
const embedExpected = new EmbedBuilder(); const embedExpected = new EmbedBuilder();
embedExpected.addFields({ name: "yes_s", value: "[f](url), [f](url)" }); embedExpected.addFields({ name: "yes_s", value: "[f](url), [f](url)" });
@ -85,12 +88,12 @@ describe("Attachements Handler", () => {
]), ]),
); );
expect(embedTest).toStrictEqual(embedExpected); assert.deepStrictEqual(embedTest, embedExpected);
}); });
} }
{ {
const name = "Two fields with multiples files each"; const name = "Two fields with multiples files each";
test(name, () => { it(name, () => {
const total = 150; const total = 150;
const embedExpected = new EmbedBuilder(); const embedExpected = new EmbedBuilder();
@ -114,12 +117,12 @@ describe("Attachements Handler", () => {
), ),
); );
expect(embedTest).toStrictEqual(embedExpected); assert.deepStrictEqual(embedTest, embedExpected);
}); });
} }
{ {
const name = "Two fields with one field with one element"; const name = "Two fields with one field with one element";
test(name, () => { it(name, () => {
const total = 103; const total = 103;
const embedExpected = new EmbedBuilder(); const embedExpected = new EmbedBuilder();
@ -143,7 +146,7 @@ describe("Attachements Handler", () => {
), ),
); );
expect(embedTest).toStrictEqual(embedExpected); assert.deepStrictEqual(embedTest, embedExpected);
}); });
} }
}); });

View file

@ -6,32 +6,32 @@ import {
splitFilenameExtensions, splitFilenameExtensions,
} from "../../utils/misc"; } from "../../utils/misc";
import { describe, it } from "node:test";
import assert from "node:assert/strict";
describe("Filename splitter", () => { describe("Filename splitter", () => {
{ {
const name = "test.js"; const name = "test.js";
test(name, () => { it(name, () => {
expect(splitFilenameExtensions(name)).toStrictEqual({ file: "test", ext: "js" }); assert.deepStrictEqual(splitFilenameExtensions(name), { file: "test", ext: "js" });
}); });
} }
{ {
const name = ".env"; const name = ".env";
test(name, () => { it(name, () => {
expect(splitFilenameExtensions(name)).toStrictEqual({ file: ".env", ext: undefined }); assert.deepStrictEqual(splitFilenameExtensions(name), { file: ".env", ext: undefined });
}); });
} }
{ {
const name = ".env.test"; const name = ".env.test";
test(name, () => { it(name, () => {
expect(splitFilenameExtensions(name)).toStrictEqual({ assert.deepStrictEqual(splitFilenameExtensions(name), { file: ".env", ext: "test" });
file: ".env",
ext: "test",
});
}); });
} }
{ {
const name = "file.test.js"; const name = "file.test.js";
test(name, () => { it(name, () => {
expect(splitFilenameExtensions(name)).toStrictEqual({ file: "file.test", ext: "js" }); assert.deepStrictEqual(splitFilenameExtensions(name), { file: "file.test", ext: "js" });
}); });
} }
}); });
@ -39,26 +39,26 @@ describe("Filename splitter", () => {
describe("Extension remover", () => { describe("Extension remover", () => {
{ {
const name = "test.js"; const name = "test.js";
test(name, () => { it(name, () => {
expect(removeExtension(name)).toBe("test"); assert.strictEqual(removeExtension(name), "test");
}); });
} }
{ {
const name = ".env"; const name = ".env";
test(name, () => { it(name, () => {
expect(removeExtension(name)).toBe(".env"); assert.strictEqual(removeExtension(name), ".env");
}); });
} }
{ {
const name = ".env.test"; const name = ".env.test";
test(name, () => { it(name, () => {
expect(removeExtension(name)).toBe(".env"); assert.strictEqual(removeExtension(name), ".env");
}); });
} }
{ {
const name = "file.test.js"; const name = "file.test.js";
test(name, () => { it(name, () => {
expect(removeExtension(name)).toBe("file.test"); assert.strictEqual(removeExtension(name), "file.test");
}); });
} }
}); });
@ -66,38 +66,38 @@ describe("Extension remover", () => {
describe("Image checker", () => { describe("Image checker", () => {
{ {
const name = "image.Png"; const name = "image.Png";
test(name, () => { it(name, () => {
expect(isImage(name)).toBe(true); assert.strictEqual(isImage(name), true);
}); });
} }
{ {
const name = "image.jpeg"; const name = "image.jpeg";
test(name, () => { it(name, () => {
expect(isImage(name)).toBe(true); assert.strictEqual(isImage(name), true);
}); });
} }
{ {
const name = "image.wav"; const name = "image.wav";
test(name, () => { it(name, () => {
expect(isImage(name)).toBe(false); assert.strictEqual(isImage(name), false);
}); });
} }
{ {
const name = "image.jpg"; const name = "image.jpg";
test(name, () => { it(name, () => {
expect(isImage(name)).toBe(true); assert.strictEqual(isImage(name), true);
}); });
} }
{ {
const name = "image.webP"; const name = "image.webP";
test(name, () => { it(name, () => {
expect(isImage(name)).toBe(true); assert.strictEqual(isImage(name), true);
}); });
} }
{ {
const name = "image.GIF"; const name = "image.GIF";
test(name, () => { it(name, () => {
expect(isImage(name)).toBe(true); assert.strictEqual(isImage(name), true);
}); });
} }
}); });
@ -105,26 +105,27 @@ describe("Image checker", () => {
describe("Code block cleaner", () => { describe("Code block cleaner", () => {
{ {
const name = "salut"; const name = "salut";
test(name, () => { it(name, () => {
expect(cleanCodeBlock(name)).toBe("`salut`"); assert.strictEqual(cleanCodeBlock(name), "`salut`");
}); });
} }
{ {
const name = "<@158260864623968257> ça va ?"; const name = "<@158260864623968257> ça va ?";
test(name, () => { it(name, () => {
expect(cleanCodeBlock(name)).toBe("<@158260864623968257>` ça va ?`"); assert.strictEqual(cleanCodeBlock(name), "<@158260864623968257>` ça va ?`");
}); });
} }
{ {
const name = "t'as vu la vidéo ? https://youtu.be/dQw4w9WgXcQ"; const name = "t'as vu la vidéo ? https://youtu.be/dQw4w9WgXcQ";
test(name, () => { it(name, () => {
expect(cleanCodeBlock(name)).toBe("`t'as vu la vidéo ? `https://youtu.be/dQw4w9WgXcQ"); assert.strictEqual(cleanCodeBlock(name), "`t'as vu la vidéo ? `https://youtu.be/dQw4w9WgXcQ");
}); });
} }
{ {
const name = "t'as vu la vidéo ? https://youtu.be/dQw4w9WgXcQ elle est cool en vrai tqt"; const name = "t'as vu la vidéo ? https://youtu.be/dQw4w9WgXcQ elle est cool en vrai tqt";
test(name, () => { it(name, () => {
expect(cleanCodeBlock(name)).toBe( assert.strictEqual(
cleanCodeBlock(name),
"`t'as vu la vidéo ? `https://youtu.be/dQw4w9WgXcQ` elle est cool en vrai tqt`", "`t'as vu la vidéo ? `https://youtu.be/dQw4w9WgXcQ` elle est cool en vrai tqt`",
); );
}); });
@ -134,16 +135,18 @@ describe("Code block cleaner", () => {
describe("Emoji to link", () => { describe("Emoji to link", () => {
{ {
const name = "☺️"; const name = "☺️";
test(name, () => { it(name, () => {
expect(emojiPng(name)).toBe( assert.strictEqual(
emojiPng(name),
"https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/72x72/263a.png", "https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/72x72/263a.png",
); );
}); });
} }
{ {
const name = "🍕"; const name = "🍕";
test(name, () => { it(name, () => {
expect(emojiPng(name)).toBe( assert.strictEqual(
emojiPng(name),
"https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/72x72/1f355.png", "https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/72x72/1f355.png",
); );
}); });

View file

@ -1,12 +1,15 @@
import { RegexC, RegExpFlags } from "../../utils/regex"; import { RegexC, RegExpFlags } from "../../utils/regex";
import { describe, it } from "node:test";
import assert from "node:assert/strict";
describe("Regex flags", () => { describe("Regex flags", () => {
test("One parameter", () => { it("One parameter", () => {
const regex = RegexC("", RegExpFlags.Global); const regex = RegexC("", RegExpFlags.Global);
expect(regex.global).toBeTruthy(); assert.strictEqual(regex.global, true);
}); });
test("All parameters", () => { it("All parameters", () => {
const regex = RegexC( const regex = RegexC(
"", "",
RegExpFlags.Global | RegExpFlags.Global |
@ -16,11 +19,11 @@ describe("Regex flags", () => {
RegExpFlags.Unicode | RegExpFlags.Unicode |
RegExpFlags.SingleLine, RegExpFlags.SingleLine,
); );
expect(regex.global).toBeTruthy(); assert.strictEqual(regex.global, true);
expect(regex.multiline).toBeTruthy(); assert.strictEqual(regex.multiline, true);
expect(regex.ignoreCase).toBeTruthy(); assert.strictEqual(regex.ignoreCase, true);
expect(regex.sticky).toBeTruthy(); assert.strictEqual(regex.sticky, true);
expect(regex.unicode).toBeTruthy(); assert.strictEqual(regex.unicode, true);
expect(regex.dotAll).toBeTruthy(); assert.strictEqual(regex.dotAll, true);
}); });
}); });

View file

@ -6,33 +6,38 @@ import {
TimeSecond, TimeSecond,
} from "../../utils/time"; } from "../../utils/time";
import { describe, it } from "node:test";
import assert from "node:assert/strict";
describe("Date with correct timezone", () => { describe("Date with correct timezone", () => {
const map = new Map([["u_time_at", "@"]]); const map = new Map([["u_time_at", "@"]]);
const date = new Date(1727434767686); const date = new Date(1727434767686);
{ {
const name = "fr"; const name = "fr";
test(name, () => { it(name, () => {
expect(showDate(name, map, date)).toBe("27/09/2024 @ 12:59:27"); assert.strictEqual(showDate(name, map, date), "27/09/2024 @ 12:59:27");
}); });
} }
{ {
const name = "en-US"; const name = "en-US";
test(name, () => { it(name, () => {
expect(showDate(name, map, date)).toBe("9/27/24, @ 1:59:27"); assert.strictEqual(showDate(name, map, date), "9/27/24, @ 1:59:27");
}); });
} }
{ {
const name = "unknown"; const name = "unknown";
// Depends on the system // Depends on the system
// The important is that the date is in the correct timezone (UTC) // The important is that the date is in the correct timezone (UTC)
test(name, () => { it(name, () => {
expect(["27/09/2024 @ 10:59:27", "9/27/24, @ 10:59:27"]).toContain(showDate(name, map, date)); assert.ok(
["27/09/2024 @ 10:59:27", "9/27/24, @ 10:59:27"].includes(showDate(name, map, date)),
);
}); });
} }
{ {
const name = "zh-CN"; const name = "zh-CN";
test(name, () => { it(name, () => {
expect(showDate(name, map, date)).toBe("2024/9/27 @ 18:59:27"); assert.strictEqual(showDate(name, map, date), "2024/9/27 @ 18:59:27");
}); });
} }
}); });
@ -40,26 +45,26 @@ describe("Date with correct timezone", () => {
describe("String time to seconds", () => { describe("String time to seconds", () => {
{ {
const name = "10m30"; const name = "10m30";
test(name, () => { it(name, () => {
expect(strToSeconds(name)).toBe(630); assert.strictEqual(strToSeconds(name), 630);
}); });
} }
{ {
const name = "12h30"; const name = "12h30";
test(name, () => { it(name, () => {
expect(strToSeconds(name)).toBe(45000); assert.strictEqual(strToSeconds(name), 45000);
}); });
} }
{ {
const name = "12s30"; const name = "12s30";
test(name, () => { it(name, () => {
expect(strToSeconds(name)).toBe(42); assert.strictEqual(strToSeconds(name), 42);
}); });
} }
{ {
const name = "1w30h20"; const name = "1w30h20";
test(name, () => { it(name, () => {
expect(strToSeconds(name)).toBe(714000); assert.strictEqual(strToSeconds(name), 714000);
}); });
} }
}); });
@ -67,54 +72,54 @@ describe("String time to seconds", () => {
describe("Next time unit", () => { describe("Next time unit", () => {
{ {
const name = TimeSecond.Minute; const name = TimeSecond.Minute;
test(name.toString(), () => { it(name.toString(), () => {
expect(nextTimeUnit(name)).toBe(TimeSecond.Second); assert.strictEqual(nextTimeUnit(name), TimeSecond.Second);
}); });
} }
{ {
const name = TimeSecond.Hour; const name = TimeSecond.Hour;
test(name.toString(), () => { it(name.toString(), () => {
expect(nextTimeUnit(name)).toBe(TimeSecond.Minute); assert.strictEqual(nextTimeUnit(name), TimeSecond.Minute);
}); });
} }
{ {
const name = TimeSecond.Second; const name = TimeSecond.Second;
test(name.toString(), () => { it(name.toString(), () => {
expect(nextTimeUnit(name)).toBe(TimeSecond.Second); assert.strictEqual(nextTimeUnit(name), TimeSecond.Second);
}); });
} }
{ {
const name = TimeSecond.Year; const name = TimeSecond.Year;
test(name.toString(), () => { it(name.toString(), () => {
expect(nextTimeUnit(name)).toBe(TimeSecond.Week); assert.strictEqual(nextTimeUnit(name), TimeSecond.Week);
}); });
} }
}); });
describe("Relative time", () => { describe("Relative time", () => {
// Thoses tests are based on time, we have 10s of acceptance. // Thoses its are based on time, we have 10s of acceptance.
{ {
const name = Date.now() + (10 * TimeSecond.Minute + 30) * 1000; const name = Date.now() + (10 * TimeSecond.Minute + 30) * 1000;
test(name.toString(), () => { it(name.toString(), () => {
expect(timeDeltaToString(name)).toMatch(/10m 30s|10m 2\ds/); assert.match(timeDeltaToString(name), /10m 30s|10m 2\ds/);
}); });
} }
{ {
const name = Date.now() + (12 * TimeSecond.Hour + 30 * TimeSecond.Minute) * 1000; const name = Date.now() + (12 * TimeSecond.Hour + 30 * TimeSecond.Minute) * 1000;
test(name.toString(), () => { it(name.toString(), () => {
expect(timeDeltaToString(name)).toMatch(/12h 30m|12h 29m 5\ds/); assert.match(timeDeltaToString(name), /12h 30m|12h 29m 5\ds/);
}); });
} }
{ {
const name = Date.now() + (TimeSecond.Week + TimeSecond.Day + 6 * TimeSecond.Hour) * 1000; const name = Date.now() + (TimeSecond.Week + TimeSecond.Day + 6 * TimeSecond.Hour) * 1000;
test(name.toString(), () => { it(name.toString(), () => {
expect(timeDeltaToString(name)).toMatch(/1w 1d 6h|1w 1d 5h 59m 5\ds/); assert.match(timeDeltaToString(name), /1w 1d 6h|1w 1d 5h 59m 5\ds/);
}); });
} }
{ {
const name = Date.now(); const name = Date.now();
test(name.toString(), () => { it(name.toString(), () => {
expect(timeDeltaToString(name)).toMatch(/\ds/); assert.match(timeDeltaToString(name), /\ds/);
}); });
} }
}); });