Mocha before and after hooks not executing

笑着哭i 提交于 2021-01-21 11:11:31

问题


I am trying to run a simple test on my local cassandra database to check if the select statement returns the correct no of records from the table. However, the code placed between the before and after blocks are not getting called. As a result my test simply fails.

var assert = require('assert');
var cassandra = require('cassandra-driver');
var async = require('async');

//Connect to the cassandra cluster, assuming that the keyspace & columnspace already exists
var client = new cassandra.Client({contactPoints: ['127.0.0.1'], keyspace: 'demo'}); 
describe('Cassandra is up and running', function() {
    before(function() {
        //Check if the connection got established successfuly 
        client.connect(function(err) {
            if(err){
                console.log("Oops, something went wrong : " + err);
            }
            console.log('I al here 1');
        });

        //Insert a few data to column space
        var queries = [
        {
        query: 'INSERT INTO users(key, name, emails) VALUES (?, ?, ?)',
        params: ['mick-jagger1', 'Sir Mick Jagger 1', 'mick1@company.com']
        },
        {
        query: 'INSERT INTO users(key, name, emails) VALUES (?, ?, ?)',
        params: ['mick-jagger2', 'Sir Mick Jagger 2', 'mick2@company.com']
        },
        {
        query: 'INSERT INTO users(key, name, emails) VALUES (?, ?, ?)',
        params: ['mick-jagger3', 'Sir Mick Jagger 3', 'mick3@company.com']
        },
        {
        query: 'INSERT INTO users(key, name, emails) VALUES (?, ?, ?)',
        params: ['mick-jagger4', 'Sir Mick Jagger 4', 'mick4@company.com']
        }
        ];

        client.batch(queries, { prepare: true }, function(err) {
            if(err){
                console.log("Oops, something went wrong : " + err);
            }
            console.log('Sample data inserted into column space');
        });
    })

    it('should return 4 when four rows of data are inserted into demo table', function() {
        var count = 0;
        client.execute('SELECT COUNT(*) FROM users', function(err, result) {
            count = result.rows[0];
        });
        assert.equal(4, count);
        console.log("I am here : " + count);
    })

    after(function() {
        client.execute('TRUNCATE users', function(err, result) {
            if(err){
                console.log("Oops, something went wrong : " + err);
            }
        });
    })
})

回答1:


Your test is performing asynchronous operations. You need to use a callback to tell mocha when your it/before/after functions have finished. For example:

before(function(done) {
    // ...
    client.batch(queries, {
        prepare: true
    }, function(err) {
            if (err) {
                console.log("Oops, something went wrong : " + err);
            }
            console.log('Sample data inserted into column space');
            done();
        });
});

and:

it('should return 4 when four rows of data are inserted into demo table', function(done) {
    var count = 0;
    client.execute('SELECT COUNT(*) FROM users', function(err, result) {
        count = result.rows[0];
        assert.equal(4, count);
        console.log("I am here : " + count);
        done();
    });
});



回答2:


ES6 update

You can now use async/await pattern for this case:

before(async () => {
        // ...
        try {
         await client.batch(queries, {
            prepare: true
          });
        } catch (err) {
        if(err) {
         console.log("Oops, something went wrong : " + err)
          }
        console.log('Sample data inserted into column space');
        }
    });

it('should return 4 when four rows of data are inserted into demo table', async () => {
      var count = 0;
      await client.execute('SELECT COUNT(*) FROM users', (err, result) => { 
       count = result.rows[0];
        assert.equal(4, count);
        console.log("I am here : " + count);
      });
    });


来源:https://stackoverflow.com/questions/30332305/mocha-before-and-after-hooks-not-executing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!