Wednesday, 5 April 2017

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

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

enter image description here

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;

Content of 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();
                });

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

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

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.

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.

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.



via Ankur Anand

No comments:

Post a Comment