Skip to content

Commit 4f757cb

Browse files
committed
refactor: split test helpers
1 parent fa2fdd6 commit 4f757cb

14 files changed

+145
-82
lines changed

tests/integration/helpers.ts

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,9 @@ type ToolInfo = Awaited<ReturnType<Client["listTools"]>>["tools"][number];
2323
export interface IntegrationTest {
2424
mcpClient: () => Client;
2525
mcpServer: () => Server;
26-
mongoClient: () => MongoClient;
27-
connectionString: () => string;
28-
connectMcpClient: () => Promise<void>;
29-
randomDbName: () => string;
3026
}
3127

3228
export function setupIntegrationTest(userConfig: UserConfig = config): IntegrationTest {
33-
let mongoCluster: runner.MongoCluster | undefined;
34-
let mongoClient: MongoClient | undefined;
35-
3629
let mcpClient: Client | undefined;
3730
let mcpServer: Server | undefined;
3831

@@ -76,10 +69,6 @@ export function setupIntegrationTest(userConfig: UserConfig = config): Integrati
7669
await mcpClient.connect(clientTransport);
7770
});
7871

79-
beforeEach(async () => {
80-
randomDbName = new ObjectId().toString();
81-
});
82-
8372
afterAll(async () => {
8473
await mcpClient?.close();
8574
mcpClient = undefined;
@@ -88,54 +77,7 @@ export function setupIntegrationTest(userConfig: UserConfig = config): Integrati
8877
mcpServer = undefined;
8978
});
9079

91-
afterEach(async () => {
92-
await mcpServer?.session.close();
93-
config.connectionString = undefined;
9480

95-
await mongoClient?.close();
96-
mongoClient = undefined;
97-
});
98-
99-
beforeAll(async function () {
100-
// Downloading Windows executables in CI takes a long time because
101-
// they include debug symbols...
102-
const tmpDir = path.join(__dirname, "..", "tmp");
103-
await fs.mkdir(tmpDir, { recursive: true });
104-
105-
// On Windows, we may have a situation where mongod.exe is not fully released by the OS
106-
// before we attempt to run it again, so we add a retry.
107-
let dbsDir = path.join(tmpDir, "mongodb-runner", "dbs");
108-
for (let i = 0; i < 10; i++) {
109-
try {
110-
mongoCluster = await MongoCluster.start({
111-
tmpDir: dbsDir,
112-
logDir: path.join(tmpDir, "mongodb-runner", "logs"),
113-
topology: "standalone",
114-
});
115-
116-
return;
117-
} catch (err) {
118-
if (i < 5) {
119-
// Just wait a little bit and retry
120-
console.error(`Failed to start cluster in ${dbsDir}, attempt ${i}: ${err}`);
121-
await new Promise((resolve) => setTimeout(resolve, 1000));
122-
} else {
123-
// If we still fail after 5 seconds, try another db dir
124-
console.error(
125-
`Failed to start cluster in ${dbsDir}, attempt ${i}: ${err}. Retrying with a new db dir.`
126-
);
127-
dbsDir = path.join(tmpDir, "mongodb-runner", `dbs${i - 5}`);
128-
}
129-
}
130-
}
131-
132-
throw new Error("Failed to start cluster after 10 attempts");
133-
}, 120_000);
134-
135-
afterAll(async function () {
136-
await mongoCluster?.close();
137-
mongoCluster = undefined;
138-
});
13981

