Skip to content

Nodejs Testing Tips

Posted on:November 22, 2023 at 12:00 AM

How to setup tests in Nodejs project?

How to write integration tests using databases in Nodejs?

How to test CLI commands in Nodejs?

// Execute CLI command
var exec = require('child_process').exec; 

exec('bash cmd', {cwd: dir}, function(error, stdout, stderr) {
  // Assert exitCode, stdout, stderr
});

How to turn JS code using require to import for tests?

// Install esm
npm install --save-dev esm 

// Use esm to run tests
npm test -- --require esm 

ref: