I am very new to jest and mocking, I have a fs module read stream, which has 2 events on and data, i am trying to mock below code
ReadFile.js
const csv = require('csv-parser');
let storeData=[];
csvFileReader() {
fs.createReadStream(path.resolve(__dirname, "./abc.csv"))
.pipe(csv())
.on('data', async (row) => {
storeData[0] = row.postcode;
})
.on('end', () => {
console.log('Done')
});
}
ReadFileTest.js
import ReadFile from './readFile.js';
const fs = require('fs');
jest.mock('fs');
describe('Load File', () => {
const readFile= new ReadFile();
test('Test data handler', async () => {
const mockPipeOn = { on: jest.fn().mockImplementation(function(this, event, handler) {
if (event === 'data') {
jest.fn.mockReturnValueOnce("Reading Data")
}
if (event === 'end') {
jest.fn.mockReturnValueOnce("Completed Reading Data")
}
return this;
}), };
const mockReadStream = { pipe: jest.fn().mockReturnValueOnce(mockPipeOn) };
const createReadStream = jest.fn().mockReturnValueOnce(mockReadStream);
await readFile.csvFileReader();
});
});
I am getting error on 'this' key word its not going into 'data' and 'end' error handler