14082
const getMcpClient = () => {
14183
if (!mcpClient) {

tests/integration/tools/mongodb/create/createCollection.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describeMongoDB } from "../mongodbHelpers.js";
2+
13
import {
24
getResponseContent,
35
dbOperationParameters,
@@ -8,8 +10,7 @@ import {
810
dbOperationInvalidArgTests,
911
} from "../../../helpers.js";
1012

11-
describe("createCollection tool", () => {
12-
const integration = setupIntegrationTest();
13+
describeMongoDB("createCollection tool", (integration) => {
1314

1415
validateToolMetadata(
1516
integration,

tests/integration/tools/mongodb/create/createIndex.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describeMongoDB } from "../mongodbHelpers.js";
2+
13
import {
24
getResponseContent,
35
dbOperationParameters,
@@ -8,8 +10,7 @@ import {
810
} from "../../../helpers.js";
911
import { IndexDirection } from "mongodb";
1012

11-
describe("createIndex tool", () => {
12-
const integration = setupIntegrationTest();
13+
describeMongoDB("createIndex tool", (integration) => {
1314

1415
validateToolMetadata(integration, "create-index", "Create an index for a collection", [
1516
...dbOperationParameters,

tests/integration/tools/mongodb/create/insertMany.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describeMongoDB } from "../mongodbHelpers.js";
2+
13
import {
24
getResponseContent,
35
dbOperationParameters,
@@ -7,8 +9,7 @@ import {
79
validateThrowsForInvalidArguments,
810
} from "../../../helpers.js";
911

10-
describe("insertMany tool", () => {
11-
const integration = setupIntegrationTest();
12+
describeMongoDB("insertMany tool", (integration) => {
1213

1314
validateToolMetadata(integration, "insert-many", "Insert an array of documents into a MongoDB collection", [
1415
...dbOperationParameters,

tests/integration/tools/mongodb/delete/deleteMany.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describeMongoDB } from "../mongodbHelpers.js";
2+
13
import {
24
getResponseContent,
35
dbOperationParameters,
@@ -7,8 +9,7 @@ import {
79
validateThrowsForInvalidArguments,
810
} from "../../../helpers.js";
911

10-
describe("deleteMany tool", () => {
11-
const integration = setupIntegrationTest();
12+
describeMongoDB("deleteMany tool", (integration) => {
1213

1314
validateToolMetadata(
1415
integration,

tests/integration/tools/mongodb/delete/dropCollection.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describeMongoDB } from "../mongodbHelpers.js";
2+
13
import {
24
getResponseContent,
35
dbOperationParameters,
@@ -8,8 +10,7 @@ import {
810
dbOperationInvalidArgTests,
911
} from "../../../helpers.js";
1012

11-
describe("dropCollection tool", () => {
12-
const integration = setupIntegrationTest();
13+
describeMongoDB("dropCollection tool", (integration) => {
1314

1415
validateToolMetadata(
1516
integration,

tests/integration/tools/mongodb/delete/dropDatabase.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describeMongoDB } from "../mongodbHelpers.js";
2+
13
import {
24
getResponseContent,
35
dbOperationParameters,
@@ -8,8 +10,7 @@ import {
810
dbOperationInvalidArgTests,
911
} from "../../../helpers.js";
1012

11-
describe("dropDatabase tool", () => {
12-
const integration = setupIntegrationTest();
13+
describeMongoDB("dropDatabase tool", (integration) => {
1314

1415
validateToolMetadata(
1516
integration,

tests/integration/tools/mongodb/metadata/collectionSchema.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describeMongoDB } from "../mongodbHelpers.js";
2+
13
import {
24
getResponseElements,
35
getResponseContent,
@@ -12,8 +14,7 @@ import { Document } from "bson";
1214
import { OptionalId } from "mongodb";
1315
import { SimplifiedSchema } from "mongodb-schema";
1416

15-
describe("collectionSchema tool", () => {
16-
const integration = setupIntegrationTest();
17+
describeMongoDB("collectionSchema tool", (integration) => {
1718

1819
validateToolMetadata(
1920
integration,

tests/integration/tools/mongodb/metadata/collectionStorageSize.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describeMongoDB } from "../mongodbHelpers.js";
2+
13
import {
24
getResponseContent,
35
setupIntegrationTest,
@@ -9,8 +11,7 @@ import {
911
} from "../../../helpers.js";
1012
import * as crypto from "crypto";
1113

12-
describe("collectionStorageSize tool", () => {
13-
const integration = setupIntegrationTest();
14+
describeMongoDB("collectionStorageSize tool", (integration) => {
1415

1516
validateToolMetadata(
1617
integration,

tests/integration/tools/mongodb/metadata/connect.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { describeMongoDB } from "../mongodbHelpers.js";
2+
13
import { getResponseContent, setupIntegrationTest, validateToolMetadata } from "../../../helpers.js";
24

35
import { config } from "../../../../../src/config.js";
46

5-
describe("Connect tool", () => {
6-
const integration = setupIntegrationTest();
7+
describeMongoDB("Connect tool", (integration) => {
78

89
validateToolMetadata(integration, "connect", "Connect to a MongoDB instance", [
910
{

0 commit comments

Comments
 (0)