Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: from mocha chai to vitest #986

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
546 changes: 465 additions & 81 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"test": "run-s test:*",
"test:lint": "prettier '**/*.ts' --list-different",
"test:type-check": "tsc --noEmit",
"test:unit": "NODE_ENV=test mocha --require tsx test/*.ts",
"test:unit": "NODE_ENV=test vitest --exclude=build --exclude=packages",
"test:unit:watch": "npm run test:unit -- --watch --watch-files src/**/*.ts,test/**/*.ts",
"test:workspaces": "npm run test --if-present --workspaces",
"update-organization-info": "tsx scripts/update-organizations-info.ts",
Expand Down Expand Up @@ -106,8 +106,6 @@
"@simplewebauthn/types": "^10.0.0",
"@simplewebauthn/typescript-types": "^8.3.4",
"@sinonjs/fake-timers": "^11.2.2",
"@types/chai": "^5.0.1",
"@types/chai-as-promised": "^7.1.8",
"@types/console-log-level": "^1.4.5",
"@types/cookie-parser": "^1.4.7",
"@types/ejs": "^3.1.5",
Expand All @@ -117,32 +115,29 @@
"@types/http-errors": "^2.0.4",
"@types/lodash": "^4.17.10",
"@types/lodash-es": "^4.17.12",
"@types/mocha": "^10.0.10",
"@types/morgan": "^1.9.9",
"@types/node": "^22.10.2",
"@types/nodemailer": "^6.4.16",
"@types/oidc-provider": "^8.5.2",
"@types/qrcode": "^1.5.5",
"@types/sinonjs__fake-timers": "^8.1.5",
"axe-core": "^4.8.4",
"chai": "^5.1.2",
"chai-as-promised": "^8.0.1",
"concurrently": "^9.0.1",
"copy-and-watch": "^0.1.6",
"csv": "^6.3.9",
"cypress": "^14.0.2",
"cypress-axe": "^1.6.0",
"cypress-maildev": "^1.3.2",
"lodash": "^4.17.21",
"mocha": "^11.0.1",
"nock": "^13.5.6",
"node-pg-migrate": "^7.6.1",
"npm-run-all2": "^6.1.2",
"prettier": "^3.4.2",
"prettier-plugin-organize-imports": "^4.1.0",
"tld-extract": "^2.1.0",
"typescript": "^5.7.2",
"vite": "^5.4.12"
"vite": "^5.4.12",
"vitest": "^3.0.5"
},
"packageManager": "[email protected]+sha512.acf301ad9b9ddba948fcb72341e2f0fcae477f56a95cc2a092934d133a7461062633cefbf93d5934a3dc0768674e2edee9f04dcfcc4bb4c327ff0e3a7d552a1b",
"engines": {
Expand Down
14 changes: 3 additions & 11 deletions packages/identite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@
"check": "npm run build -- --noEmit",
"clean": "rm -rf dist tsconfig*.tsbuildinfo",
"dev": "npm run build -- --watch --preserveWatchOutput",
"test": "mocha"
},
"mocha": {
"reporter": "spec",
"require": [
"tsx"
],
"spec": "src/**/*.test.ts"
"test": "vitest --moduleResolution node"
},
"dependencies": {
"sql-template-tag": "^5.2.1"
Expand All @@ -57,11 +50,10 @@
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.2",
"await-to-js": "^3.0.0",
"chai": "^5.1.2",
"mocha": "^11.0.1",
"node-pg-migrate": "^7.6.1",
"pg": "^8.13.0",
"tsx": "^4.19.2"
"tsx": "^4.19.2",
"vitest": "^3.0.5"
},
"publishConfig": {
"access": "public",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import { emptyDatabase, migrate, pg } from "#testing";
import { expect } from "chai";
import { before, describe, it } from "mocha";
import { describe, it } from "mocha";
import { addDomainFactory } from "./add-domain.js";

//

const addDomain = addDomainFactory({ pg: pg as any });

describe(addDomainFactory.name, () => {
before(migrate);
beforeAll(migrate);
beforeEach(emptyDatabase);

it("should add domain", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { emptyDatabase, migrate, pg } from "#testing";
import { expect } from "chai";
import { before, describe, it } from "mocha";
import { describe, it } from "mocha";
import { findEmailDomainsByOrganizationIdFactory } from "./find-email-domains-by-organization-id.js";

//
Expand All @@ -11,7 +11,7 @@ const findEmailDomainsByOrganizationId =
findEmailDomainsByOrganizationIdFactory({ pg: pg as any });

describe(findEmailDomainsByOrganizationIdFactory.name, () => {
before(migrate);
beforeAll(migrate);
beforeEach(emptyDatabase);

it("should find email domains by organization id", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import { emptyDatabase, migrate, pg } from "#testing";
import { expect } from "chai";
import { before, describe, it } from "mocha";
import { describe, it } from "mocha";
import { findByIdFactory } from "./find-by-id.js";

//

const findById = findByIdFactory({ pg: pg as any });

describe(findByIdFactory.name, () => {
before(migrate);
beforeAll(migrate);
beforeEach(emptyDatabase);

it("should find the Necron organization", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import { emptyDatabase, migrate, pg } from "#testing";
import { expect } from "chai";
import { before, describe, it } from "mocha";
import { describe, it } from "mocha";
import { getUsersByOrganizationFactory } from "./get-users-by-organization.js";

//

const getUsersByOrganization = getUsersByOrganizationFactory({ pg: pg as any });

describe(getUsersByOrganizationFactory.name, () => {
before(migrate);
beforeAll(migrate);
beforeEach(emptyDatabase);

it("should find users by organization id", async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/identite/src/repositories/organization/upset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import { emptyDatabase, migrate, pg } from "#testing";
import { expect } from "chai";
import { before, describe, it } from "mocha";
import { describe, it } from "mocha";
import { upsertFactory } from "./upsert.js";

//

const upset = upsertFactory({ pg: pg as any });

describe("upset", () => {
before(migrate);
beforeAll(migrate);
beforeEach(emptyDatabase);

it("should create the Tau Empire organization", async () => {
Expand Down
15 changes: 6 additions & 9 deletions packages/identite/src/repositories/user/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
//

import { emptyDatabase, migrate, pg } from "#testing";
import { expect } from "chai";
import { before, describe, it } from "mocha";
import { beforeAll, beforeEach, expect, test } from "vitest";
import { createUserFactory } from "./create.js";

//

const createUser = createUserFactory({ pg: pg as any });

describe(createUserFactory.name, () => {
before(migrate);
beforeEach(emptyDatabase);
beforeAll(migrate);
beforeEach(emptyDatabase);

it("should create the god-emperor of mankind", async () => {
const user = await createUser({ email: "god-emperor@mankind" });
expect(user.email).to.equal("god-emperor@mankind");
});
test("should create the god-emperor of mankind", async () => {
const user = await createUser({ email: "god-emperor@mankind" });
expect(user.email).toBe("god-emperor@mankind");
});
39 changes: 18 additions & 21 deletions packages/identite/src/repositories/user/find-by-email.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
//

import { emptyDatabase, migrate, pg } from "#testing";
import { expect } from "chai";
import { before, describe, it } from "mocha";
import { beforeAll, beforeEach, expect, test } from "vitest";
import { findByEmailFactory } from "./find-by-email.js";

//

const findByEmail = findByEmailFactory({ pg: pg as any });

describe(findByEmailFactory.name, () => {
before(migrate);
beforeEach(emptyDatabase);
beforeAll(migrate);
beforeEach(emptyDatabase);

it("should find a user by email", async () => {
await pg.sql`
INSERT INTO users
(id, email, created_at, updated_at, given_name, family_name, phone_number, job)
VALUES
(1, '[email protected]', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque'),
(2, '[email protected]', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'iv', 'primarque')
;
`;
test("should find a user by email", async () => {
await pg.sql`
INSERT INTO users
(id, email, created_at, updated_at, given_name, family_name, phone_number, job)
VALUES
(1, '[email protected]', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque'),
(2, '[email protected]', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'iv', 'primarque')
;
`;

const user = await findByEmail("[email protected]");
const user = await findByEmail("[email protected]");

expect(user?.email).to.equal("[email protected]");
});
expect(user?.email).toBe("[email protected]");
});

it("❎ fail to find the God-Emperor of Mankind", async () => {
const user = await findByEmail("the God-Emperor of Mankind");
test("❎ fail to find the God-Emperor of Mankind", async () => {
const user = await findByEmail("the God-Emperor of Mankind");

expect(user).to.be.undefined;
});
expect(user).toBeUndefined();
});
39 changes: 18 additions & 21 deletions packages/identite/src/repositories/user/find-by-id.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
//

import { emptyDatabase, migrate, pg } from "#testing";
import { expect } from "chai";
import { before, describe, it } from "mocha";
import { beforeAll, beforeEach, expect, test } from "vitest";
import { findByIdFactory } from "./find-by-id.js";

//

const findById = findByIdFactory({ pg: pg as any });

describe(findByIdFactory.name, () => {
before(migrate);
beforeEach(emptyDatabase);
beforeAll(migrate);
beforeEach(emptyDatabase);

it("should find a user by id", async () => {
await pg.sql`
INSERT INTO users
(id, email, created_at, updated_at, given_name, family_name, phone_number, job)
VALUES
(1, '[email protected]', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque'),
(2, '[email protected]', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'iv', 'primarque')
;
`;
test("should find a user by id", async () => {
await pg.sql`
INSERT INTO users
(id, email, created_at, updated_at, given_name, family_name, phone_number, job)
VALUES
(1, '[email protected]', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque'),
(2, '[email protected]', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'iv', 'primarque')
;
`;

const user = await findById(1);
const user = await findById(1);

expect(user?.email).to.equal("[email protected]");
});
expect(user?.email).toEqual("[email protected]");
});

it("❎ fail to find the God-Emperor of Mankind", async () => {
const user = await findById(42);
test("❎ fail to find the God-Emperor of Mankind", async () => {
const user = await findById(42);

expect(user).to.be.undefined;
});
expect(user).toBeUndefined();
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//

import { emptyDatabase, migrate, pg } from "#testing";
import { expect } from "chai";
import { before, describe, it } from "mocha";
import { beforeAll, beforeEach, expect, test } from "vitest";
import { updateUserOrganizationLinkFactory } from "./update-user-organization-link.js";

//
Expand All @@ -11,36 +10,35 @@ const updateUserOrganizationLink = updateUserOrganizationLinkFactory({
pg: pg as any,
});

describe(updateUserOrganizationLink.name, () => {
before(migrate);
beforeEach(emptyDatabase);
beforeAll(migrate);
beforeEach(emptyDatabase);

it("should update the user organization link", async () => {
await pg.sql`
INSERT INTO organizations
(cached_libelle, cached_nom_complet, id, siret, created_at, updated_at)
VALUES
('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19')
;
`;
await pg.sql`
INSERT INTO users
(id, email, created_at, updated_at, given_name, family_name, phone_number, job)
VALUES
(1, '[email protected]', '4444-04-04', '4444-04-04', 'Lion', 'El''Jonson', 'I', 'Primarque')
;
`;
await pg.sql`
INSERT INTO users_organizations
(user_id, organization_id, created_at, updated_at, is_external, verification_type, needs_official_contact_email_verification, official_contact_email_verification_token, official_contact_email_verification_sent_at)
VALUES
(1, 1, '4444-04-04', '4444-04-04', false, 'no_verification_means_available', false, null, null)
;
`;
test("should update the user organization link", async () => {
await pg.sql`
INSERT INTO organizations
(cached_libelle, cached_nom_complet, id, siret, created_at, updated_at)
VALUES
('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19')
;
`;
await pg.sql`
INSERT INTO users
(id, email, created_at, updated_at, given_name, family_name, phone_number, job)
VALUES
(1, '[email protected]', '4444-04-04', '4444-04-04', 'Lion', 'El''Jonson', 'I', 'Primarque')
;
`;
await pg.sql`
INSERT INTO users_organizations
(user_id, organization_id, created_at, updated_at, is_external, verification_type, needs_official_contact_email_verification, official_contact_email_verification_token, official_contact_email_verification_sent_at)
VALUES
(1, 1, '4444-04-04', '4444-04-04', false, 'no_verification_means_available', false, null, null)
;
`;

const user = await updateUserOrganizationLink(1, 1, {
is_external: true,
});
expect(user.is_external).to.equal(true);
const user = await updateUserOrganizationLink(1, 1, {
is_external: true,
});

expect(user.is_external).toBeTruthy();
});
Loading
Loading