mocha share variables between files

孤街醉人 提交于 2021-01-27 23:10:32

问题


I am trying to wire the objects between 2 mocha test files. Here is my test1.js file which should export a variable once all the test cases are executed.

var assert = require('assert');


var newUser = {
    email: "test@ex.com",
    name: "test@ex.com",
    password: "test@ex.com",
    confirmPassword: "test@ex.com"
}


var studentAcademicData = {
    marks: {},
    activities: {}
}

var studentInterests = []

var testSummary = {},
    loggedInUser = {},
    avaialbleAssessment = {},
    test = {},
    interests = {};

var studentAcademicId, studentId, academicYearId, assessmentId, testId;



describe('perform functional Test', function() {

    before(function() {
        this.timeout(15000);
        db.init(config.mongodb);
    })

    //Register a student    it ('Register a student', function(done){

    StudentController.register(newUser).then(function(data) {
        assert.equal(data.name, newUser.name) assert.equal(data.tenant, newUser.tenant) assert.equal(data.customerType, newUser.customerType)

        done();
    }).catch(done)
});

//User authentication   it ('Authenticates user', function(done){


var userInfo = {
    appId: "abc",
    email: newUser.email,
    password: newUser.password
}

security.userAuthenticate(userInfo).then(function(data) {

securityToken = data.securityToken;
tenantId = data.tenantId;
emailStatus = data.emailStatus;
mobileStatus = data.mobileStatus studentId = data.userId;

done();
}).catch(done)
});


it('Gets Student by id', function(done) {

StudentController.getById(studentId).then(function(data) {

    loggedInUser = data;
    loggedInUser.tenantId = 'abc';
    loggedInUser.userId = studentId;
    loggedInUser.securityToken = securityToken;

    done();
}).catch(done)
});

})

module.exports.testUser = {
    loggedInUser: loggedInUser,
    avaialbleAssessment: avaialbleAssessment,
    interests: interests,
    testSummary: testSummary,
    studentAcademicData: studentAcademicData,
    newUs
    er: newUser,
    test: test
};

Here is my test2.file which imports the object from test1.js file

var assert = require('assert');
var rewire = require('rewire');

var TestUserObj = require('./test1');


describe('perform test2 Test', function() {

    console.log("in test2")

    console.log("TestUserObj ::::" + JSON.stringify(TestUserObj))

});

The output i get in test2.js file is

TestUserObj

    ::::{
        "testUser": {
            "loggedInUser": {},
            "avaialbleAssessment": {},
            "interests": {},
            "testSummary": {},
            "newUser": {
                email: "test@ex.com",
                name: "test@ex.com",
                password: "test@ex.com",
                confirmPassword: "test@ex.com"
            },
            "test": {}
        }
    }

the exported values does not not contain the modified objects


回答1:


As @sheplu mentioned above in the comments, test files in unit testing should be separate. In fact, each individual unit being tested should be independent of other units.

What you are looking for in your case, is a buildup and teardown system, or fixtures.

Basically, you need to make sure that you have the required items already set up before you run a set of tests.

For this you can look into the following:

Buildup phase:

  • Create a set of dummy data, and quickly seed it into your database
  • Implement a way to simulate logged in user
  • Make use of Mocha's before() and/or beforeEach() hooks to do this
  • If you are using MongoDB with mongoose, mockgoose provides an excellent package to simulate a test database in memory

Teardown phase:

After test cases are done, you should also take care to:

  • Log out the user
  • Remove the data from db
  • Use Mocha's after() and/or afterEach() hooks

You can also check out supertest to make API request calls in test cases.

Doing the above in your second file would ensure that you always have a working set of data to run your tests with, within specific test suite.



来源:https://stackoverflow.com/questions/45811000/mocha-share-variables-between-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!