test/
├── TestOne.js
└── TestTwo.js
Say, TestOne.js is :
This test case reads from file InputOne.json and add one record to the object which already has 3 records.
describe('Add Items', function () {
it('Should add items', function () {
var input = require('./data/InputOne');
var obj = new Department();
var result = obj.AddDept(input);
result.should.have.length(4);
});
});
Say, TestTwo.js is :
This test case reads from file InputOne.json and count the no of elements
describe('Count Items', function () {
it('Should count items length', function () {
var input = require('./data/InputOne');
var obj = new Department();
var result = obj.CountDept(input);
result.should.have.length(3);
});
});
Problem:
Both the test cases uses same file as input. If I run test cases using mocha both the test cases passes
mocha TestOne // Passes
mocha TestTwo // Passes
if I say npm test (which runs all the test cases) I get error as test case in TestOne file has modified the input file. How I can make test case not to cache input file (or force test case to create its own copy of input file)
To ensure the file is always loaded in its current form from disk, load it with fs:
var fs = require('fs');
var input = fs.readFileSync('./data/InputOne');
input = JSON.parse(input);
http://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_options