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

SharpCoder
  • 18,279
  • 43
  • 153
  • 249

1 Answers1

0

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

jgillich
  • 71,459
  • 6
  • 57
  • 85
  • I am trying to make use of the code you suggested but i ran into another problem: this is the code I am using now to read file: `var input = fs.readFileSync('./Specs/queries/inOne.json'); input = JSON.parse(input);` Now when I say `npm test` system runs test case but when I say `mocha TestOne` I get following error `no such file or directory c:\testcase\specs\Specs\Queries\inOne.json` – SharpCoder Aug 05 '14 at 09:38
  • How I can ensure both the commands uses the same path. When i use mocha it is automatically appending `specs` folder – SharpCoder Aug 05 '14 at 09:40
  • @PUT_OR_POST See `__dirname`: http://nodejs.org/docs/latest/api/globals.html#globals_dirname – jgillich Aug 05 '14 at 09:53
  • Thanks for quick help but looks like I cannot use `__dirname` in my code. See this link : http://stackoverflow.com/questions/8817423/node-dirname-not-defined – SharpCoder Aug 05 '14 at 10:31