Guide to running, testing, and collecting coverage for a Node.js Solar System app locally, plus Docker, tests, OpenAPI, and CI/CD considerations
In this lesson you’ll run the Solar System Node.js application on your local machine or VM, inspect its structure, execute tests, collect coverage, and observe runtime behavior. Later lessons will show how to automate these same steps using Jenkins CI/CD.This repository contains:
A small HTML frontend
Server startup code (app.js)
Controller logic and Mongoose model (app.controller.js)
app.js boots Express and connects to MongoDB using Mongoose. At runtime the app expects these environment variables:
MONGO_URI
MONGO_USERNAME
MONGO_PASSWORD
Below is a corrected minimal excerpt showing required imports and proper mongoose.connect usage:
Copy
const express = require("express");const path = require("path");const OS = require("os");const bodyParser = require("body-parser");const mongoose = require("mongoose");const cors = require("cors");const serverless = require("serverless-http");const app = express();app.use(bodyParser.json());app.use(express.static(path.join(__dirname, "/")));app.use(cors());mongoose.connect( process.env.MONGO_URI, { user: process.env.MONGO_USERNAME, pass: process.env.MONGO_PASSWORD, useNewUrlParser: true, useUnifiedTopology: true, }, function (err) { if (err) { console.log("MongoDB connection error: " + err); } else { // Connection successful } },);// ... other app routes and server startup ...
Ensure MONGO_URI, MONGO_USERNAME, and MONGO_PASSWORD are provided in your
environment before running tests or starting the server. Without a valid
MONGO_URI, mongoose.connect will fail with “The uri parameter to openUri()
must be a string, got ‘undefined‘“.
The client script fetches /os on window load and writes the host/pod name into the page. The snippet below demonstrates robust fetch handling and wiring the click handler:
$ npm installadded 365 packages, and audited 366 packages in 3s44 packages are looking for funding run `npm fund` for details8 vulnerabilities (2 moderate, 5 high, 1 critical)To address all issues, run: npm audit fixRun `npm audit` for details.
Running tests — common failure mode and mitigation
If you run npm test without MONGO_URI set, app.js will try to connect to MongoDB and Mongoose will throw an error during startup:
Copy
$ npm test> Solar System@6.7.6 test> mocha app-test.js --timeout 10000 --reporter mocha-junit-reporter --exitMongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string. at Connection.openUri (...node_modules/mongoose/lib/connection.js:694:11) ...
To run tests locally you can:
Provide real environment variables (MONGO_URI, MONGO_USERNAME, MONGO_PASSWORD), or
Mock the database connection in tests (recommended for CI), or
Use a local MongoDB instance, or
Temporarily hardcode a connection string (not recommended—see warning below).
Never commit real credentials. If you must hardcode a connection string for
local debugging, ensure it is removed before committing and never store
production secrets in source control.
Example of a hardcoded connection (for demo only — do not commit):
When the DB connection is satisfied (or tests are mocked), mocha exits with code 0 and produces a JUnit XML via mocha-junit-reporter, which CI systems can consume:
Running coverage uses nyc and enforces the threshold defined in package.json. If coverage is below the threshold, the command exits non-zero—useful to fail or gate CI pipelines:
This intentional failure is often used in CI to demonstrate how to stop or continue pipeline execution on coverage failures; Jenkins automation will show patterns for handling such failures.
$ npm start> Solar System@6.7.6 start> node app.jsServer successfully running on port - 3000(node:83049) [DEP0170] DeprecationWarning: The URL mongodb://... is invalid. Future versions of Node.js will throw an error.(Use `node --trace-deprecation ...` to show where the warning was created)
The app listens on port 3000 by default. Access it locally via http://localhost:3000 or on your VM using its IP and port 3000.
Examined the repository structure and key files (app.js, app.controller.js, client.js, tests, Dockerfile, OpenAPI)
Installed dependencies and run tests locally
Learned common failure modes (missing MONGO_URI) and mitigations
Collected code coverage using nyc
Started the app and verified the UI and basic endpoints
Next steps: automate these commands in a Jenkins pipeline to run installs, tests, coverage checks, and builds so you can validate the application automatically as part of CI/CD.