Reputation: 2262
I am writing a unit test for file and path manipulations, but I cannot stub isFile
in fs.statSync(filePath).isFile()
.
I am getting the following error:
ReferenceError: isFile is not defined
My code is :
const readFiles = fs.readdirSync(directoryPath);
readFiles.map(file => {
filePath = path.resolve(process.cwd(), `${directoryPath}/${file}`);
const isFile = fs.statSync(filePath).isFile();
if (!isFile) {
const filesList = fs.readdirSync(filePath).map(fileName => file + '/' + fileName);
files = [
...files,
...filesList,
];
} else {
files = fs.readdirSync(directoryPath);
}
});
here is my before test code, to create the stubs and assign to rewired :
beforeEach(() => {
helpers.__set__({
'fs.statSync': sinon.stub().returns(true),
isFile: sinon.stub().returns(true),
'path.resolve': sinon.stub().returns('/a/b/c'),
});
});
please advise
Upvotes: 0
Views: 470
Reputation: 3331
If your stub for fs.statSync
works as intended, it provides true
. So you can't call isFile
on it.
So I suppose you can simply mock a function isFile
in the object provided by statSync
that would return true:
'fs.statSync': sinon.stub().returns({ isFile: () => true})
Upvotes: 2