joshua-anderson joshua-anderson - 26 days ago 15
Node.js Question

mocha fs.existsSync always failing

I am attempting to check if a file exists in my mocha test. I know for a fact that the file exists in the test folder(I am placing it there for simplicity while I try and get this to work). But no matter what I do, fs.existsSync always fails. Logger.startup() creates the file. Logger.getFilename() returns a value like 5-17-30-2013.log. I am new to mocha so have no clue if I am making a classic blunder. As far as I know I am using mocha synchronously. Thanks for all of the help.

Here is my Mocha Test script:

var logger = require('../logger');
var fs = require('fs');

describe("Logger", function () {
it("Creates a file", function () {
logger.startup();
console.log(logger.getFilename());
if (!fs.existsSync(logger.getFilename())) {
throw ("Logging File Does Not Exist");
}
})
})

Answer

I followed @jjm and his advice on the program. After logging where it thought the file was, it turns out that it was looking in the folder "logger", not "logger/test" Apearently fs looks at the root of the project, the package.json folder, and not realative to the file calling it, which I did not know.