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

upgrade to mongo-memory-server >=7 #28

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {}
"rules": {
"@typescript-eslint/no-namespace": ["off"]
}
}
5 changes: 2 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 17.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

node-version: [16.x, 17.x, 18.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -27,4 +25,5 @@ jobs:
- run: yarn install --frozen-lockfile
- run: yarn lint
- run: yarn prettier
- run: yarn test
- run: yarn compile
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules/
dist/
lerna.json
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"npm.packageManager": "yarn"
"npm.packageManager": "yarn",
"[typescript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
}
}
8 changes: 0 additions & 8 deletions lerna.json

This file was deleted.

21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
"lint:fix": "eslint . --ext .ts --max-warnings 0 --fix"
},
"devDependencies": {
"@tsconfig/node12": "1.0.9",
"@types/jest": "27.4.0",
"@tsconfig/node16": "^1.0.3",
"@types/jest": "29.5.0",
"@types/mongodb": "4.0.7",
"@typescript-eslint/eslint-plugin": "5.10.1",
"@typescript-eslint/parser": "5.10.1",
"eslint": "8.7.0",
"lerna": "3.22.1",
"mongodb-memory-server": "6.9.3",
"prettier": "2.5.1",
"rimraf": "3.0.2",
"typescript": "4.5.5"
"@types/node": "^18.15.7",
"@typescript-eslint/eslint-plugin": "5.56.0",
"@typescript-eslint/parser": "5.56.0",
"eslint": "8.36.0",
"jest-environment-node": "^29.5.0",
"mongodb-memory-server": "8.12.1",
"prettier": "2.8.7",
"rimraf": "4.4.1",
"typescript": "5.0.2"
}
}
1 change: 0 additions & 1 deletion packages/jest-environment-mongodb-ephemeral/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# jest-environment-mongodb-ephemeral

[![npm version](https://badge.fury.io/js/jest-environment-mongodb-ephemeral.svg)](https://badge.fury.io/js/jest-environment-mongodb-ephemeral)
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/)

This package provides a wrapper around
[jest-environment-mongodb](https://www.npmjs.com/package/jest-environment-mongodb)
Expand Down
6 changes: 2 additions & 4 deletions packages/jest-environment-mongodb-ephemeral/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
"files": [
"dist"
],
"dependencies": {
"jest-environment-mongodb": "^1.0.5"
},
"peerDependencies": {
"mongodb-memory-server": "4.x || 5.x || 6.x"
"mongodb-memory-server": "7.x || 8.x",
"jest-environment-node": "*"
}
}
22 changes: 10 additions & 12 deletions packages/jest-environment-mongodb-ephemeral/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import MongoDbEnvironment, {
MongoDbEnvironmentConfig,
} from "jest-environment-mongodb";
import MongoDbEnvironment, { MongoDbEnvironmentConfig } from "jest-environment-mongodb";
import type { EnvironmentContext } from "@jest/environment";

export default class EphemeralEnvironment extends MongoDbEnvironment {
constructor(config: MongoDbEnvironmentConfig) {
super({
constructor(config: MongoDbEnvironmentConfig, context: EnvironmentContext) {
config = {
...config,
testEnvironmentOptions: {
...config.testEnvironmentOptions,
instance: {
...(config.testEnvironmentOptions.instance as Record<string, never>),
storageEngine: "ephemeralForTest",
},
},
});
...config.testEnvironmentOptions?.instance ?? {},
storageEngine: "ephemeralForTest"
}
}
}
super(config, context);
}
}

module.exports = EphemeralEnvironment;
1 change: 0 additions & 1 deletion packages/jest-environment-mongodb-wiredtiger/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# jest-environment-mongodb-wiredtiger

[![npm version](https://badge.fury.io/js/jest-environment-mongodb-wiredtiger.svg)](https://badge.fury.io/js/jest-environment-mongodb-wiredtiger)
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/)

This package provides a wrapper around
[jest-environment-mongodb](https://www.npmjs.com/package/jest-environment-mongodb)
Expand Down
6 changes: 2 additions & 4 deletions packages/jest-environment-mongodb-wiredtiger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
"files": [
"dist"
],
"dependencies": {
"jest-environment-mongodb": "^1.0.5"
},
"peerDependencies": {
"mongodb-memory-server": "4.x || 5.x || 6.x"
"mongodb-memory-server": "7.x || 8.x",
"jest-environment-node": "*"
}
}
22 changes: 10 additions & 12 deletions packages/jest-environment-mongodb-wiredtiger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import MongoDbEnvironment, {
MongoDbEnvironmentConfig,
} from "jest-environment-mongodb";
import MongoDbEnvironment, { MongoDbEnvironmentConfig } from "jest-environment-mongodb";
import type { EnvironmentContext } from "@jest/environment";

export default class WiredTigerEnvironment extends MongoDbEnvironment {
constructor(config: MongoDbEnvironmentConfig) {
super({
constructor(config: MongoDbEnvironmentConfig, context: EnvironmentContext) {
config = {
...config,
testEnvironmentOptions: {
...config.testEnvironmentOptions,
instance: {
...(config.testEnvironmentOptions.instance as Record<string, never>),
storageEngine: "wiredTiger",
},
},
});
...config.testEnvironmentOptions?.instance ?? {},
storageEngine: "wiredTiger"
}
}
}
super(config, context);
}
}

