首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mocha -哪个先运行,beforeEach还是之前?

Mocha -哪个先运行,beforeEach还是之前?
EN

Stack Overflow用户
提问于 2016-04-21 05:48:38
回答 1查看 2.1K关注 0票数 4

我有一个web应用程序,它的规范如下:

代码语言:javascript
运行
复制
describe('Hook them up', () => {

    var server;

    beforeEach(done => {
        server = app.listen(done);
    });

    before(done => {
       // Does this run before or after "beforeEach"
       // If I try to access the api at this point I get an ECONNREFUSED
    });

    after(done => {
        server.close(done);
    });


    it('should set the \'createdAt\' property for \'DndUsers\' objects', done => {
        api.post('/api/tweets')
            .send({ text: 'Hello World' })
            .then(done)
            .catch(err => {
                console.log(err);
                done();
            });
    });
});

在我的其他项目中,如果我尝试访问before块中的api,它就能正常工作,就好像beforeEach已经在运行一样。

EN

Stack Overflow用户

发布于 2016-04-21 06:04:27

See my answer here回答了一个非常类似的问题。

Mocha的test runner在Hooks section of the Mocha Test Runner中最好地解释了这个功能。

在Hooks部分中:

代码语言:javascript
运行
复制
describe('hooks', function() {

    before(function() {
        // runs before all tests in this block
    });

    after(function() {
        // runs after all tests in this block
    });

    beforeEach(function() {
        // runs before each test in this block
    });

    afterEach(function() {
        // runs after each test in this block
    });

    // test cases
    it(...);  // Test 1
    it(...);  // Test 2
});

您可以将这些例程嵌套在其他describe块中,这些代码块也可以包含之前/beforeEach例程。

这应该会给你

代码语言:javascript
运行
复制
hooks
    before
        beforeEach
            Test 1
        afterEach
        beforeEach
            Test 2
        afterEach
    after
票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36755836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档