I am using Jest framework for unit testing and came across a scenario to mock the fs.readFile.I have used spyOn and mocked the implementation.My code below
test_file.ts
import * as fs from 'fs';
it('read File', () => {
const spy = jest.spyOn(fs, 'readFile')
.mockImplementation((_, callback) => callback(null, Buffer.from('Sample')));
// Calling the function
myFunction('./path');
expect(spy).toHaveBeenCalled();
});
The spy is not called when i run the test case and the mock is not working.The original implementation is always working.
My function using fs.readFile
myFunction = (path) => {
// Reading the file
fs.readFile(path, async (error, file) => {
console.log(error) // No such file error thrown instead of null
/** Block of code with async work**/
});
};
Briefly,what i am trying to do
How to mock the fs.readFile correctly ?
EDIT
When i tried to console the error in my original callback function,it threw error 'no such file' . But i am expecting the error to be null since I am mocking it to return value as null.
I am using Jest framework for unit testing and came across a scenario to mock the fs.readFile.I have used spyOn and mocked the implementation.My code below
test_file.ts
import * as fs from 'fs';
it('read File', () => {
const spy = jest.spyOn(fs, 'readFile')
.mockImplementation((_, callback) => callback(null, Buffer.from('Sample')));
// Calling the function
myFunction('./path');
expect(spy).toHaveBeenCalled();
});
The spy is not called when i run the test case and the mock is not working.The original implementation is always working.
My function using fs.readFile
myFunction = (path) => {
// Reading the file
fs.readFile(path, async (error, file) => {
console.log(error) // No such file error thrown instead of null
/** Block of code with async work**/
});
};
Briefly,what i am trying to do
How to mock the fs.readFile correctly ?
EDIT
When i tried to console the error in my original callback function,it threw error 'no such file' . But i am expecting the error to be null since I am mocking it to return value as null.
-
I just tested with
function myFunction(path) { fs.readFile(path, (error, res) => console.log('error', error, 'res', res)); }
and it works properly. Are you sure there's no issue with your function? EDIT: Be aware that the callback shouldn't beasync
. – JC Olivares Commented Jan 18, 2020 at 10:31 - I just tested with your version and it seems to be working fine. What's the current output? – JC Olivares Commented Jan 18, 2020 at 10:38
- Yes,my callback is async as it does some db work inside .What should i do now ? – Sathya Narayanan GVK Commented Jan 18, 2020 at 10:43
-
3
The test is passing when using your code. Are you sure you are importing the same
fs
module on both places? Can you post a full example (all files including imports)? – JC Olivares Commented Jan 18, 2020 at 12:01 - 1 yes, that was the problem here ! import statements were different..thanks – Sathya Narayanan GVK Commented Jan 20, 2020 at 4:19
1 Answer
Reset to default 4Thanks to @JC Olivares,
'fs' import was the problem here.In the test case file, i imported 'fs' as
import * as fs from 'fs';
But the file with function had import
import fs from 'fs';