module.exports = WiredTigerEnvironment;
16 changes: 5 additions & 11 deletions packages/jest-environment-mongodb/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# jest-environment-mongodb

[![npm version](https://badge.fury.io/js/jest-environment-mongodb.svg)](https://badge.fury.io/js/jest-environment-mongodb)
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/)

Easily run [Jest](https://jestjs.io/) integration tests that require a running
[MongoDB](https://www.mongodb.com/) server
Expand Down Expand Up @@ -109,9 +108,7 @@ jest --watch --runInBand mytests.test.js
Configure the MongoDB server by passing options to `testEnvironmentOptions` of
your Jest configuration file.

The available `testEnvironmentOptions` are the same as the
`mongodb-memory-server`
[options](https://www.npmjs.com/package/mongodb-memory-server#available-options).
The available `testEnvironmentOptions` are the same as for [`mongodb-memory-server`](https://www.npmjs.com/package/mongodb-memory-server)`.

Example:

Expand All @@ -134,12 +131,11 @@ environments.

## Globals

The `jest-environment-mongodb` environment exposes three global variables:
The `jest-environment-mongodb` environment exposes two global variables:

```
global.MONGO_URI // The server connection URI
global.MONGO_DB_NAME // The database name
global.MONGOD // The mongod instance from `mongodb-memory-server`
global.__MONGO_URI__ // The server connection URI with a random db name
global.__MONGOD__ // The mongod instance from `mongodb-memory-server`
```

## Usage
Expand All @@ -152,11 +148,9 @@ global.MONGOD // The mongod instance from `mongodb-memory-server`
import { MongoClient } from "mongodb";

let client;
let db;

beforeAll(async () => {
client = await MongoClient.connect(global.MONGO_URI);
db = await client.db(global.MONGO_DB_NAME);
});

afterAll(async () => {
Expand All @@ -165,7 +159,7 @@ afterAll(async () => {

beforeEach(async () => {
// Reset the database before each test
await db.dropDatabase();
await client.db().dropDatabase();
});

it("should aggregate docs from collection", async () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-environment-mongodb/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
13 changes: 10 additions & 3 deletions packages/jest-environment-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
"files": [
"dist"
],
"dependencies": {
"jest-environment-node": "^26.6.2"
"scripts": {
"test": "jest"
},
"peerDependencies": {
"mongodb-memory-server": "4.x || 5.x || 6.x"
"jest-environment-node": "*",
"mongodb-memory-server": "7.x || 8.x"
},
"devDependencies": {
"@types/jest": "^29.5.0",
"jest": "^29.5.0",
"mongodb": "^5.1.0",
"ts-jest": "^29.0.5"
}
}
9 changes: 0 additions & 9 deletions packages/jest-environment-mongodb/src/NodeJS.d.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/jest-environment-mongodb/src/another.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* @jest-environment ./src/index.ts */
it("is just a stub for runInBand mode to watch", async () => {
console.log("stubstub");
});
48 changes: 48 additions & 0 deletions packages/jest-environment-mongodb/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* @jest-environment ./src/index.ts */
import { MongoClient } from "mongodb";

declare namespace global {
const __MONGO_URI__: string;
}

let client: MongoClient;

beforeAll(async () => {
client = await MongoClient.connect(global.__MONGO_URI__);
});

afterAll(async () => {
await client.close();
});

beforeEach(async () => {
// Reset the database before each test
await client.db().dropDatabase();
});

it("should aggregate docs from collection", async () => {
expect(true).toBeTruthy()
const files = client.db().collection("files");

await files.insertMany([
{ type: "Document" },
{ type: "Video" },
{ type: "Image" },
{ type: "Document" },
{ type: "Image" },
{ type: "Document" },
]);

const topFiles = await files
.aggregate([
{ $group: { _id: "$type", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
])
.toArray();

expect(topFiles).toEqual([
{ _id: "Document", count: 3 },
{ _id: "Image", count: 2 },
{ _id: "Video", count: 1 },
]);
});
Loading