Reputation: 20252
I am having a tough time getting this working. I'm not able to get the second async call to run:
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
})
})
The 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)
})
}
Here's what I see in WebStorm:
Note: The picture shows that I have
(done) =>
and done()
but took those out since I'm using async () =>
. Either way I arrive at the same problem, same error regardless.
Also Note that I can put a breakpoint inside the callback of the first await call (createPRBranch) in my test and it gets it just fine. But if I put a breakpoint in the second call (await for deletePRBranch), the second never gets hit, nor does that call resolve.
Upvotes: 3
Views: 431
Reputation: 1
given that exec is a 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
})
The exported functions can simply be - note, no need for async
as they return a Promise already
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`);
}
The promise returned by the promisified exec resolves to
{ stdout: "...", stderr: "..." }
Note: I haven't tested for error condition of
exec
- the error object is
{
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 of
({stdout, stderr} = await Deploy.deletePRBucket(options.branch));
The ( .. ) around the assignment statement is required syntax when using object literal destructuring assignment without a declaration.
{a, b} = {a: 1, b: 2} is not valid stand-alone syntax, as the {a, b} on the left-hand side is considered a block and not an object literal.
However, ({a, b} = {a: 1, b: 2}) is valid, as is var {a, b} = {a: 1, b: 2}
NOTE: Your ( ..) expression needs to be preceded by a semicolon or it may be used to execute a function on the previous line.
source: MDN Docs
Upvotes: 2