3

I'm trying to run some tests on node using karma. I'm running using both phantom and real browsers.

Whichever way I run I get an error on fs read file functions.

 'undefined' is not an object (evaluating 'fs.existsSync')

This is even if I have a very simple file like:

var fs = require('fs');
console.error(fs);
var text = fs.readFile('data.txt', 'utf8');

The first console writes out Object {}. The second one gives me the above error. I'm assuming that the object is empty.

I'm using the latest version of karma and dependencies.

Can anybody point me in the right direction as to why the fs object is empty/not working.

user281921
  • 661
  • 2
  • 8
  • 25
  • From some investigations, it seems it might have something to do with the fact that the 'fs' is a node server object and karma is basically a browser runner which has no access to the local resources. – user281921 Jul 23 '15 at 15:30

1 Answers1

1

Karma is the client side js test runner you cannot use node file system on it. To test server side js I suggest to use a different test runner like Mocha. If you use Mocha for the server js namely nodejs you will be able to use node filesystem.

There is another way to read file from the client is using XMLHttpRequest(). Refer you to this question Javascript - read local text file

Community
  • 1
  • 1