How Mocha knows which file to load first in the test suite

前端 未结 3 1380
抹茶落季
抹茶落季 2021-02-08 13:58

I\'m trying to learn the A Test Driven Approach with MongodB. The folder structure

A user.js to test in the src folder

const mongo         


        
相关标签:
3条回答
  • 2021-02-08 14:44

    It doesn't

    Tests should not have a specific order. All test suites should be working as standalone agnostic to other suites. Inside the suite you can use "before" and "beforeEach" (or "after", "afterEach") in-order to create setup and teardown steps.

    But if the order of the tests matters, something is broken in the design.

    0 讨论(0)
  • 2021-02-08 14:46

    There is a very easy way to load tests sequentially.

    Step 1 : Set up a test script in package.json: e.g.

    "scripts": {
        "test": "mocha ./tests.js"
      }
    

    Let us assume that tests.js is a file which defines the order to execute tests.

    require("./test/general/test_login.js");
    require("./test/Company/addCompany.js");
    ...
    ...
    

    So here test_login will run first and then others one by one.

    Step 2: Then run tests with:

    $ npm test
    
    0 讨论(0)
  • 2021-02-08 14:49

    There is no default set order to how Mocha loads the test files.

    When Mocha scans a directory to find files it it, it uses fs.readdirSync. This call is a wrapper around readdir(3), which itself does not guarantee order. Now, due to an implementation quirk the output of fs.readdir and fs.readdirSync is sorted on Linux (and probably POSIX systems in general) but not on Windows. Moreover, it is possible that the sorted behavior on Linux could eventually be removed because the documentation says fs.readdir is just readdir(3) and the latter does not guarantee order. There's a good argument to be made that the behavior observed on Linux is a bug (see the issue I linked to above).

    Note that there is a --sort option that will sort files after Mocha finds them. But this is off by default.

    The behavior you observe is explainable not merely by loading order but by execution order. Here is what happens:

    1. Mocha loads the test files and executes them. So anything that is at the top level of your file executes right away. This means that the code in test_helper.js executes right away. Every call to describe immediately executes its callback. However, calls to it record the test for later execution. Mocha is discovering your tests while doing this but not executing them right away.

    2. Once all files are executed, Mocha starts running the tests. By this time, the code in test_helper.js has already run and your test benefits from the connection it has created.

    Major warning Connecting to a database is an asynchronous operation, and currently there is nothing that guarantees that the asynchronous operation in test_helper.js will have completed before the tests starts. That it works fine right now is just luck.

    If this were me, I'd either put the connection creation in a global asynchronous before hook. (A global before hook appearing in any test file will be executed before any test whatsoever, even tests that appear in other files.) Or I'd use --delay and explicitly call run() to start the suite after the connection is guaranteed to be made.

    0 讨论(0)
提交回复
热议问题