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

Addition of utilities to help spin up and clear a mongo database #3

Merged
merged 3 commits into from
Aug 15, 2023
Merged
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
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
14 changes: 14 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {}
}
90 changes: 90 additions & 0 deletions __tests__/mongo/mongo-extension.t.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { describe, it, expect, afterEach } from "@jest/globals";
import { MongoExtension } from "../../src";
import { MongoClient } from "mongodb";

describe("MongoExtension", () => {
afterEach(async () => {
if (global.MONGO_CONTAINER) {
await global.MONGO_CONTAINER.stop();
global.MONGO_CONTAINER = undefined;
}
});

describe("startMongoContainer", () => {
it("should start the mongo container", async () => {
await MongoExtension.startMongoContainer();

expect(global.MONGO_CONTAINER).toBeDefined();

await global.MONGO_CONTAINER.stop();
}, 60_000);
});

describe("stopMongoContainer", () => {
it("should stop the mongo container", async () => {
await MongoExtension.startMongoContainer();
await MongoExtension.stopMongoContainer();

expect(global.MONGO_CONTAINER).toBeUndefined();
}, 60_000);

it("should throw error when container is not previously started", () => {
expect(MongoExtension.stopMongoContainer()).rejects.toEqual(
Error(
"IllegalStateException: Mongo container has not been previously started",
),
);

expect(global.MONGO_CONTAINER).toBeUndefined();
});
});

describe("getMongoBaseUrl", () => {
it("should return the base url for the started container", async () => {
await MongoExtension.startMongoContainer();

const url = MongoExtension.getMongoBaseUrl();
expect(url).toContain("mongodb://localhost:");
}, 60_000);

it("should throw error when container is not previously started", () => {
expect(() => MongoExtension.getMongoBaseUrl()).toThrow(
"IllegalStateException: Mongo container has not been previously started",
);
});
});

describe("clearDatabase", () => {
it("should drop all collections in the database", async () => {
await MongoExtension.startMongoContainer();

const url = MongoExtension.getMongoBaseUrl();

const client = new MongoClient(url);
const db = client.db("kiwi");

await db.createCollection("collection1");
await db.createCollection("collection2");

const collections = await db.collections();
expect(collections).toHaveLength(2);

await MongoExtension.clearDatabase("kiwi");

const collectionsAfterClear = await db.collections();
expect(collectionsAfterClear).toHaveLength(0);

await client.close(true);
}, 60_000);

it("should throw error when container is not previously started", () => {
expect(MongoExtension.clearDatabase("kiwi")).rejects.toEqual(
Error(
"IllegalStateException: Mongo container has not been previously started",
),
);

expect(global.MONGO_CONTAINER).toBeUndefined();
});
});
});
6 changes: 0 additions & 6 deletions babel.config.js

This file was deleted.

6 changes: 6 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
["@babel/preset-env", { "targets": { "node": "current" } }],
"@babel/preset-typescript"
]
}
Loading