How to combine mocha with inquirer.js

171 views Asked by At

I am looking to make my automation tests a bit more flexible. I have a QA team that does not know much javascript and possibly have to design the tests for users with no or little programming skills.

I had a few scripts created using mocha test framework and spectron.js (for an app built with electrion.js) test a few features of the product I dont want to run every single test every time I run the script. My temporary solution is bundle the tests in to a function as a "suite". Like this -

function DiagnosticSuite(location, workstation, workflowName){
    CreateWorkflow(location, workflowName); 
    SetWorkFlowToStation(location, workstation, workflowName);
    DiagnosticTestFlow();
    return;
}

function PowerflowSuite(imei, location, workstation, workflowName){
    SetWorkFlowToStation(location, workstation, workflowName);
    powerOffFlow(imei);
    return;
}

I was thinking of using Inquirer and use a conditional based on input to run one of the tests above. Like this -

inquirer.prompt([
    {
        type: 'list',
        name: 'Which workflow do you want to run?',
        choices: ['Power Off', 'Diagnostic']
    }
]).then((answers) => { 
     if(answers == 'Power Off'){
          PowerflowSuite(imei, location, workstation, workflowName);
     }   
})

How ever when I test that Mocha seems to not wait for the user input from inquerior to run the tests and I get an output like this -

$ npm test

> [email protected] test C:\Users\DPerez1\Desktop\metis-automation
> mocha

? Which workflow do you want to run?: (Use arrow keys)
> Power Off
  Diagnostic

  0 passing (0ms)

Seems like it runs and doesnt see a any tests and finishes and when I select the answer the program just closes.

I am wondering why Mocha does this and if its possible to run my existing mocha scripts with a library like inquirer.

1

There are 1 answers

0
perez6736 On

So I found a solution to my problem in case anyone stumbles here and is wondering.

I separated the CLI portion and the Mocha portion into different scripts on the package.json file. That way I can use nodes child process library to run the mocha part and pass the information from the CLI part via the process.argv objject.

So the CLI part will ask me what test to run, what env, and what user. And I create a command to put in child process exec function (I think there are others to use but not important.) When mocha tests run I parse the argvs and pass them into the functions so that the test can run based on that.