0

I want to run my program and afterwards run tests to see if everything was done properly. I am running npm start command inside before function, but it does not work.

I have checked and tried Async function in mocha before() is alway finished before it() spec? as such:

return new Promise((resolve) => {
   execa('npm start')
   process.chdir('../Project')
   console.log(process.cwd());
   resolve()
});

and

return new Promise((resolve, reject) => {
  execa('npm start')
    .then(function() {
      process.chdir('../Project')
      console.log(process.cwd());
      resolve()
    })
    .catch(function(err) {
      console.log('Error')
      return reject(new Error(err))
    })
});

However console.log is executing before npm start finishes.

I have also checked In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded and at the moment in my package.json file test start like this: "test": "mocha --timeout 30000". This does not help.

My code:

npm start executes following code:

try {
  process.chdir('../')
  var packages = ['webpack']
  var args = ['install', '--save', '--save-exact']

  let exec = args.concat(packages, ['--verbose'])

  new Promise(function(resolve, reject) {
    execa('npm', exec)
    .then(function() {
      console.log(`Current directory: ${process.cwd()}`);
      console.log(`Execa instaled: `, exec);
      return execa('npm', ['install'])
    })
    .then(function() {
      console.log('Success. Installed packages in', dir);
      resolve()
    })
    .catch(function() {
      console.log('Error')
      return reject(new Error('npm installation failed'))
    })
  })
} catch (err) {
  console.error(`chdir: ${err}`);
}

Out of all things I tried mocha is either does not wait for the script to finish or the script finishes and then mocha is throwing Error: Timeout of 30000ms exceeded.

How do I do it properly? I expect that this is my misunderstanding of the Promises.

Gleb Eliseev
  • 65
  • 11
  • If you can let do it to Jenkins(ci) with a simple pipeline: 1) npm install 2) run linting 3) run all tests 4) save the artifact – Vash72 May 27 '19 at 20:17
  • @Vash72 Not really. I am trying to create a program that is tested beforehand, during a development stage. Probably even using a BDD approach – Gleb Eliseev May 29 '19 at 14:07

0 Answers0