Access to projectDir from karma test?

304 views Asked by At

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);
    });
...
1

There are 1 answers

5
sdgluck On

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/');