Rajesh Rajesh - 8 months ago 75
Node.js Question

How to mock python and shell scripts in nodejs

I've to run python and shell scripts in my node JS code. But to begin with, i've to mock the script calls. Is there any ways i can mock this python and shell scripts and send my expected response? Kindly share me your thoughts.

Below is the Code i use to call the script. When the PythonShell.run gets executed, i want to mock results and return my preferred response.

function callScript(){
var options = {
mode: 'text',
pythonPath: 'path/to/python',
pythonOptions: ['-u'],
scriptPath: 'path/to/my/scripts',
args: ['value1', 'value2', 'value3']

PythonShell.run('my_script.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);


with sinon i am able to mock the script call(not only script, we can mock any complex logic) and send expected json repsonse.

var sinonSandbox =  sinon.sandbox.create();

sinonSandbox.stub(PythonShell,'run').withArgs(sinon.match.any, sinon.match.any, sinon.match.any).yields(<Json object>)

after execution: