Leightweight agent
Light agent for coverage collection using Node V8 Coverage
Configuration
Cli argument can’t be used directly with a lightweight agent. It should be passed via file sl.conf file or via env var.
Env var should be prefixed by SL_
Default values
Name | Default value |
---|
tokenFile | sltoken.txt |
buildSessionIdFile | buildSessionId |
projectRoot | process.cwd(); |
Usage
Preloader script should be injected/installed beforehand. For example, into ./agent/lightweight.js
Define execution of lightweight agent before main script by setting env var NODE_OPTIONS="-r <path to agent>"
for instance NODE_OPTIONS="-r ./node_modules/lean-coverage-collector/preloader.js"
When you need define folder where coverage footprints will be stored. You can provide NODE_V8_COVERAGE env. Otherwise, default folder .sl-cov will be used.
By default, coverage listener will be executed in a child process but it can be configured
Run in the same process
Env var SL_IN_PROCESS_COVERAGE_COLLECTION
can be passed to run coverage listener in a same process with tested app. Also if you want everything to be executed in one process you also need to pass NODE_V8_COVERAGE explicitly. Otherwise, app will be executed in a separate process.
It will cause sending of extra hits because internal hits from agent will be sent. But it won't have impact on coverage accuracy.
Run in sidecar
Run in a sidecar allow to collect coverage from other processes via file system (even more from one at once). To do that agent should be started directly like npx lean-coverage-collector
. For correct work it require env NODE_V8_COVERAGE
to be defined. Also all processes that will provide coverage should call method v8.takeCoverage()
periodically.
To achive that, process can be executed with preloader and option SL_SKIP_PRELOADER=1
. In that case preloader main logic won't be executed but will be defined a timer to take coverage each 10 sec. It will prevent execution of preloader script and will set a timer for coverage snapshots generation.
WARN: Usage of preloader without SL_SKIP_PRELOADER=1
parameter will create new instance of agent that will conflict with a sidecar instance
Also separate script ./node_modules/lean-coverage-collector/coverage-collector.js
can be used. Just run it before execution via -r
option
Another option is to modify the itself and add import of coverage-collector.js
or just add next code:
cosnt v8 = require('v8');
const timer = setInterval(() => {
log('Get coverage');
v8.takeCoverage();
}, +process.env['SL_COVERAGE_INTERVAL']);
timer.unref();
Example:
Run of agent in a sidecar
export NODE_V8_COVERAGE=.sl-cov
npx lean-coverage-collector
OR
node ./node_modules/lean-coverage-collector/runner.js
Run of observable process
export NODE_V8_COVERAGE=.sl-cov
export SL_SKIP_PRELOADER=1
node -r ./node_modules/lean-coverage-collector/preloader.js ./dist/server.js
or
export NODE_V8_COVERAGE=.sl-cov
node -r ./node_modules/lean-coverage-collector/coverage-collector.js ./dist/server.js