How can I build my test suite asynchronously?

后端 未结 3 1536
鱼传尺愫
鱼传尺愫 2020-11-28 17:16

I\'m trying to create mocha tests for my controllers using a config that has to be loaded async. Below is my code. However, when the mocha test is run, it doesn\'t run any

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 17:37

    The problem with using the --delay command line flag and run() callback that @Louis mentioned in his accepted answer, is that run() is a single global hook that delays the root test suite. Therefore, you have to build them all at once (as he mentioned), which can make organizing tests a hassle (to say the least).

    However, I prefer to avoid magic flags whenever possible, and I certainly don't want to have to manage my entire test suite in a single global run() callback. Fortunately, there's a way to dynamically create the tests on a per-file basis, and it doesn't require any special flags, either :-)

    To dynamically create It() tests in any test source file using data obtained asynchronously, you can (ab)use the before() hook with a placeholder It() test to ensure mocha waits until before() is run. Here's the example from my answer to a related question, for convenience:

    before(function () {
        console.log('Let the abuse begin...');
        return promiseFn().
            then(function (testSuite) {
                describe('here are some dynamic It() tests', function () {
                    testSuite.specs.forEach(function (spec) {
                        it(spec.description, function () {
                            var actualResult = runMyTest(spec);
                            assert.equal(actualResult, spec.expectedResult);
                        });
                    });
                });
            });
    });
    
    it('This is a required placeholder to allow before() to work', function () {
        console.log('Mocha should not require this hack IMHO');
    });
    

提交回复
热议问题