Skip to content

Mongosh test suite PoC #49

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

Merged
merged 4 commits into from
Jun 27, 2025
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
# IDE files
## Visual Studio Code files
.vscode

## JetBrains IDE files
*.idea

## Python virtual environment
*/venv

# Language specific output
## Node.js dependencies
*/node_modules
node_modules

## Java build files
*/target

# Mongosh temporary files
temp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MongoClient } from 'mongodb';

export async function createIndexBasic() {
// connect to your Atlas deployment
// connect to the Atlas deployment
const uri = "<connection-string>";
const client = new MongoClient(uri);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#! /bin/bash

PROJECT=$(git rev-parse --show-toplevel)
JS_EXAMPLES=$PROJECT/javascript/examples
GENERATED_EXAMPLES=$PROJECT/generated-examples/javascript
JS_EXAMPLES=$PROJECT/usage-examples/javascript/driver/examples
GENERATED_EXAMPLES=$PROJECT/generated-usage-examples/javascript/driver

echo "Bluehawking JavaScript examples"
npx bluehawk snip $JS_EXAMPLES -o $GENERATED_EXAMPLES

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

132 changes: 132 additions & 0 deletions usage-examples/javascript/mongosh/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# mongosh Code Example Test PoC

This is a PoC to explore testing mongosh code examples for MongoDB documentation.

The structure of this JavaScript project is as follows:

- `/examples`: This directory contains example code and output to validate.
- `/tests`: This directory contains the test infrastructure to actually run
the tests by invoking the example code.

While running the tests, this test suite creates files in a `/temp` directory. We concatenate
the code example with the necessary commands to connect to the deployment and use the correct
database.

## To run the tests locally

### Create a MongoDB Docker Deployment

To run these tests locally, you need a MongoDB Docker deployment. Make sure Docker is
running, and then:

1. Pull the MongoDB image from Docker Hub:

```shell
docker pull mongo
```
2. Run the container:

```shell
docker run --name mongodb-test -d -p 27017:27017 mongo
```

3. Verify the container is running:

```shell
docker ps
```

The output resembles:

```text
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ef70cce38f26 mongo "/usr/local/bin/runn…" 29 hours ago Up 29 hours (healthy) 127.0.0.1:63201->27017/tcp mongodb-test
```

You may note the actual port is different than `27017`, if something else is already running on
`27017` on your machine. Note the port next to the IP address for running the tests. Alternately, you can get just
the port info for your container using the following command:

```shell
docker port mongodb-test
```

The output resembles:

```text
27017/tcp -> 0.0.0.0:27017
27017/tcp -> [::]:27017
```

### Create a .env file

Create a file named `.env` at the root of the `/mongosh` directory.
Add the following values to your .env file, substituting the port where your local deployment
is running:

```
CONNECTION_STRING="mongodb://localhost:63201"
CONNECTION_PORT="63201"
```

### Install the dependencies

This test suite requires you to have `Node.js` v20 or newer installed. If you
do not yet have Node installed, refer to
[the Node.js installation page](https://nodejs.org/en/download/package-manager)
for details.

From the root of the `/mongosh` directory, run the following command to install
dependencies:

```
npm install
```

### Run the tests

You can run tests from the command line or through your IDE.

#### Run All Tests from the command line

From the `/mongosh` directory, run:

```
npm test
```

This invokes the following command from the `package.json` `test` key:

```
jest --runInBand --detectOpenHandles
```

In the above command:

- `jest` is the command to run the test suite
- `--runInBand` is a flag that specifies only running one test at a time
to avoid collisions when creating/editing/dropping indexes. Otherwise, Jest
defaults to running tests in parallel.
- `--detectOpenHandles` is a flag that tells Jest to track resource handles or async
operations that remain open after the tests are complete. These can cause the test suite
to hang, and this flag tells Jest to report info about these instances.

#### Run Test Suites from the command line
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why aren't we using npm scripts here? would let us simplify these last sections to running the following from /mongosh root without requiring jest installed:

npm test -- -t '<text string from the 'describe()' block you want to run>'

or

npm test -- -t '<text string from the 'it()' block you want to run>'

...if we do keep them as a jest commands, we should:

  • add jest install to prereqs (npm install -g jest)
  • drop the export since it's not needed -- the setup already handles it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! I did not know this was an option 😅 Updated!


You can run all the tests in a given test suite (file).

From the `/mongosh` directory, run:

```
npm test -- -t '<text string from the 'describe()' block you want to run>'
```

#### Run Individual Tests from the command line

You can run a single test within a given test suite (file).

From the `/mongosh` directory, run:

```
npm test -- -t '<text string from the 'it()' block you want to run>'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"_id": "2014-04-04",
"totalSaleAmount": Decimal128("200"),
"averageQuantity": 15,
"count": 2
},
{
"_id": "2014-03-15",
"totalSaleAmount": Decimal128("50"),
"averageQuantity": 10,
"count": 1
},
{
"_id": "2014-03-01",
"totalSaleAmount": Decimal128("40"),
"averageQuantity": 1.5,
"count": 2
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
db.sales.aggregate([
// First Stage
{
$match : { "date": { $gte: new ISODate("2014-01-01"), $lt: new ISODate("2015-01-01") } }
},
// Second Stage
{
$group : {
_id : { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } },
averageQuantity: { $avg: "$quantity" },
count: { $sum: 1 }
}
},
// Third Stage
{
$sort : { totalSaleAmount: -1 }
}
])
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
_id: null,
totalSaleAmount: Decimal128('452.5'),
averageQuantity: 7.875,
count: 8
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
db.sales.aggregate([
{
$group : {
_id : null,
totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } },
averageQuantity: { $avg: "$quantity" },
count: { $sum: 1 }
}
}
])
Loading