'Writing test to nodejs function that saves string as columns to CSV file
I am trying to write jest test to a nodejs function which basically gets a string as an argument and writes that string as a columns to CSV file using fs's createWriteStream function. Below is the code.
import fs from 'fs';
export default (str) => {
if (!str) return console.log('Please enter string');
const stream = fs.createWriteStream('data.csv');
[...str].map(i => stream.write(i + ', ')).join(',');
stream.end();
console.log('CSV created!');
return 'CSV created!';
}
The test I wrote is not complete which is below.
import CSV from './CSV';
import fs from 'fs';
test('test csv', () => {
expect(CSV('hello world')).toBe('CSV created!');
});
Ofcourse above test gets passed but I want it to be tested properly. I think jest.fn() is something needs to be used. I went through it docs but still don't know how to properly use it in this case. My question is how can I write jest test for above function?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
