0

私が書いているテストで、次の内部コールバックで再び非同期地獄に遭遇しました。待機しないコールバックについてコメントしました。関数をマーシャリングするために async.series と、内部反復を同期させるために async.each の両方を使用しています。Mocha compalins "done() が複数回呼び出されました" - コードが待機していないのはなぜですか?

describe('Report Generation (R subsystem)', function () {
  before(function (done) {
    //clear test files
    async.series([function (callback) { //1st
        console.log('Delete local test files');
        _.each(output_file_template, function (test_file) {
            if (fs.existsSync(__dirname + '/../reports/' + test_file + user_file_code + '.png')) {
                fs.unlinkSync(__dirname + '/../reports/' + test_file + user_file_code + '.png');
            };
        }); //..._.each
        callback();
    }, function (callback) { //2nd
        console.log('Delete remote test files');
        async.each(output_file_template, function (test_file, cb) {
            console.log(test_file);
            s3.del('reports/' + test_file + user_file_code + '.png', function (err, res) {
                console.log("Delete err", err);
                console.log("Delete result", res);
                cb();

            }, function(err) {callback(err);}); //s3.head

        }); //...async.each

    }], function (err, res) { //3rd
        done(); //this should tell the begin() clause to complete
    }); //...async.series

    }); //...before
    it('should not start this test until before() has finished!', function (done) {
        console.log("1st test here");

    });
});
4

1 に答える 1

3

、私が見ることができるのは、async.series3つの関数の配列でやっているが、最後に制御関数がないことです。あなたのコードはit('should not start...そこにあるべきものだと思います。

だから(私は思う)あなたのコードは次のようになるはずです:

describe('My Test 1', function () {
    //clear test files
    async.series([
        function (callback) { //1st
            console.log('Delete local test files');
            ...
            callback();
        },
        function (callback) { //2nd
            console.log('Delete remote test files');

            async.each(
                output_file_template, 
                function (test_file, cb) {
                    console.log(test_file);
                    s3.del('reports/' + test_file + user_file_code + '.png', function (err, res) { //I can't get the following nested callback to wait
                        console.log("Delete err", err);
                        console.log("Delete result", res);
                        cb();
                    });  //...s3.head
                },
                function( err ) {  // this is the control function for async.each, so now it waits after all s3 have finished
                   callback( err );
                }
            ); //...s3.head
        },
        function (callback) { //3rd -> will be called now after the async.each (I don't think, you use it so can be deleted anyway)
            callback();
        }
    ],
    function( err, result ) {
        done();  // whatever this has to do with the last "it" -> here is the point, where the "before" is completely done
    }
});

ソースをテストしていないので、中にタイプミスがあるかもしれませんが、絵を示していると思います。

于 2013-06-18T16:52:52.983 に答える