Running Backend Server
To collect code coverage for a Node.js backend application, the SeaLights Node.js agent can be attached in one of two ways:
Non-Intrusive Approach (Using
preload.js
withNODE_OPTIONS
)Wrapper Command (Using
slnodejs run
)
Once your application starts with the agent, it should appear under Live Agents Monitor in the SeaLights Cockpit.
Non-Intrusive Approach (Using preload.js
with NODE_OPTIONS
)
preload.js
with NODE_OPTIONS
)This method is ideal when you cannot or prefer not to modify the application’s execution command, such as in Docker containers, pm2
, or CI environments.
The SeaLights agent provides a preload.js
script that can be injected into the process using the NODE_OPTIONS
environment variable.
export SL_tokenfile=./sltoken.txt
export SL_buildsessionidfile=buildSessionId
export SL_labid=my_lab_id
export NODE_OPTIONS="--require ./node_modules/slnodejs/lib/preload.js"
node ./server/app.js
💡 Note: This method also works with TypeScript apps run via
ts-node
, e.g.,ts-node ./server/app.ts
. No special handling is needed beyond settingNODE_OPTIONS
correctly.
Wrapper Command (Using slnodejs run
)
slnodejs run
)This method wraps the application’s startup command using the SeaLights CLI runner. It gives you full control over parameters and visibility into agent behavior.
Generic Node.js App
npx slnodejs run --tokenfile ./sltoken.txt --buildsessionidfile buildSessionId --labid my_lab_id --workspacepath ./server --useinitialcolor true -- ./server/app.js
TypeScript App Using ts-node
ts-node
SeaLights only supports ts-node
apps when started with: node -r ts-node/register ./server/app.ts
Wrap this format with the agent:
npx slnodejs run --tokenfile ./sltoken.txt --buildsessionidfile buildSessionId --labid my_lab_id --workspacepath ./server --useinitialcolor true --useslnode2 -- -r ts-node/register ./server/app.ts
Additional Notes
To verify the agent is running, you can run the following command:
ps -ef | grep slnodejs
We recommend using the
--labid
parameter (orSL_labid
for preload) to correlate your test and listener sessions. Iflabid
is not set, the session ID will be used automatically.For full parameter details, see the Node.js Command Reference.
Last updated
Was this helpful?