How do I write Mocha tests dependent on other Mocha tests?

时间:2017-11-08 21:57:55

标签: node.js express testing mocha chai

I have a series of tests I'm trying to run on an Express REST API, emphasis on the word series, because they need to be sequential to some degree. Let me explain:

Tests, in order:

  1. API is accessible?
  2. API connects and authenticates properly?
  3. Auth tokens can then be used to write to the data store?
  4. Auth tokens can then be used to retrieve data just written to data store?
  5. Auth tokens can then be used to change data written (test if it worked with another retrieve)?

Note that these steps really only make any sense if they happen in order, and yet Mocha doesn't seem to run tests in order. I could make one test that runs Step 1, then another that runs Steps 1-2, then another that runs Steps 1-3, and so on, but that's definitely not DRY. I could also try to set up a chain of before and afters, but that doesn't seem up to conventions.

Is there a proper way of running Mocha tests checking sequential steps in an process?

I'm trying to get rid of this pattern of just adding additional steps to repeated tests...

describe('Api', () => {
    it('should be accessible', (done) => {
        // Try to connect:
        Api.connect((error, conn) => {
            done(error);
        });
    });

    it('should connect and authenticate properly', (done) => {
        // Try to connect:
        Api.connect((error, api) => {
            if (error) done(error);
            // Then try to authenticate:
            api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) => {
                done(error);
            });
        });
    });

    it('should allow for data to be written to the data store', (done) => {
        // Try to connect:
        Api.connect((error, api) => {
            if (error) done(error);
            // Then try to authenticate:
            api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) => {
                if (error) done(error);
                // Then try to write data:
                conn.write(generateTestData(), (error, res) => {
                    done(err);
                });
            });
        });
    });

    it('should allow for written data to be read from the data store', (done) => {
        // Try to connect:
        Api.connect((error, api) => {
            if (error) done(error);
            // Then try to authenticate:
            api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) => {
                if (error) done(error);
                let testData = generateTestData();
                // Then try to write data:
                conn.write(testData, (error, res) => {
                    if (error) done(error);
                    // Then try to read data:
                    conn.readLast((error, res) => {
                        if (error) done(error);
                        assert.equal(testData, res);
                    });
                });
            });
        });
    });
});

1 个答案:

答案 0 :(得分:0)

Mocha测试是连续运行的,但是,如果出现故障,它会继续进行下一次测试,因此看起来它们并行运行。

来自https://mochajs.org/

  

Mocha测试连续运行,允许灵活和准确   报告,同时将未捕获的异常映射到正确的测试用例

据说你可以在第一次失败后停止,但我无法让这个标志起作用:

  

-b, - 首次测试失败后保释 -

测试之间传递状态的示例:

describe('Api', () => {
  var api, conn;

  it('should be accessible', (done) => {
    // Try to connect:
    Api.connect((error, new_api) => {
      if (error) {done(error);} else {
        api = new_api
        done();
      }
    });
  });

  it('should authenticate properly', (done) => {
    // Then try to authenticate:
    api.authenticate(TEST_AUTH_CREDENTIALS, (error, new_conn) => {
      if (error) {done(error);} else {
        conn = new_conn;
        done();
      }
    });
  });

  it('should allow for data to be written to the data store', (done) => {
    // Then try to write data:
    conn.write(generateTestData(), (error, res) => {
      if (error) {done(error);} else {
        done();
      }
    });
  });
});