Mocha如何知道在测试套件中首先加载哪个文件 [英] How Mocha knows which file to load first in the test suite

查看:109
本文介绍了Mocha如何知道在测试套件中首先加载哪个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MongodB学习A测试驱动方法。文件夹结构

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

A user.js 在src文件夹中测试

A user.js to test in the src folder

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
const Schema = mongoose.Schema;

const UserSchema = new Schema ({
    name: String
});

const User = mongoose.model('user', UserSchema);

module.exports = User;

内容 test_helper.js

const mongoose = require('mongoose');;

mongoose.connect('mongodb://localhost/users_test');

    mongoose.connection
    .once('open', () => {
        console.log('Connected to Mongo!');
        done()}) 
    .on('error', (error) => { 
        console.warn('Warning', error);
    });

create_test.js content

const assert = require('assert');
const User = require('../src/user');

describe('Creating records', () => {

    it('Saves a user', (done) => {
        const user = new User({ name: 'Ankur' });
        user.save()
                .then(() => {
                    assert(!user.isNew);
                    done();
                });

现在我运行 npm test 测试正在通过。

Now when i run npm test the test are getting passed.

Connected to Mongo!
  Creating records
    √ Saves a user (779ms)

但我怀疑<$ c $是怎么回事c> Mocha 知道首先运行 test_helper.js 文件。(同时将此文件命名为任何其他名称不会改变行为)。

But My doubt is how does Mocha knows to run the test_helper.js file first, Everytime. (Also naming this file to any other name doesn't change the behavior).

我也没有使用任何根级挂钩。

Also i'm not using any root-level hook.

我知道mocha递归加载文件在每个目录中,从根目录开始,因为这里的所有内容都只是一个目录,因此它不会产生任何差异在这里。

i know mocha loads files recursively in each directory, starting with the root directory, and since everything here is one directory only so its not making any difference here.

有人可以建议或帮助,Mocha如何确切地知道 test_helper.js (或任何具有相同内容的文件名)应首先运行。

Can someone please suggest or help, how does Mocha exactly know that test_helper.js (or any filename with the same content) should be running first.

推荐答案

没有默认值设置Mocha如何加载测试文件的顺序。

当Mocha 扫描目录以查找文件,它使用 fs.readdirSync 。此调用是 readdir(3)<的一个包装。 / code> ,它本身并不保证订单。现在,由于实施怪癖 fs.readdir的输出 fs.readdirSync 在Linux(通常可能是POSIX系统)上排序,但不在Windows上。此外,最终可能会删除Linux上的排序行为,因为文档说 fs.readdir 只是 readdir(3)而后者不保证订单。有一个很好的论据可以证明在Linux上观察到的行为是一个错误(请参阅我上面链接的问题)。

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).

请注意,有一个 - sort 选项将在Mocha找到它们之后对文件进行排序。但默认情况下这是关闭的。

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加载测试文件并执行它们。因此,位于文件顶层的任何内容都会立即执行 。这意味着 test_helper.js 中的代码会立即执行。每次调用 describe 都会立即执行其回调。 然而,调用 it 会记录测试以供以后执行。 Mocha在执行此操作时发现您的测试但不是立即执行

  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.

执行完所有文件后,Mocha开始运行测试。到目前为止, test_helper.js 中的代码已经运行,并且您的测试会从它创建的连接中受益。

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.

严重警告连接数据库是一种异步操作,目前没有什么可以保证中的异步操作test_helper.js 将在测试开始之前完成。它现在工作正常只是运气。

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.

如果这是我,我要么在挂钩之前将连接创建放在全局异步中。 (在任何测试文件中出现之前的全局 挂钩将在任何测试之前执行,甚至是其他文件中出现的测试。)或者我使用 - 延迟 和显式调用 run()以在保证建立连接后启动套件。

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.

这篇关于Mocha如何知道在测试套件中首先加载哪个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