I'm utterly confused on how to wrap nested async callbacks in a Mocha test. Here is the offending code sample: It's calling Amazon S3 to check that files exist:
var should = require('should');
var appConfig = require("../config.js");
var fs = require('fs');
var async = require('async');
var s3 = require('aws2js').load('s3', appConfig.awsAccessKeyId, appConfig.awsSecretAccessKey);
s3.setBucket(appConfig.awsBucketName);
var test_user_id = 590170571;
var user_file_code = test_user_id * 2;
var output_file_template = ['wordcloud_r', 'polarity_r', 'emot_cat_r'];
describe('Should show uploaded files to amazon s3', function () {
it.only('should upload three local graphics files to Amazon S3 bucket', function (done) {
async.each(output_file_template, function (test_file, cb) {
console.log(test_file);
s3.head('reports/dsfsdf.dff', function (err, res) {
if (err) {
console.log(err)
}
console.log(res)
cb(null);
// done(); //nope
});
// done(); //nope
});
// done(); //nope
});
});
Either code hangs waiting to complete (if I omit done() ) - or, the code completes without callbacks, or, node complains that done() was called multiple times.
With the help below, I sort of got it working, but it looks like asynchronous voodoo stew
it.only('should upload three local graphics files to Amazon S3 bucket', function (done) {
async.series([function (callback) {
async.each(output_file_template, function (test_file, cb) {
console.log(test_file);
s3.head('reports/dsfsdf.dff', function (err, res) {
if (err) {
console.log(err)
}
console.log(res)
cb();
callback();
});
});
}, function (callback) {
done();
callback();
}]);
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…