如何 运行 Mocha 以定义的顺序进行测试

How to run Mocha tests in a defined order

背景

我正在 Node.js 开发一个程序,并使用 Chai 和 SinonJS 在 Mocha 中编写我的测试套件。我有核心图形模块,它控制对 node-webgl 上下文的访问。

由于 node-webgl 的工作方式,我只想为整个测试 运行 初始化一次上下文。在核心模块初始化之前,我希望 运行 进行一些测试,例如:

describe('module:core', function () {
    describe('pre-init', function () {
        describe('.isInitialized', function () {
            it('should return false if the module is not initialized', function () {
                expect(core.isInitialized()).to.be.false;
            });
        });
        describe('.getContext', function () {
            it('should error if no context is available', function () {
                expect(function () {
                    core.getContext();
                }).to.throw(/no context/i);
            });
        });
    });
    describe('.init', function () {
        it('should error on an invalid canvas', function () {
            expect(function () {
                core.init(null);
            }).to.throw(/undefined or not an object/i);
            expect(function () {
                core.init({});
            }).to.throw(/missing getcontext/i);
        });
        it('should error if the native context could not be created', function () {
            var stub = sinon.stub(global._canvas, 'getContext').returns(null);
            expect(function () {
                core.init(global._canvas);
            }).to.throw(/returned null/i);
            stub.restore();
        });
        it('should initialize the core module', function () {
            expect(function () {
                core.init(global._canvas);
            }).not.to.throw();
        });
    });
    describe('post-init', function () {
        describe('.isInitialized', function () {
            it('should return true if the module is initialized', function () {
                expect(core.isInitialized()).to.be.true;
            });
        });
        describe('.getContext', function () {
            it('should return the current WebGL context', function () {
                var gl = null;
                expect(function () {
                    gl = core.getContext();
                }).not.to.throw();
                // TODO Figure out if it's actually a WebGL context.
                expect(gl).to.exist;
            });
        });
    });
});

然后我可以运行剩下的测试。

问题

当我通过 Mocha 运行 时,一切都很好,因为核心测试套件是第一个 运行。我担心的是,如果任何测试套件在核心测试套件之前获得 运行,那么这些测试套件将失败,因为核心尚未初始化。

确保核心测试套件始终运行在任何其他测试套件之前的最佳方法是什么?

按照文档中的说明使用 before()。

describe('hooks', function() {
    before(function() {
        // runs before all tests in this block
    });
    ......
});

前面的函数将 运行 首先,然后是他描述的所有其他内容。

希望这对您有所帮助。

最后我重构了我的代码以允许在不影响 node-webgl 的情况下拆除核心模块并使用 before 块对其进行初始化,如下所示:

// Run this before all tests to ensure node-webgl is initialized
before(function () {
    if (!global._document) {
        global._document = WebGL.document();
        global._document.setTitle('Machination Graphics Test Suite');
    }
    if (!global._canvas) {
        global._canvas = global._document.createElement('canvas', 640, 480);
    }
});

describe('test suite goes here', function () {
    // Ensure core is ready for us before testing (except when testing core)
    before(function () {
        if (!core.isInitialized()) {
            core.init(global._canvas);
        }
    });
    // Tear down core after all tests are run
    after(function () {
        core.deinit();
    });
    ...
});