Skip to content

Commit

Permalink
🚧 conversion to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
coyotte508 committed Nov 26, 2023
1 parent 07ef2e6 commit 82128f2
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 1,170 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
import assert from "assert";
import { Server } from "http";
import env from "./env";
import initDb from "./db";
import mongoose from "mongoose";
import { listen } from "../app";
import initDb from "./db";
import env from "./env";

let server: Server;
assert(process.env.NODE_ENV === "test");

let server: Server;
assert(env.database.bgs.name.endsWith("-test"));

async function init() {
assert(env.database.bgs.name.endsWith("-test"));
env.listen.port.api = 50606;
env.silent = true;

export async function setupForTest() {
await initDb(env.database.bgs.url, false);

const users = await mongoose.connection.db.collection("users").countDocuments();
assert(users < 10, "This doesn't seem to be a test database");
await mongoose.connection.db.dropDatabase();

env.listen.port.api = 50606;
env.silent = true;

server = await listen();

run();
}

init();

after(async () => {
export async function teardownForTest() {
server.close();
await mongoose.connection.db.dropDatabase();
await mongoose.connection.close();
});
}
1 change: 1 addition & 0 deletions apps/api/app/engine/elo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { eloDiff } from "./elo";
import { describe, it, expect } from "vitest";

describe("eloDiff", () => {
it("should let player win if the opponent dropped", () => {
Expand Down
17 changes: 12 additions & 5 deletions apps/api/app/models/gamenotification.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mongoose, { Types } from "mongoose";
import { defaultKarma, Game, GameNotification, GamePreferences, maxKarma, User } from "./index";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";

const { ObjectId } = Types;

Expand All @@ -11,7 +12,7 @@ describe("GameNotification", () => {

describe("processGameEnded", () => {
describe("karma", () => {
before(async () => {
beforeAll(async () => {
await mongoose.connection.db.dropDatabase();

await User.create({ _id: userId, account: { username: "test", email: "[email protected]" } });
Expand All @@ -28,7 +29,9 @@ describe("GameNotification", () => {
],
});
});
after(() => mongoose.connection.db.dropDatabase());
afterAll(async () => {
await mongoose.connection.db.dropDatabase();
});

it("should add karma to the active player and no karma to the dropped player", async () => {
await GameNotification.create({ kind: "gameEnded", game: "test" });
Expand Down Expand Up @@ -74,7 +77,9 @@ describe("GameNotification", () => {
await GamePreferences.create({ game: "gaia-project", user: userId2, elo: { value: 110, games: 110 } });
await GamePreferences.create({ game: "gaia-project", user: userId3, elo: { value: 105, games: 5 } });
});
afterEach(() => mongoose.connection.db.dropDatabase());
afterEach(async () => {
await mongoose.connection.db.dropDatabase();
});

it("should add elo to player and player2, and min elo 100 to player3, set elo 1 to beginner player4 ", async () => {
await GameNotification.create({ kind: "gameEnded", game: "test" });
Expand Down Expand Up @@ -161,12 +166,14 @@ describe("GameNotification", () => {
});

describe("processPlayerDrop", () => {
before(async () => {
beforeAll(async () => {
await User.create({ _id: userId, account: { username: "test", email: "[email protected]" } });
await User.create({ _id: userId2, account: { username: "test2", email: "[email protected]" } });
});

after(() => mongoose.connection.db.dropDatabase());
afterAll(async () => {
await mongoose.connection.db.dropDatabase();
});

it("should drop 10 karma after dropping out", async () => {
await GameNotification.create({
Expand Down
8 changes: 6 additions & 2 deletions apps/api/app/routes/game/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import mongoose, { Types } from "mongoose";
import env from "../../config/env";
import { Game, GameInfo, GamePreferences, JwtRefreshToken, User } from "../../models";
import { afterAll, beforeAll, describe, it } from "vitest";
import { expect } from "chai";

describe("Game API", () => {
const userId = new Types.ObjectId();
let headers: Record<string, string>;

before(async () => {
beforeAll(async () => {
await User.create({
_id: userId,
account: { username: "test", email: "[email protected]" },
Expand Down Expand Up @@ -126,5 +128,7 @@ describe("Game API", () => {
expect(await Game.countDocuments({ _id: "test" })).to.equal(0, "Game should be deleted after creator unjoins");
});

after(() => mongoose.connection.db.dropDatabase());
afterAll(async () => {
await mongoose.connection.db.dropDatabase();
});
});
4 changes: 2 additions & 2 deletions apps/api/app/routes/game/listings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { GameStatus } from "@bgs/types";
import { joinAnd } from "@bgs/utils/join-and";
import { removeFalsy } from "@bgs/utils/remove-falsy";
import { Game } from "app/models";
import GameInfoService from "app/services/gameinfo";
import { Game } from "../../models";
import GameInfoService from "../../services/gameinfo";
import assert from "assert";
import { Context } from "koa";
import Router from "koa-router";
Expand Down
3 changes: 2 additions & 1 deletion apps/api/app/services/gameinfo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { GameInfo, GamePreferences, User } from "../models";
import { sortBy } from "lodash";
import { seed } from "../../scripts/seed";
import GameInfoService from "./gameinfo";
import { describe, beforeAll, it, expect } from "vitest";

describe("GameInfoService", () => {
describe("latestAccessibleGames", () => {
before(async () => {
beforeAll(async () => {
await seed(["GameInfo", "User", "GamePreferences"], true);
});

Expand Down
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"lodash": "^4.17.13",
"mailgun-js": "^0.20.0",
"mongo-locks": "^2.0.0",
"mongoose": "^6.0.3",
"mongoose": "6.0.3",
"node-cache": "^5.1.0",
"passport-discord": "^0.1.4",
"passport-facebook": "^3.0.0",
Expand Down
48 changes: 24 additions & 24 deletions apps/api/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion apps/api/scripts/seed.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import mongoose from "mongoose";
import { seed } from "./seed";
import { describe, beforeAll, it } from "vitest";

describe("Seed", () => {
before(() => mongoose.connection.db.dropDatabase());
beforeAll(() => mongoose.connection.db.dropDatabase().then(() => {}));

it("should seed with no problems", async () => {
await seed();
Expand Down
3 changes: 1 addition & 2 deletions apps/game-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"lint": "eslint server.ts app/**/*.{js,ts,tsx} --fix",
"lint-check": "eslint server.ts app/**/*.{js,ts,tsx} --max-warnings 0",
"start": "ts-node -T server.ts || npm start",
"test": "vitest run",
"type-check": "tsc --noEmit"
},
"dependencies": {
Expand All @@ -30,7 +29,7 @@
"koa-router": "^8.0.8",
"lodash": "^4.17.15",
"mongo-locks": "^2.0.0",
"mongoose": "^6.0.3"
"mongoose": "6.0.3"
},
"devDependencies": {
"@types/fs-extra": "^8.1.0",
Expand Down
Loading

0 comments on commit 82128f2

Please sign in to comment.