1

Within a karma test beforeEach) block I would like to access (read) a file from the project directory. However, I do not know how to get access to that directory, since process.cwd() returns the directory the test is running in, which is a random private dir assigned by node or gulp.

How can I find what the project dir is within a running test?

describe.only('convertClaimTypes', function () {
    var claimTypes;

    before(function () {
        var base = process.cwd();
        claimTypes = fs.readFileSync(path.join(base, 'build/resources/claimTypes.json'));
        claimTypes = JSON.parse(claimTypes);
    });
...
ed4becky
  • 1,488
  • 1
  • 17
  • 54

1 Answers1

-1

Try using __dirname in place of process.cwd() as it gives you the directory of the current file rather than the directory of the executable.

For example, if your tests are within a directory /test and /build is a directory within the root of your application, access /build from /test like so:

path.resolve(__dirname, '../build/');
sdgluck
  • 24,894
  • 8
  • 75
  • 90
  • 1
    Tried that, that variable doesn't exist in the context of the running test. – ed4becky Aug 15 '15 at 16:52
  • It is a global variable maintained by Node, so I can't see why this would be the case. Perhaps it is that you are not writing the variable name correctly? It has two underscores at the beginning. – sdgluck Aug 15 '15 at 17:01
  • That's a pain. Thanks for the link. In which case it looks like you will need to hardcode an absolute path. You could have this path in a configuration file. – sdgluck Aug 17 '15 at 08:59
  • Thats what I was originally doing, but I have two machines with different paths to myproject – ed4becky Aug 18 '15 at 12:33
  • This is quite common. You can have different configs for each machine and use the NODE_ENV environment variable to denote which to use. – sdgluck Aug 18 '15 at 16:22