Lesson 16 · Node.js on IBM i — From RPG Developer to Modern IBM i Developer
Test the Tools Rental API
Use Node.js's built-in test runner and Fastify injection to verify routes without opening a network port or changing Db2 data.
A useful test does more than prove that the server starts. It protects behavior we care about: available tools can be listed, bad requests are rejected, and a valid rental reaches the business operation.
We do not want a unit test to change real Db2 rows. First, make the server accept a database implementation.
Make the dependency replaceable
This takes three small edits in src/server.js.
First, remove the top-level database import:
// Before
const {
findToolById,
listAvailableTools,
listOpenRentals,
rentTool,
returnTool
} = require('./db');
// No top-level database import is needed.
Second, let buildServer receive its database and its logging choice:
// Before
function buildServer() {
const server = Fastify({ logger: true });
// After
function buildServer({ db = require('./db'), logger = true } = {}) {
const server = Fastify({ logger });
Third, put db. in front of each of the five database calls inside the routes:
listAvailableTools() → db.listAvailableTools()
findToolById(request.params.toolId) → db.findToolById(request.params.toolId)
rentTool(request.body) → db.rentTool(request.body)
listOpenRentals() → db.listOpenRentals()
returnTool(request.params.rentalId) → db.returnTool(request.params.rentalId)
Nothing else in the routes changes. The complete src/server.js is printed at the end of the next lesson if you want to compare your file.
Production loads the real module when no db is supplied. Tests supply a fake database, so the default expression is never evaluated and the IBM i-native connector is not loaded on the workstation. A test can also switch the request log off to keep its output readable.
Create the first tests
Create test/server.test.js:
const test = require('node:test');
const assert = require('node:assert/strict');
const { buildServer } = require('../src/server');
function createFakeDb() {
return {
async listAvailableTools() {
return [{
TOOL_ID: 1,
NAME: 'Cordless Drill',
CATEGORY: 'DRILL',
DAILY_RATE: 14,
STATUS: 'AVAILABLE'
}];
},
async findToolById(toolId) {
return toolId === 1
? { TOOL_ID: 1, NAME: 'Cordless Drill' }
: null;
},
async listOpenRentals() {
return [];
},
async rentTool() {},
async returnTool() {}
};
}
function buildTestServer() {
return buildServer({ db: createFakeDb(), logger: false });
}
test('GET /tools returns available tools', async () => {
const server = buildTestServer();
const response = await server.inject({
method: 'GET',
url: '/tools'
});
assert.equal(response.statusCode, 200);
assert.equal(response.json()[0].NAME, 'Cordless Drill');
await server.close();
});
test('GET /tools/:id returns 404 when missing', async () => {
const server = buildTestServer();
const response = await server.inject({
method: 'GET',
url: '/tools/999'
});
assert.equal(response.statusCode, 404);
await server.close();
});
test('POST /rentals rejects an incomplete body', async () => {
const server = buildTestServer();
const response = await server.inject({
method: 'POST',
url: '/rentals',
payload: { toolId: 1 }
});
assert.equal(response.statusCode, 400);
await server.close();
});
server.inject passes a request through Fastify without listening on a TCP port. The test is fast and can run on a developer workstation or IBM i.
Add the test command
"scripts": {
"start": "node src/server.js",
"dev": "node --watch src/server.js",
"test": "node --test"
}
Run it:
npm test
node --test finds files whose names end in .test.js and runs them. A passing run ends with a summary that includes these counts:
tests 3
pass 3
fail 0
To see a test do its job, change 'Cordless Drill' to another name in the first assertion and run npm test again. The summary reports one failure and shows the expected and actual values. Restore the name before continuing.
Add integration tests separately
These tests replace Db2, so they do not prove that SQL procedures and authorities are correct. Add a smaller integration suite for a dedicated test schema and run it in a controlled environment. It should create known data, exercise a rental and return, verify the rows, and clean up after itself.
Do not point automated destructive tests at production. Make the target schema explicit and make the test refuse to run when required test configuration is missing.
Commit the passing tests. From now on, npm test becomes a required step before deployment.
References
Documentation and references used for this lesson.

Comments
Share your thoughts, questions, or real-world IBM i experiences related to this article.