Mocha - start and resolve two async / await calls

It's hard for me to work. I cannot start the second asynchronous call:

it.only('can delete an S3 bucket for a given PR', async() => {
  let result
  const options = { branch: '11'}
    // this runs and completes just fine (as you can see in the webstorm pic)
    await Deploy.createPRBucket(options, (error, stdout, stderr) => {
      console.log(`created error: ${error}`)
      console.log(`created stdout: ${stdout}`)
      console.log(`created stderr: ${stderr}`)
      result = JSON.parse(stdout)
      console.log("added")
    })

    // it never hits my console log and bombs before that right after the createPRBucket call resolves
    console.log("deleting...")
    await Deploy.deletePRBucket(options.branch, (error, stdout, stderr) => {
      console.log(`deleted error: ${error}`)
      console.log(`deleted stdout: ${stdout}`)
      console.log(`deleted stderr: ${stderr}`)
      expect(stdout).to.exist
    })
})

      

Code :

export async function createPRBucket (options, callback){
  try {
    await exec(`aws s3api create-bucket --bucket ${options.branch} --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2`,
      (error, stdout, stderr) => {
      console.error(`exec error: ${error}`);
      console.log(`stdout: ${stdout}`);
      console.log(`stderr: ${stderr}`);

      callback(error, stdout, stderr)
      // process.exit(0)
    })
    let error, stdout, stderr

  }
  catch(err) {
    console.log(`there was a problem creating this bucket: ${err}`)
    // process.exit(1)
  }
}

export async function deletePRBucket(branch, callback){

    await exec(`aws s3 rb s3://xxx-${branch} --force`,
      (error, stdout, stderr) => {
        console.error(`exec error: ${error}`);
        console.log(`stdout: ${stdout}`);
        console.log(`stderr: ${stderr}`);

        callback(error, stdout, stderr)
        // process.exit(0)
      })
}

      

That's what I see in WebStorm: Note . The picture shows that I have and , but pulled them out as I am using . Either way, I end up with the same problem, regardless of the same error.enter image description here (done) =>

done()

async () =>

Also note that I can put a breakpoint in the callback of the first pending call (createPRBranch) in my test and it gets it just fine. But if I put a breakpoint in the second call (wait for deletePRBranch), the second never hits and that call is not resolved.

+3


source to share


1 answer


considering exec is util.promisifed child_process.exec

it.only('can delete an S3 bucket for a given PR', async() => {
    let result;
    const options = { branch: '11' };
        // this runs and completes just fine (as you can see in the webstorm pic)
    let {stdout, stderr} = await Deploy.createPRBucket(options);
    console.log(`created error: ${error}`);
    console.log(`created stdout: ${stdout}`);
    console.log(`created stderr: ${stderr}`);
    result = JSON.parse(stdout);
    console.log("added");
    console.log("deleting...");

    ({stdout, stderr} = await Deploy.deletePRBucket(options.branch));
    console.log(`deleted error: ${error}`);
    console.log(`deleted stdout: ${stdout}`);
    console.log(`deleted stderr: ${stderr}`);
    expect(stdout).to.exist
})

      

Exported functions can be simple - note, not necessary async

, since they return already promised

export function createPRBucket(options) {
    return exec(`aws s3api create-bucket --bucket ${options.branch} --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2`);
}

export function deletePRBucket(branch) {
    return exec(`aws s3 rb s3://xxx-${branch} --force`);
}

      

Promise returned by promisified resolves

{ stdout: "...", stderr: "..." }

      



Note. I have not tested the error condition exec

- error object

{ 
  killed: true or false, 
  code: a number?, 
  signal: null (not sure what else can be here, 
  cmd: "the original command line that was run"
}

      

explanation

({stdout, stderr} = await Deploy.deletePRBucket(options.branch));

      

(..) syntax is required around the assignment operator when using unclassified object literal destruct assignment.

{a, b} = {a: 1, b: 2} the stand-alone syntax is invalid because the {a, b} on the left is considered a block and not an object literal.

However, ({a, b} = {a: 1, b: 2}) is true, as is var {a, b} = {a: 1, b: 2}

NOTE. Your expression (..) must be preceded by a semicolon, or it can be used to execute the function on the previous line.

source: MDN Docs

+2


source







All Articles